#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define M 1000000007
#define M2 998244353
#define ll long long
#define pll pair<long,long>
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define REPI(i,a,b) for(ll i=b-1;i>=a;i--)
#define ff first
#define ss second
#define pb push_back
#define db pop_back
#define mp make_pair
#define mt make_tuple
#define g(a,b) get<a>(b)
#define INF (ll)1e18+5
#define ull unsigned long long
 
#define o_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define o_setll tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
 
ll modI(ll a, ll m);
ll gcd(ll a, ll b);
ll powM(ll x, ll y, ll m);
void pairsort(int a[], int b[], int n);
void pairsortll(ll a[],ll b[],ll n);
ll logint(ll x,ll y);
 
 
ll gcd(ll x,ll y)
{
    if(x==0) return y;
    return gcd(y%x,x);
}
 
ll powM(ll x,ll y,ll m)
{
    if(y==0) return 1;
    ll p=powM(x,y/2,m)%m;
    p=(p*p)%m;
    return (y%2==0)?p:(x*p)%m;
}
 
ll modI(ll a, ll m)
{
   return powM(a, m-2, m);
}
 
void pairsort(int a[],int b[],int n)
{
    pair<int,int> v[n];
    REP(i,0,n)
    {
        v[i].ff=a[i];
        v[i].ss=b[i];
    }
    sort(v,v+n);
    REP(i,0,n)
    {
        a[i]=v[i].ff;
        b[i]=v[i].ss;
    }
}
 
void pairsortll(ll a[],ll b[],ll n)
{
    pair<ll,ll> v[n];
    REP(i,0,n)
    {
        v[i].ff=a[i];
        v[i].ss=b[i];
    }
    sort(v,v+n);
    REP(i,0,n)
    {
        a[i]=v[i].ff;
        b[i]=v[i].ss;
    }
}
 
ll logint(ll x,ll y)
{
    ll ans=0;
    ll a=1;
    for(ll i=0;i<=x;i++)
    {
        if(x<a)
        {
            return ans;
        }
        ans++;
        a*=y;
    }
    return -1;
}
const int N = 1e5+5;
/*ll parent[N];
ll siz[N];
void make(){
    REP(i, 0, N){
        parent[i]=i;
        siz[i] = 1;
    }
}
ll find(ll x){
    while(parent[x] != x){
        x = parent[x];
    }
    return x;
}
void unite(ll a, ll b){
    a = find(a);
    b = find(b);
    if(a != b){
        if(siz[a] < siz[b]){
            swap(a, b);}
        parent[b] = a;
        if(siz[a] == siz[b]){
            siz[a]++;
        }
    }
} */
/*struct node{
    ll ans;
    ll freq;
    node(){
        ans=0;
        freq=0;
    }
};
ll n, m;
ll ar[N];
node stree[1<<18];
node combine(node l, node r){
    node curr;
    ll a = l.ans;
    ll b = r.ans;
    curr.freq=0;
    ll val = __gcd(a, b);
    curr.ans=val;
    if(a==val){
        curr.freq += l.freq;
    }
    if(b == val){
        curr.freq += r.freq;
    }
    return curr;
}
void build(ll l, ll r, ll v){
    if(l == r){
        node curr;
        curr.ans=ar[l];
        curr.freq = 1;
        stree[v]=curr;
    }
    else{
        ll mid = (l+r)/2;
        build(l, mid, 2*v);
        build(mid+1, r, 2*v+1);
        stree[v] = combine(stree[2*v], stree[2*v+1]);
        //return stree[v];
    }
}

node query(ll l, ll r, ll v, ll ql, ll qr){
    if(ql > r || qr < l){
        return node();
    }
    if(ql <= l && qr >= r){
        return stree[v];
    }
    else{
        ll mid = (l+r)/2;
        ll left= 2*v;
        ll right = 2*v+1;
        return combine(query(l, mid, left, ql, qr), query(mid+1, r, right, ql, qr));
    }
}*/
int dp[81][81][2];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    /* 
    Hey, 
    You, 
    Dont practice this, 
    Easy Question. 
    */
    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];
        }
    }
    int c[h][w];int maxi=0;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            cin>>b[i][j];
            c[i][j] = abs(a[i][j]-b[i][j]);
            maxi = max(maxi, c[i][j]);
        }
    }
    int take = 40*(h+w)+10;
    int dp[h][w][take];
    memset(dp, 0, sizeof(dp));
    dp[0][0][c[0][0]] = 1;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            if(i == 0 && j == 0){continue;}
            for(int k=0; k< take; k++){
                if(i > 0){
                    dp[i][j][k] |= dp[i-1][j][abs(k-c[i][j])];
                    if(k+c[i][j] < take){
                        dp[i][j][k] |= dp[i-1][j][k+c[i][j]];
                    }
                }
                if(j > 0){
                    dp[i][j][k] |= dp[i][j-1][abs(k-c[i][j])];
                    if(k+c[i][j] < take){
                        dp[i][j][k] |= dp[i][j-1][k+c[i][j]];
                    }
                }
            }
        }
    }

    for(int i=0; i<take; i++){
            if(dp[h-1][w-1][i]){
                cout<<i;
                return 0;
            }
        }
    }

Measured memory_bytes: 160640