#include<cmath>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar getchar_unlocked
#endif
template<typename T> void read(T &x) { int f = 1; x = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-')f = -1; ch = getchar(); }while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }x *= f; }
template<typename T, typename ...Args> void read(T &x, Args &...args) { read(x); read(args...); }
template<typename T> void print(T x) { if (x < 0) { putchar('-'); x = -x; }if (x > 9) { print(x / 10); }putchar(char(x % 10 + 48)); }
template<typename T, typename ...Args> void print(T x, Args ...args) { print(x); putchar(' '); print(args...); }
typedef long long ll;
typedef long double db;
const int N = 2e5 + 10;
const int INT_INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
template<typename T1, typename T2> ll _pow(T1 a, T2 b) { ll x = 1, y = a; while(b > 0) {if (b & 1) x *= y; y *= y; b >>= 1; } return x; }
ll p[2 * N];
vector<ll> a[N];
vector<pair<ll, ll> > dp[N];
int main() {
int n, m, x; read(n, m);
for (int i = 1; i <= n; i++) {
a[i].push_back(0);
for (int j = 1; j <= m; j++) {
read(x);
a[i].push_back(x);
}
}
for (int i = 1; i < n + m; i++) read(p[i]);
ll new_f, new_s;
for (int i = 1; i <= n; i++) {
dp[i].push_back(make_pair(0, 0));
for (int j = 1; j <= m; j++) {
if (i == 1 && j == 1) {
dp[i].push_back(make_pair(max(0ll, p[1] - a[i][j]), max(0ll, a[i][j] - p[1])));
continue;
}
dp[i].push_back(make_pair(LL_INF, -1));
if (i != 1) {
if (dp[i - 1][j].second + a[i][j] >= p[i + j - 1]) {
new_f = dp[i - 1][j].first;
new_s = dp[i - 1][j].second + a[i][j] - p[i + j - 1];
}
else {
new_f = p[i + j - 1] - dp[i - 1][j].second - a[i][j] + dp[i - 1][j].first;
new_s = 0;
}
if (new_f < dp[i][j].first) {
dp[i][j].first = new_f;
dp[i][j].second = new_s;
}
else if (new_f == dp[i][j].first) {
dp[i][j].second = max(dp[i][j].second, new_s);
}
}
if (j != 1) {
if (dp[i][j - 1].second + a[i][j] >= p[i + j - 1]) {
new_f = dp[i][j - 1].first;
new_s = dp[i][j - 1].second + a[i][j] - p[i + j - 1];
}
else {
new_f = p[i + j - 1] - dp[i][j - 1].second - a[i][j] + dp[i][j - 1].first;
new_s = 0;
}
if (new_f < dp[i][j].first) {
dp[i][j].first = new_f;
dp[i][j].second = new_s;
}
else if (new_f == dp[i][j].first) {
dp[i][j].second = max(dp[i][j].second, new_s);
}
}
}
}
print(dp[n][m].first);
return 0;
}