#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
#define MAXN 100005
#define uns unsigned
#define INF 0x3f3f3f3f
using namespace std;
inline ll read(){
	ll x=0;bool f=1;char s=getchar();
	while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
	while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
	return f?x:-x;
}
int n,m,a[10],b[10];
int c[10][10];
int dp[15630][7][7][5],ans=INF;
struct itn{int sk[7];};
inline int getcd(itn a){
	int code=0;
	for(int i=1;i<=n;i++)code=code*5+a.sk[i];
	return code;
}
inline itn getst(int c){
	itn state={0,0,0,0,0,0,0};
	for(int i=n;i>0;i--)state.sk[i]=c%5,c/=5;
	return state;
}
inline bool check(itn s){
	for(int i=1;i<=n;i++)
		if(s.sk[i]<a[i])return 0;
	return 1;
}
int main()
{
	n=read(),m=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=m;i++)b[i]=read();
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			c[i][j]=read();
	memset(dp,0x3f,sizeof(dp));
	dp[0][1][1][0]=0;
	int M=pow(5,n);
	for(int S=0;S<M;S++){
		itn s=getst(S);bool ok=1;
		for(int i=1;i<=n;i++)if(s.sk[i]>a[i])ok=0;
		for(int j=1;ok&&j<=m;j++)
			for(int i=1;i<=n;i++)
				for(int r=0;r<5;r++){
					if(r>b[j])break;
					for(int f=0;f<5;f++){
						if(s.sk[i]+f>a[i]||r+f>b[j])break;
						itn nw=s;
						nw.sk[i]+=f;
						int ad=(f?c[i][j]:0),ni=i,nj=j,nr=r+f;
						if(i==n)ni=1,nj++,nr=0;
						else ni++;
						if(check(nw))ans=min(ans,dp[S][i][j][r]+ad);
						if(nj<=m){
							int cd=getcd(nw);
							dp[cd][ni][nj][nr]=min(dp[cd][ni][nj][nr],dp[S][i][j][r]+ad);
						}
					}
				}
	}
	if(ans>=INF)ans=-1;
	printf("%d\n",ans);
	return 0;
}