#include<bits/stdc++.h>

using namespace std;

#define rep(x, L, R) for(int x = (L), _x = (R); x <= _x; x++)
#define per(x, R, L) for(int x = (R), _x = (L); x >= _x; x--)
#define broken fprintf(stderr, "running on %s %d\n", __FUNCTION__, __LINE__)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define SZ(v) (int)(v).size()
#define ALL(x) (x).begin(), (x).end()
#define x first
#define y second
#define mp make_pair
#define pb push_back

template<typename T> inline bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long ll;
const int K = 15625;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n, m;
int p5[6];
int a[6], b[6];
ll c[6][6];
ll dp[10][6][K];

void ckmin(ll &x, ll y) {
    if(x > y) x = y;
}

int main() {
    scanf("%d%d", &n, &m);
    p5[0] = 1;
    for(int i = 1; i < m; i++) p5[i] = p5[i - 1] * 5;
    for(int i = 0; i < n; i++) scanf("%d", &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("%lld", &c[i][j]);
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0][0] = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < a[i]; j++) {
            for(int msk = 0; msk < K; msk++) {
                for(int k = 0; k < m; k++) {
                    int p = (msk / p5[k]) % 5;
                    for(int l = 1; l <= a[i] - j && l <= b[k] - p; l++) {
                        ckmin(dp[i][j + l][msk + l * p5[k]], dp[i][j][msk] + c[i][k]);
                    }
                }
            }
        }
        for(int msk = 0; msk < K; msk++) {
            dp[i + 1][0][msk] = dp[i][a[i]][msk];
        }
    }
    ll ans = inf;
    for(int msk = 0; msk < K; msk++) ckmin(ans, dp[n][0][msk]);
    printf("%lld\n", ans >= inf ? -1 : ans);
    return 0;
}