#include<bits/stdc++.h>
#include<string.h>
 
typedef long long int ll;
#define all(x) (x).begin(), (x).end()
 
using namespace std;

int nxt() {
    int x;
    cin >> x;
    return x;
}

ll nxtl(){
    ll x;
    cin>>x;
    return x;
}
 
void SieveOfEratosthenes(int n,vector <int> &primes) 
{ 
    // Create a boolean array "prime[0..n]" and initialize 
    // all entries it as true. A value in prime[i] will 
    // finally be false if i is Not a prime, else true. 
    bool prime[n+1]; 
    memset(prime, true, sizeof(prime)); 
  
    for (int p=2; p*p<=n; p++) 
    { 
        // If prime[p] is not changed, then it is a prime 
        if (prime[p] == true) 
        { 
            // Update all multiples of p greater than or  
            // equal to the square of it 
            // numbers which are multiple of p and are 
            // less than p^2 are already been marked.  
            for (int i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
  
    // Print all prime numbers 
    for (int p=2; p<=n; p++) 
       if (prime[p]) 
          primes.push_back(p);
} 
 
ll max(ll a,ll b)
{
    if(a>b)
        return a;
    return b;
}
 
ll power(ll x, ll y,ll mod) 
{ 
    ll temp; 
    if( y == 0) 
        return 1; 
    temp = power(x, y/2,mod); 
    if (y%2 == 0) 
        return (temp*temp)%mod; 
    else
        return (((x*temp)%mod)*temp)%mod; 
} 

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int r=nxt(),c=nxt();
    vector <vector <pair<int,int> > > v;
    vector <pair<int,int> > row(c);
    v.resize(r,row);
    int m=0;
    for(int i=0;i<r;i++)
        for(int j=0;j<c;j++)
            cin>>v[i][j].first;
    for(int i=0;i<r;i++)
        for(int j=0;j<c;j++){
            cin>>v[i][j].second;
            if(m<abs(v[i][j].first-v[i][j].second))
                m=abs(v[i][j].first-v[i][j].second);
        }
    m*=(r+c-1);
    vector < bitset <12801> > prev(c),cur(c);
    prev[0][abs(v[0][0].first-v[0][0].second)]=1;
    for(int i=1;i<c;i++)
    {
        for(int j=0;j<=m;j++)
        {
            if(prev[i-1][j])
                prev[i][j+abs(v[0][i].first-v[0][i].second)]=1;
            if(prev[i-1][j])
                prev[i][abs(j-abs(v[0][i].first-v[0][i].second))]=1;
        }
    }
    for(int i=1;i<r;i++)
    {
        for(int j=0;j<=m;j++)
        {
            if(prev[0][j]){
            cur[0][j+abs(v[i][0].first-v[i][0].second)]=1;
            cur[0][abs(j-abs(v[i][0].first-v[i][0].second))]=1;}
        }

        for(int k=1;k<c;k++)
        {int diff=abs(v[i][k].first-v[i][k].second);
            for(int j=0;j<=m;j++)
            {
                if(prev[k][j]){
                cur[k][j+diff]=1;
                cur[k][abs(j-diff)]=1;}
                if(cur[k-1][j]){
                cur[k][j+diff]=1;
                cur[k][abs(j-diff)]=1;}
            }
        }
        for(int j=0;j<c;j++)
            for(int k=0;k<=m;k++)
                prev[j][k]=cur[j][k];
        for(int j=0;j<c;j++)
            for(int k=0;k<=m;k++)
                cur[j][k]=0;
    }
    int ans;
    for(int j=0;j<=m;j++)
        if(prev[c-1][j])
        {
            ans=j;
            break;
        }
    cout<<ans<<endl;

    

    return 0;   
}


 
 


Measured memory_bytes: 512