#include <bits/stdc++.h>
#define i64 long long
#define sz(a) int((a).size())
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b) - 1; i >= (a); --i)
using namespace std;

const int N = 7, Ns = 125 * 125, int_max = 1e9;
int na, nb, ns, a[N], b[N], c[N][N];
int pw5[N], f[Ns], g[Ns];

void dfs(int i, int d, int s, int t, int sb, int sc) {
	if (d == nb) {
		if (sb >= a[i]) {g[t] = min(g[t], f[s] + sc);}
		return;
	}
	rep(b_s, 0, b[d] + 1) {rep(b_t, b_s, b[d] + 1)
	 {dfs(i, d + 1, s + pw5[d] * b_s, t + pw5[d] * b_t,
	 sb + (b_t - b_s), sc + (b_s == b_t ? 0 : c[i][d]));}}
}

int main() {
	// freopen("#.in", "r", stdin); // @
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin >> na >> nb, pw5[0] = 1;
	rep(i, 0, nb) {pw5[i + 1] = pw5[i] * 5;}
	int sum_a = 0, sum_b = 0; ns = pw5[nb];
	rep(i, 0, na) {cin >> a[i]; sum_a += a[i];}
	rep(i, 0, nb) {cin >> b[i]; sum_b += b[i];}
	rep(i, 0, na) {rep(j, 0, nb) {cin >> c[i][j];}}
	if (sum_a > sum_b) {cout << "-1\n"; return 0;}
	rep(s, 0, ns) {f[s] = g[s] = int_max;} f[0] = 0;
	rep(i, 0, na) {
		dfs(i, 0, 0, 0, 0, 0);
		rep(s, 0, ns) {f[s] = g[s], g[s] = int_max;}
	}
	int res = int_max;
	rep(s, 0, ns) {res = min(res, f[s]);}
	cout << res << '\n';
	return 0;
}

/* 📌https://www.cnblogs.com/George1123/p/tips.html */