#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "debug.cpp"
#else
#define debug(...) 
#endif
template<typename T> bool updmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template<typename T1, typename T2>
struct default_map: map<T1, T2> {
    T2 value;
    default_map(T2 value) {
        this->value = value;
    }
    T2 &operator[](T1 key) {
        if (this->count(key) == 0) {
            this->insert({key, this->value});
        }
        return this->at(key);
    }
};
void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> b(m);
    for (int i = 0; i < m; i++) {
        cin >> b[i];
    }
    vector<vector<int>> c(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> c[i][j];
        }
    }
    int ans = 1e9;
    default_map<tuple<vector<int>, int, int>, int> dp(1e9);
    dp[{vector<int>(m), 0, 0}] = 0;
    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= a[i]; j++) {
            vector<int> cur(m);
            function<void(int)> go = [&](int k) {
                if (k == m) {
                    if (dp[{cur, i, j}] > 5e8) {
                        return;
                    }
                    if (i == n) {
                        updmin(ans, dp[{cur, i, j}]);
                        return;
                    }
                    if (j < a[i]) {
                        for (int t = 0; t < m; t++) {
                            if (cur[t] < b[t]) {
                                int x = min(b[t] - cur[t], a[i] - j);
                                for (int xx = 1; xx <= x; xx++) {
                                    auto next = cur;
                                    next[t] += xx;
                                    updmin(dp[{next, i, j + xx}], dp[{cur, i, j}] + c[i][t]);
                                }
                            }
                        }
                    } else {
                        updmin(dp[{cur, i + 1, 0}], dp[{cur, i, j}]);
                    }
                    return;
                }
                for (int i = 0; i <= b[k]; i++) {
                    cur[k] = i;
                    go(k + 1);
                }
            };
            go(0);
        }
    }
    if (ans < 5e8) {
        cout << ans << endl;
    } else {
        cout << -1 << endl;
    }
}
 
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}