#include <bits/stdc++.h>
using namespace std;

//#define int long long
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define yn(x) ((x) ? "Yes" : "No")

typedef pair <int, int> ii;
typedef pair <int, ii> iii;

const int siz = 80 + 1;
const int SIZ = 2 * 6400 + 1;
const int mod = 1e9 + 7;
const int maxx = 2e9;
const int MAXX = 1e18;
const string file = "1";

int a[siz][siz];
bool f[siz][siz][SIZ];

int32_t main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

//    freopen ((file + ".inp").c_str(), "r", stdin);
//    freopen ((file + ".out").c_str(), "w", stdout);

    int m, n;
    cin >> m >> n;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
    }

    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            int x;
            cin >> x;

            a[i][j] = abs(a[i][j] - x);
        }
    }

    f[1][1][a[1][1]] = true;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            for (int k = 0; k < SIZ; k++) {
                if (f[i][j][k] == true) {
                    if (i < m) {
                        f[i + 1][j][abs(k - a[i + 1][j])] = true;
                        f[i + 1][j][k + a[i + 1][j]] = true;
                    }

                    if (j < n) {
                        f[i][j + 1][abs(k - a[i][j + 1])] = true;
                        f[i][j + 1][k + a[i][j + 1]] = true;
                    }
                }
            }
        }
    }

    for (int k = 0; k < SIZ; k++) {
        if (f[m][n][k] == true) {
            return cout << k << "\n", 0;
        }
    }

//    cerr << "Time: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";

    return 0;
}

#include <bits/stdc++.h>
#define rp(i,n) for(int i=0; i<n; i++)
using namespace std;
using ll=long long;
using P=pair<int,int>;
using G=vector<vector<int>>;
bool dp[6410][20000];
int abs(int x,int y){
    if(x>=y) return x-y;
    else return y-x;
}
int main(){
    int h,w;
    cin >> h >> w;
    vector<int> a(h*w), b(h*w);
    rp(i,h*w) cin >> a[i];
    rp(i,h*w) cin >> b[i];
    rp(i,6410){
        rp(j,20000) dp[i][j]=false;
    }
    int m=0;
    rp(i,h*w) m=max(m,abs(a[i],b[i]));
    dp[0][abs(a[0],b[0])]=true;
    for(int i=1; i<h*w; i++){
        int dif=abs(a[i],b[i]);
        if(i%w){
            for(int k=0; k<=(h+w)*m;k++){
                if(dp[i-1][k]){
                    dp[i][abs(k,dif)]=true;
                    if(k+dif<=(h+w)*m*2) dp[i][k+dif]=true;
                }
            }
        }
        if(i>=w){
            for(int k=0; k<=(h+w)*m;k++){
                if(dp[i-w][k]){
                    dp[i][abs(k,dif)]=true;
                    if(k+dif<=(h+w)*m*2) dp[i][k+dif]=true;
                }
            }
        }
    }
    int ans;
    int c=0;
    while(1){
        if(dp[h*w-1][c]){
            ans=c;
            break;
        }
        c++;
    }
    cout << ans << endl;
    return 0;
}