#include <cstdio>
#include <cstdlib>
#include <cstring>
#define CLR(x) memset((x), false, sizeof(x))
const int MAXD = 12801;
const int MAXN = 81;
int  A[2][MAXN][MAXN];
bool   dp[MAXN][MAXN][MAXD];
int H, W;

int solve() {
  CLR(dp);
  dp[0][0][0] = true;
  dp[0][1][0] = true;
  dp[1][0][0] = true;
  for (int i = 1; i <= H; ++i) {
    for (int j = 1; j <= W; ++j) {
      for (int k = 0; k < MAXD; ++k) {
        int d = std::abs(A[0][i][j] - A[1][i][j]);
        int nk1 = k + d;
        if (nk1 < MAXD) {
          dp[i][j][k] |= dp[i-1][j][nk1] | dp[i][j-1][nk1];
        }
        int nk2 = std::abs(k - d);
        dp[i][j][k] |= dp[i-1][j][nk2] | dp[i][j-1][nk2];
      }
    }
  }
  for (int k = 0; k < MAXD; ++k) {
    if (dp[H][W][k]) {
      return k;
    }
  }
  return -1;
}

int main() {
  scanf(" %d%d", &H, &W);
  for (int k = 0; k < 2; ++k) {
    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j) {
        scanf("%d", &A[k][i][j]);
      }
    }
  }
  printf("%d\n", solve());
  return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int H, W;
    cin >> H >> W;
    vector<vector<int>> A(H, vector<int>(W)), B(H, vector<int>(W));
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            cin >> A.at(i).at(j);
        }
    }
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            cin >> B.at(i).at(j);
        }
    }

    vector<vector<int>> d(H, vector<int>(W));
    for (int i = 0; i < H; i++)
    {
        for (int j = 0; j < W; j++)
        {
            d.at(i).at(j) = abs(A.at(i).at(j) - B.at(i).at(j));
        }
    }

    auto to_id = [&](int i, int j) {
        return i * W + j;
    };
    vector<vector<bool>> dp(H * W, vector<bool>(80 * 160 + 1));
    dp.at(to_id(0, 0)).at(d.at(0).at(0)) = true;
    for (int i = 1; i < H; i++)
    {
        for (int k = 0; k < 80 * 160 + 1; k++)
        {
            if (dp.at(to_id(i - 1, 0)).at(k))
            {
                dp.at(to_id(i, 0)).at(abs(k + d.at(i).at(0))) = true;
                dp.at(to_id(i, 0)).at(abs(k - d.at(i).at(0))) = true;
            }
        }
    }
    for (int j = 1; j < W; j++)
    {
        for (int k = 0; k < 80 * 160 + 1; k++)
        {
            if (dp.at(to_id(0, j - 1)).at(k))
            {
                dp.at(to_id(0, j)).at(abs(k + d.at(0).at(j))) = true;
                dp.at(to_id(0, j)).at(abs(k - d.at(0).at(j))) = true;
            }
        }
    }
    for (int i = 1; i < H; i++)
    {
        for (int j = 1; j < W; j++)
        {
            for (int k = 0; k < 80 * 160 + 1; k++)
            {
                if (dp.at(to_id(i - 1, j)).at(k))
                {
                    dp.at(to_id(i, j)).at(abs(k + d.at(i).at(j))) = true;
                    dp.at(to_id(i, j)).at(abs(k - d.at(i).at(j))) = true;
                }
                if (dp.at(to_id(i, j - 1)).at(k))
                {
                    dp.at(to_id(i, j)).at(abs(k + d.at(i).at(j))) = true;
                    dp.at(to_id(i, j)).at(abs(k - d.at(i).at(j))) = true;
                }
            }
        }
    }
    for (int k = 0; k < 80 * 160 + 1; k++)
    {
        if (dp.at(to_id(H - 1, W - 1)).at(k))
        {
            cout << k << endl;
            break;
        }
    }
    return 0;
}
