#include <iostream>
                                    #include <iomanip>
                                    #include <sstream>
                                    #include <cstring>
                                    #include <vector>
                                    #include <deque>
                                    #include <queue>
                                    #include <set>
                                    #include <map>
                                    #include <valarray>
                                    #include <iterator>
                                    #include <functional>
                                    #include <limits>
                                    #include <algorithm>
                                    #include <numeric>
                                    #include <cmath>
                                    #include <cassert>
                                    #include <unordered_map>
                                    #include <unordered_set>
                                    #include <stack>
                                    using namespace std;
                                    #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
                                     #define trace(x) cerr << #x << ": " << x << " " << endl;
                                 
                                    typedef int64_t ll;
                                 
                                    #define endl '\n'
                                    //#define int ll
                                    ll mod=1e9+7;
                                    ll mod1=1e9+5;
                                    
                                    ll power(ll a,ll b)
                                    {
                                        if(b==0) return 1;
                                        else if(b%2==0)
                                            return power((((a%mod)*(a%mod))%mod),b/2)%mod;
                                        else return ((a%mod)*(power((((a%mod)*(a%mod))%mod),b/2)%mod))%mod;
                                    }
                                    
                                  
                                
                                 int dp[80][80][20001];
                                int main()
                                {
                                    IOS
                                 

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

                                for(int i=0;i<n;++i)
                                {
                                    for(int j=0;j<m;++j)
                                        cin>>b[i][j];
                                }
                                int base=10000;
                                memset(dp,0,sizeof(dp));
                                dp[0][0][a[0][0]-b[0][0]+base]++;
                                dp[0][0][-a[0][0]+b[0][0]+base]++;
                                for(int i=0;i<n;++i)
                                {
                                    for(int j=0;j<m;++j)
                                    {
                                        if(i!=0)
                                        {
                                            for(int k=20000;k>=0;--k)
                                            {
                                                if(dp[i-1][j][k])
                                                    dp[i][j][a[i][j]-b[i][j]+k]++;
                                                if(dp[i-1][j][k])
                                                    dp[i][j][-a[i][j]+b[i][j]+k]++;
                                            }
                                        }
                                        if(j!=0)
                                        {
                                            for(int k=20000;k>=0;--k)
                                            {
                                                if(dp[i][j-1][k])
                                                    dp[i][j][a[i][j]-b[i][j]+k]++;
                                                if(dp[i][j-1][k])
                                                    dp[i][j][-a[i][j]+b[i][j]+k]++;
                                            }
                                        }
                                        

                                    }
                                }
                                int ans=1e9;
                                for(int k=20000;k>=0;--k)
                                {
                                    if(dp[n-1][m-1][k])
                                    ans=min(ans,abs(k-base));
                                }

                                cout<<ans;
                                


                                }                       


Measured memory_bytes: 500352