#include <stdio.h>
typedef long long ll;

ll abs(ll a) {
  return a > 0 ? a : -a;
}

int main(void) {
  ll i, j, k, h, w;
  scanf("%lld%lld", &h, &w);
  ll a[h][w], b[h][w], d[h][w];
  bool dp[w][80 * (h + w)], tmp[w][80 * (h + w)];
  for(j = 0; j < w; ++j) for(k = 0; k < 80 * (h + w); ++k) dp[j][k] = false;
  for(i = 0; i < h; ++i) for(j = 0; j < w; ++j) scanf("%lld", &a[i][j]);
  for(i = 0; i < h; ++i) for(j = 0; j < w; ++j) scanf("%lld", &b[i][j]), d[i][j] = abs(a[i][j] - b[i][j]);
  for(i = 0; i < w; ++i) for(j = 0; j < 80 * (h + w); ++j) dp[i][j] = false;
  dp[0][d[0][0]] = true;
  for(i = 1; i < w; ++i) for(j = 0; j < 80 * (h + w); ++j) if(dp[i - 1][j]) {
    dp[i][j + d[0][i]] = true;
    dp[i][abs(j - d[0][i])] = true;
  }
  for(k = 1; k < h; ++k) {
    for(i = 0; i < w; ++i) for(j = 0; j < 80 * (h + w); ++j) tmp[i][j] = false;
    for(i = 0; i < 80 * (h + w); ++i) if(dp[0][i]) {
      tmp[0][i + d[k][0]] = true;
      tmp[0][abs(i - d[k][0])] = true;
    }
    for(i = 1; i < w; ++i) for(j = 0; j < 80 * (h + w); ++j) {
      if(tmp[i - 1][j]) {
        tmp[i][j + d[k][i]] = true;
        tmp[i][abs(j - d[k][i])] = true;
      }
      if(dp[i][j]) {
        tmp[i][j + d[k][i]] = true;
        tmp[i][abs(j - d[k][i])] = true;
      }
    }
    for(i = 0; i < w; ++i) for(j = 0; j < 80 * (h + w); ++j) dp[i][j] = tmp[i][j];
  }
  for(i = 0; ; ++i) if(dp[w - 1][i]) {
    printf("%lld", i);
    return 0;
  }
}
#include <bits/stdc++.h>
#include<iostream>
#include<vector>
#include<map>
#include<math.h>

#define irep(i,n) for(int i = 0; i < (int)(n); i++)
#define irep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++)
#define irrep(i,n) for(int i = (int)(n-1); i > -1; i--)
#define irrep2(i,n,a) for(int i = (int)(n); i >= (int)(a); i--)

#define fi first
#define se second

using namespace std;

using ll = long long;
using v_int = vector<int>;
using v2_int = vector<v_int>;
using v_ll = vector<ll>;
using v2_ll = vector<v_ll>;
using v_string = vector<string>;
using v_bool = vector<bool>;
using v2_bool = vector<v_bool>;
using pii = pair<int, int>;
using mii = map<int, int>;

const double PI = 3.1415926535897932;
const int INF = (int)2e9;
const ll LINF = (ll)2e18;
const ll MOD = 1000000007;
const int dc[8] = {1, -1, 0,  0, 1, -1,  1, -1};
const int dr[8] = {0,  0, 1, -1, 1,  1, -1, -1};

template<typename T, typename U> inline
ostream& operator<<(ostream &o, pair<T,U> &p) {
    return o << "{" << p.first << "," << p.second << "}";
}
template<typename T> inline
istream& operator>>(istream &is, vector<T> &vec) {
    for(auto &v : vec) { is >> v; }
    return is;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<T> &vec) {
    for(auto &v : vec) { os << v << ",";}
    return os;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<vector<T> > &mat) {
    for(auto &row : mat) { os << row << endl; }
    return os;
}

template<typename T> inline
void chmin(T &a, T b) { a = min(a, b); }
template<typename T> inline
void chmax(T &a, T b) { a = max(a, b); }

int main(void)
{
    int n, l;
    cin >> n >> l;

    int sum = n * l + n*(n+1)/2 - n;
    int ans = INF;
    int apple = 0;
    
    irep2(i, 1, n) {
        int eat = sum - (l + i - 1);
        if(ans > abs(eat-sum)) {
            apple = i;
            ans = abs(eat-sum);
        }
    }
    cout << sum - (l + apple -1) << endl;
    return 0;
} /*atcoder*/

