#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);

int DP[85][85][30000]; 

int main() {
  int H, W;
  cin>>H>>W;
  int A[H][W], B[H][W];
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      cin>>A[i][j];
    }
  }
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      cin>>B[i][j];
    }
  }
  for(int i=0;i<H;++i) {
    for(int j=0;j<W;++j) {
      A[i][j] -= B[i][j];
    }
  }
  DP[0][0][15000+A[0][0]] = 1;
  DP[0][0][15000-A[0][0]] = 1;
  for(int i=0;i<H-1;++i) {
    for(int j=0;j<30000;++j) {
      if(DP[i][0][j]>0) {
        DP[i+1][0][j+A[i+1][0]] = 1;
        DP[i+1][0][j-A[i+1][0]] = 1;
      }
    }
  }
  for(int i=0;i<W-1;++i) {
    for(int j=0;j<30000;++j) {
      if(DP[0][i][j]>0) {
        DP[0][i+1][j+A[0][i+1]] = 1;
        DP[0][i+1][j-A[0][i+1]] = 1;
      }
    }
  }
  for(int i=1;i<H;++i) {
    for(int j=1;j<W;++j) {
      for(int k=0;k<30000;++k) {
        if(DP[i-1][j][k]>0) {
          DP[i][j][k+A[i][j]] = 1;
          DP[i][j][k-A[i][j]] = 1;
        }
        if(DP[i][j-1][k]>0) {
          DP[i][j][k+A[i][j]] = 1;
          DP[i][j][k-A[i][j]] = 1;
        }
      }
    }
  }
  int ans = inf;
  for(int i=0;i<30000;++i) {
    if(DP[H-1][W-1][i]>0) ans = min(ans, abs(i-15000));
  }
  cout<<ans<<endl;
}

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T>
bool chmin(T& a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T>
bool chmax(T& a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for(int i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "\n" : " ");
    }
    return os;
}
template <typename T>
vector<T> make_v(size_t a) {
    return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
    t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
    for(auto& e : t) {
        fill_v(e, v);
    }
};
struct UnionFind {
    vector<int> par, siz;
    UnionFind(int n) {
        par.resize(n);
        siz.resize(n, 1);
        for(int i = 0; i < n; i++)
            par[i] = i;
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }
    bool unite(int u, int v) {
        u = find(u), v = find(v);
        if(u == v)
            return false;
        if(siz[u] < siz[v])
            swap(u, v);
        par[v] = u;
        siz[u] += siz[v];
        return true;
    }
    bool same(int u, int v) { return find(u) == find(v); }
};
struct edge {
    ll u, v, cost;
    bool operator<(const edge& e) { return cost < e.cost; }
};
ll d;
vector<ll> a;
vector<edge> es;
void calc(int l, int r) {
    if(r - l <= 1) {
        return;
    }
    int m = (l + r) / 2;
    ll mi = LINF, s, t;
    for(ll i = l; i < m; i++) {
        ll k = -i * d + a[i];
        if(mi > k) {
            mi = k;
            s = i;
        }
    }
    mi = LINF;
    for(ll i = m; i < r; i++) {
        ll k = i * d + a[i];
        if(mi > k) {
            mi = k;
            t = i;
        }
    }
    for(ll i = l; i < m; i++) {
        es.push_back({i, t, (t - i) * d + a[t] + a[i]});
    }
    for(ll i = m; i < r; i++) {
        es.push_back({s, i, (i - s) * d + a[i] + a[s]});
    }
    calc(l, m);
    calc(m, r);
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n >> d;
    a.resize(n + 1);
    for(ll i = 1; i <= n; i++) {
        cin >> a[i];
    }
    calc(1, n + 1);
    UnionFind uf(n + 1);
    sort(all(es));
    ll ans = 0;
    for(auto e : es) {
        if(uf.unite(e.u, e.v)) {
            ans += e.cost;
        }
    }
    cout << ans << endl;
}
