#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pi pair<int, int>
#define f first
#define s second

const int inf = 0x3f3f3f3f;
const int mxn = 6, k = 5, mxw = 15625;
int n, m, w, z;
int a[mxn], p[mxn], b[mxw], dp[mxw];

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> m;
	
	for(int i = 0; i < n; i++) cin >> a[i], z += a[i];
	
	for(int i = 0; i < m; i++){
		int x;
		cin >> x;
		p[i] = i ? k * p[i - 1] : 1;
		w += x * p[i], z -= x;
	} 
	
	if(z > 0){
		cout << -1 << endl;
		return 0;
	}

	for(int i = 0; i <= w; i++){
		dp[i] = inf;
		for(int j = 0; j < m; j++) b[i] += i / p[j] % k;
	}
	
	//knapsack distributing flow among to different chests so cut is all profits
	z = b[w], dp[w] = 0;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			int x;
			cin >> x;
			for(int l = 0; l <= w; l++)
			for(int y = 1; l + y * p[j] <= w && 
			y + l / p[j] % k <= w / p[j] % k; y++){
				dp[l] = min(dp[l], dp[l + y * p[j]] + x);
			}
		}
		z -= a[i];
		for(int j = 0; j <= w; j++) if(b[j] != z) dp[j] = inf;
	} 
	
	int ret = inf;
	for(int i = 0; i <= w; i++) if(b[i] == z) ret = min(ret, dp[i]);
	
	cout << ret << endl;
	
	return 0;
}