#include<bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N=6,M=15625,INF=(1<<30)-1;

int n,m,a[N+9],sa,b[N+9],c[N+9][N+9];

void into(){
  scanf("%d%d",&n,&m);
  for (int i=0;i<n;++i){
	scanf("%d",&a[i]);
	sa+=a[i];
  }
  for (int i=0;i<m;++i)
	scanf("%d",&b[i]);
  for (int i=0;i<n;++i)
	for (int j=0;j<m;++j)
	  scanf("%d",&c[i][j]);
}

int dp[N+9][M+9],ml[N+9];

void Dfs_dp(int id,int s,int k,int sum,int now){
  if (k>=m){
	if (sum==a[id]) dp[id+1][s]=min(dp[id+1][s],now);
	return;
  }
  int x=s/ml[k]%(b[k]+1);
  for (int i=0;sum+i<=a[id]&&x+i<=b[k];++i)
	Dfs_dp(id,s+i*ml[k],k+1,sum+i,now+(bool)i*c[id][k]);
}

int ans;

void Get_dp(){
  ml[0]=1;
  for (int i=1;i<=m;++i) ml[i]=ml[i-1]*(b[i-1]+1);
  for (int i=0;i<=n;++i)
	for (int s=0;s<ml[m];++s) dp[i][s]=INF;
  dp[0][0]=0;
  for (int i=0;i<n;++i)
	for (int s=0;s<ml[m];++s){
	  if (dp[i][s]==INF) continue;
	  Dfs_dp(i,s,0,0,dp[i][s]);
	}
  ans=INF;
  for (int s=0;s<ml[m];++s){
	int sum=0;
	for (int i=0;i<m;++i) sum+=s/ml[i]%(b[i]+1);
	//printf("%d : %d %d\n",s,sum,sa);
	if (sum==sa) ans=min(ans,dp[n][s]);
  }
}

void work(){
  Get_dp();
}

void outo(){
  printf("%d\n",ans==INF?-1:ans);
}

int main(){
  into();
  work();
  outo();
  return 0;
}