stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
/**

 * Created by hiramekun at 20:44 on 2019-12-08.

 */

#include <bits/stdc++.h>



using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)



int main() {

    ll h, w;

    cin >> h >> w;

    vector<vector<ll>> a(h, vector<ll>(w)), b(h, vector<ll>(w));

//    int a[h][w], b[h][w];

    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) cin >> b[i][j];



    ll max_diff = 2 * 80 * 160;

    vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(max_diff)));

//    bool dp[h][w][max_diff];

    rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;



    ll mid = max_diff / 2;

    dp[0][0][b[0][0] - a[0][0] + mid] = true;

    dp[0][0][-b[0][0] + a[0][0] + mid] = true;



    rep(i, h) {

        rep(j, w) {

            rep(l, max_diff) {

                if (!dp[i][j][l]) continue;

                if (i + 1 <= h - 1) {

                    dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] = true;

                    dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] = true;

                }

                if (j + 1 <= w - 1) {

                    dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] = true;

                    dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] = true;

                }

            }

        }

    }

    ll ans = 1e18;

    rep(i, max_diff) {

        if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid));

    }

    cout << ans << '\n';

}

// Faster C++ solution for the same problem (measured speedup: 1.107391398828471x):
/**

 * Created by hiramekun at 20:44 on 2019-12-08.

 */

#include <bits/stdc++.h>



using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)



int main() {

    ll h, w;

    cin >> h >> w;

    vector<bool> v;

    v[0];

    vector<int> v2;

    v2[0];

    int a[h][w], b[h][w];

    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) cin >> b[i][j];



    ll max_diff = 2 * 80 * 160;

    bool dp[h][w][max_diff];

    rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;



    ll mid = max_diff / 2;

    dp[0][0][b[0][0] - a[0][0] + mid] = true;

    dp[0][0][-b[0][0] + a[0][0] + mid] = true;



    rep(i, h) {

        rep(j, w) {

            rep(l, max_diff) {

                if (i + 1 <= h - 1) {

                    dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] |= dp[i][j][l];

                    dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] |= dp[i][j][l];

                }

                if (j + 1 <= w - 1) {

                    dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] |= dp[i][j][l];

                    dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] |= dp[i][j][l];

                }

            }

        }

    }

    ll ans = 1e18;

    rep(i, max_diff) {

        if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid));

    }

    cout << ans << '\n';

}
// Slower C++ solution:
/**

 * Created by hiramekun at 20:44 on 2019-12-08.

 */

#include <bits/stdc++.h>



using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)



int main() {

    ll h, w;

    cin >> h >> w;

//    vector<vector<ll>> a(h, vector<ll>(w)), b(h, vector<ll>(w));

    int a[h][w], b[h][w];

    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) cin >> b[i][j];



    ll max_diff = 2 * 80 * 160;

    vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(max_diff)));

//    bool dp[h][w][max_diff];

    rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;



    ll mid = max_diff / 2;

    dp[0][0][b[0][0] - a[0][0] + mid] = true;

    dp[0][0][-b[0][0] + a[0][0] + mid] = true;



    rep(i, h) {

        rep(j, w) {

            rep(l, max_diff) {

                if (!dp[i][j][l]) continue;

                if (i + 1 <= h - 1) {

                    dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] = true;

                    dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] = true;

                }

                if (j + 1 <= w - 1) {

                    dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] = true;

                    dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] = true;

                }

            }

        }

    }

    ll ans = 1e18;

    rep(i, max_diff) {

        if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid));

    }

    cout << ans << '\n';

}

// Faster C++ solution for the same problem (measured speedup: 1.1027756318907351x):
/**

 * Created by hiramekun at 20:44 on 2019-12-08.

 */

#include <bits/stdc++.h>



using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)



int main() {

    ll h, w;

    cin >> h >> w;

    vector<bool> v;

    v[0];

    vector<int> v2;

    v2[0];

    int a[h][w], b[h][w];

    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) cin >> b[i][j];



    ll max_diff = 2 * 80 * 160;

    bool dp[h][w][max_diff];

    rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;



    ll mid = max_diff / 2;

    dp[0][0][b[0][0] - a[0][0] + mid] = true;

    dp[0][0][-b[0][0] + a[0][0] + mid] = true;



    rep(i, h) {

        rep(j, w) {

            rep(l, max_diff) {

                if (i + 1 <= h - 1) {

                    dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] |= dp[i][j][l];

                    dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] |= dp[i][j][l];

                }

                if (j + 1 <= w - 1) {

                    dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] |= dp[i][j][l];

                    dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] |= dp[i][j][l];

                }

            }

        }

    }

    ll ans = 1e18;

    rep(i, max_diff) {

        if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid));

    }

    cout << ans << '\n';

}
// Slower C++ solution:
/**

 * Created by hiramekun at 20:44 on 2019-12-08.

 */

#include <bits/stdc++.h>



using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)



int main() {

    ll h, w;

    cin >> h >> w;

    vector<vector<ll>> a(h, vector<ll>(w)), b(h, vector<ll>(w));

//    int a[h][w], b[h][w];

    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) cin >> b[i][j];



    ll max_diff = 2 * 80 * 160;

    vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(max_diff)));

//    bool dp[h][w][max_diff];

    rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;



    ll mid = max_diff / 2;

    dp[0][0][b[0][0] - a[0][0] + mid] = true;

    dp[0][0][-b[0][0] + a[0][0] + mid] = true;



    rep(i, h) {

        rep(j, w) {

            rep(l, max_diff) {

                if (!dp[i][j][l]) continue;

                if (i + 1 <= h - 1) {

                    dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] = true;

                    dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] = true;

                }

                if (j + 1 <= w - 1) {

                    dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] = true;

                    dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] = true;

                }

            }

        }

    }

    ll ans = 1e18;

    rep(i, max_diff) {

        if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid));

    }

    cout << ans << '\n';

}

// Faster C++ solution for the same problem (measured speedup: 1.1659746208435429x):
/**

 * Created by hiramekun at 20:44 on 2019-12-08.

 */

#include <bits/stdc++.h>



using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)



int main() {

    ll h, w;

    cin >> h >> w;

//    vector<vector<ll>> a(h, vector<ll>(w)), b(h, vector<ll>(w));

    int a[h][w], b[h][w];

    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) cin >> b[i][j];



    ll max_diff = 2 * 80 * 160;

//    vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(max_diff)));

    bool dp[h][w][max_diff];

    rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;



    ll mid = max_diff / 2;

    dp[0][0][b[0][0] - a[0][0] + mid] = true;

    dp[0][0][-b[0][0] + a[0][0] + mid] = true;



    rep(i, h) {

        rep(j, w) {

            rep(l, max_diff) {

                if (!dp[i][j][l]) continue;

                if (i + 1 <= h - 1) {

                    assert(l - b[i + 1][j] + a[i + 1][j] >= 0);

                    assert(l + b[i + 1][j] - a[i + 1][j] >= 0);

                    dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] = true;

                    dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] = true;

                }

                if (j + 1 <= w - 1) {

                    dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] = true;

                    dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] = true;

                }

            }

        }

    }

    ll ans = 1e18;

    rep(i, max_diff) {

        if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid));

    }

    cout << ans << '\n';

}
// Slower C++ solution:
/**

 * Created by hiramekun at 20:44 on 2019-12-08.

 */

#include <bits/stdc++.h>



using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)



int main() {

    ll h, w;

    cin >> h >> w;

//    vector<vector<ll>> a(h, vector<ll>(w)), b(h, vector<ll>(w));

    int a[h][w], b[h][w];

    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) cin >> b[i][j];



    ll max_diff = 2 * 80 * 160;

    vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(max_diff)));

//    bool dp[h][w][max_diff];

    rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;



    ll mid = max_diff / 2;

    dp[0][0][b[0][0] - a[0][0] + mid] = true;

    dp[0][0][-b[0][0] + a[0][0] + mid] = true;



    rep(i, h) {

        rep(j, w) {

            rep(l, max_diff) {

                if (!dp[i][j][l]) continue;

                if (i + 1 <= h - 1) {

                    dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] = true;

                    dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] = true;

                }

                if (j + 1 <= w - 1) {

                    dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] = true;

                    dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] = true;

                }

            }

        }

    }

    ll ans = 1e18;

    rep(i, max_diff) {

        if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid));

    }

    cout << ans << '\n';

}

// Faster C++ solution for the same problem (measured speedup: 1.1611146705939541x):
/**

 * Created by hiramekun at 20:44 on 2019-12-08.

 */

#include <bits/stdc++.h>



using namespace std;

using ll = long long;

#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)



int main() {

    ll h, w;

    cin >> h >> w;

//    vector<vector<ll>> a(h, vector<ll>(w)), b(h, vector<ll>(w));

    int a[h][w], b[h][w];

    rep(i, h) rep(j, w) cin >> a[i][j];

    rep(i, h) rep(j, w) cin >> b[i][j];



    ll max_diff = 2 * 80 * 160;

//    vector<vector<vector<bool>>> dp(h, vector<vector<bool>>(w, vector<bool>(max_diff)));

    bool dp[h][w][max_diff];

    rep(i, h) rep(j, w) rep(l, max_diff) dp[i][j][l] = false;



    ll mid = max_diff / 2;

    dp[0][0][b[0][0] - a[0][0] + mid] = true;

    dp[0][0][-b[0][0] + a[0][0] + mid] = true;



    rep(i, h) {

        rep(j, w) {

            rep(l, max_diff) {

                if (!dp[i][j][l]) continue;

                if (i + 1 <= h - 1) {

                    assert(l - b[i + 1][j] + a[i + 1][j] >= 0);

                    assert(l + b[i + 1][j] - a[i + 1][j] >= 0);

                    dp[i + 1][j][l + b[i + 1][j] - a[i + 1][j]] = true;

                    dp[i + 1][j][l - b[i + 1][j] + a[i + 1][j]] = true;

                }

                if (j + 1 <= w - 1) {

                    dp[i][j + 1][l + b[i][j + 1] - a[i][j + 1]] = true;

                    dp[i][j + 1][l - b[i][j + 1] + a[i][j + 1]] = true;

                }

            }

        }

    }

    ll ans = 1e18;

    rep(i, max_diff) {

        if (dp[h - 1][w - 1][i]) ans = min(ans, abs(i - mid));

    }

    cout << ans << '\n';

}
// Slower C++ solution:
#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++)

        copy_n(istream_iterator<int>(cin), w, A[i].begin());



    for (int i = 0; i < h; i++)

        copy_n(istream_iterator<int>(cin), w, B[i].begin());



    const int sup = 1e5;

    vector<vector<bitset<sup>>> M(h + 1, vector<bitset<sup>>(w + 1));



    M[0][0][sup / 2] = true;

    for (int i = 0; i < h; i++){

        for (int j = 0; j < w; j++){

            int c = abs(A[i][j] - B[i][j]);

            M[i + 1][j] |= M[i][j] << c;

            M[i + 1][j] |= M[i][j] >> c;

            M[i][j + 1] |= M[i][j] << c;

            M[i][j + 1] |= M[i][j] >> c;

        }

    }



    int ans = 1e9;

    for (int k = 0; k < sup; k++){

        if (M[h - 1][w][k])

            ans = min(ans, abs(k - sup / 2));

    }



    cout << ans << endl;

}

// Faster C++ solution for the same problem (measured speedup: 1.6423432572723562x):
#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++)

        copy_n(istream_iterator<int>(cin), w, A[i].begin());



    for (int i = 0; i < h; i++)

        copy_n(istream_iterator<int>(cin), w, B[i].begin());



    const int sup = 160 * 160 + 1;

    vector<vector<bitset<sup>>> M(h + 1, vector<bitset<sup>>(w + 1));



    M[0][0][sup / 2] = true;

    for (int i = 0; i < h; i++){

        for (int j = 0; j < w; j++){

            int c = abs(A[i][j] - B[i][j]);

            M[i + 1][j] |= M[i][j] << c;

            M[i + 1][j] |= M[i][j] >> c;

            M[i][j + 1] |= M[i][j] << c;

            M[i][j + 1] |= M[i][j] >> c;

        }

    }



    int ans = 1e9;

    for (int k = 0; k < sup; k++){

        if (M[h - 1][w][k])

            ans = min(ans, abs(k - sup / 2));

    }



    cout << ans << endl;

}
// Slower C++ solution:
#include "bits/stdc++.h"

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;



using ll = long long;

const ll MOD = 1e9 + 7;

const int INF = 1e9;



int main() {

    int H, W;

    cin >> H >> W;

    vector<vector<int>> A(H, vector<int>(W)), B(H, vector<int>(W));

    rep(i, H) rep(j, W) cin >> A[i][j];

    rep(i, H) rep(j, W) cin >> B[i][j];

    vector<vector<vector<int>>> s(H + 1, vector<vector<int>>(W + 1, vector<int>(12800, 0)));

    s[1][1][abs(A[0][0] - B[0][0])] = 1;

    rep(i, H) {

        rep(j, W) {

            //cout << i << j << endl;

            if (i == 0 && j == 0) continue;

            rep(k, 12800) {

                if (s[i][j + 1][k] == 0) continue;

                s[i + 1][j + 1][k + abs(A[i][j] - B[i][j])] = 1;

                s[i + 1][j + 1][abs(k - abs(A[i][j] - B[i][j]))] = 1;

            }

            rep(k, 12800) {

                if (s[i + 1][j][k] == 0) continue;

                s[i + 1][j + 1][k + abs(A[i][j] - B[i][j])] = 1;

                s[i + 1][j + 1][abs(k - abs(A[i][j] - B[i][j]))] = 1;

            }

            //cout << "30" << endl;

        }

    }

    //cout << "end" << endl;

    rep(i, 12800) {

        if (s[H][W][i] == 1) {

            cout << i << endl;

            return 0;

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 2.0987361634546304x):
#include "bits/stdc++.h"

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;



using ll = long long;



int main() {

    int H, W;

    cin >> H >> W;

    vector<vector<int>> A(H, vector<int>(W)), B(H, vector<int>(W));

    rep(i, H) {

        rep(j, W) {

            cin >> A[i][j];

        }

    }

    rep(i, H) {

        rep(j, W) {

            cin >> B[i][j];

        }

    }

    vector<vector<vector<bool>>> s(H + 1, vector<vector<bool>>(W + 1, vector<bool> (12800, false)));

    s[0][1][0] = true;

    s[1][0][0] = true;

    rep(i, H) {

        rep(j, W) {

            rep(k, 12800) {

                if (s[i][j + 1][k] || s[i + 1][j][k]) {

                    s[i + 1][j + 1][abs(k + A[i][j] - B[i][j])] = true;

                    s[i + 1][j + 1][abs(k - A[i][j] + B[i][j])] = true;

                }

            }

        }

    }

    rep(i, 12800) {

        if (s[H][W][i]) {

            cout << i << "\n";

            return 0;

        }

    }

    /*

    rep(i, H) {

        cout << "i=" << i << "\n";

        rep(j, W) {

            cout << "j=" << j << " s=";

            for (auto k : s[i + 1][j + 1]) {

                cout << k << " ";

            }

            cout << "\n";

        }

    }

    */

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=85,low=80*80*3;

int a[N][N],b[N][N];

bitset<N*N*10>dp[N][N];

int mx[N][N];

int n,m;

int main()

{

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&a[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&b[i][j]);

    dp[1][1].set(a[1][1]-b[1][1]+low,1);

    dp[1][1].set(b[1][1]-a[1][1]+low,1);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

    {

        if(i==1&&j==1) continue;

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

        dp[i][j]=(dp[i-1][j]<<x)|(dp[i-1][j]>>x)|(dp[i][j-1]<<x)|(dp[i][j-1]>>x);

    }

    int ans=1e9;

    for(int i=0;i<=low*3;i++)

        if(dp[n][m][i])

        {

            ans=min(ans,abs(i-low));

        }

    printf("%d\n",ans);

}


// Faster C++ solution for the same problem (measured speedup: 1.213348336479524x):
#include<bits/stdc++.h>

using namespace std;

const int N=85,low=80*80*3;

int a[N][N],b[N][N];

bitset<N*N*8>dp[N][N];

int mx[N][N];

int n,m;

int main()

{

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&a[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&b[i][j]);

    dp[1][1].set(a[1][1]-b[1][1]+low,1);

    dp[1][1].set(b[1][1]-a[1][1]+low,1);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

    {

        if(i==1&&j==1) continue;

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

        dp[i][j]=(dp[i-1][j]<<x)|(dp[i-1][j]>>x)|(dp[i][j-1]<<x)|(dp[i][j-1]>>x);

    }

    int ans=1e9;

    for(int i=0;i<=low*3;i++)

        if(dp[n][m][i])

        {

            ans=min(ans,abs(i-low));

        }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=85,low=80*80*3;

int a[N][N],b[N][N];

bitset<N*N*10>dp[N][N];

int mx[N][N];

int n,m;

int main()

{

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&a[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&b[i][j]);

    dp[1][1].set(a[1][1]-b[1][1]+low,1);

    dp[1][1].set(b[1][1]-a[1][1]+low,1);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

    {

        if(i==1&&j==1) continue;

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

        dp[i][j]=(dp[i-1][j]<<x)|(dp[i-1][j]>>x)|(dp[i][j-1]<<x)|(dp[i][j-1]>>x);

    }

    int ans=1e9;

    for(int i=0;i<=low*3;i++)

        if(dp[n][m][i])

        {

            ans=min(ans,abs(i-low));

        }

    printf("%d\n",ans);

}


// Faster C++ solution for the same problem (measured speedup: 1.3214893763107523x):
#include<bits/stdc++.h>

using namespace std;

const int N=85,low=85*85*2;

int a[N][N],b[N][N];

bitset<N*N*4>dp[N][N];

int mx[N][N];

int n,m;

int main()

{

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&a[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&b[i][j]);

    dp[1][1].set(a[1][1]-b[1][1]+low,1);

    dp[1][1].set(b[1][1]-a[1][1]+low,1);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

    {

        if(i==1&&j==1) continue;

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

        dp[i][j]=(dp[i-1][j]<<x)|(dp[i-1][j]>>x)|(dp[i][j-1]<<x)|(dp[i][j-1]>>x);

    }

    int ans=1e9;

    for(int i=0;i<=low*2;i++)

        if(dp[n][m][i])

        {

            ans=min(ans,abs(i-low));

        }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=85,low=80*80*3;

int a[N][N],b[N][N];

bitset<N*N*10>dp[N][N];

int mx[N][N];

int n,m;

int main()

{

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&a[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&b[i][j]);

    dp[1][1].set(a[1][1]-b[1][1]+low,1);

    dp[1][1].set(b[1][1]-a[1][1]+low,1);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

    {

        if(i==1&&j==1) continue;

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

        dp[i][j]=(dp[i-1][j]<<x)|(dp[i-1][j]>>x)|(dp[i][j-1]<<x)|(dp[i][j-1]>>x);

    }

    int ans=1e9;

    for(int i=0;i<=low*3;i++)

        if(dp[n][m][i])

        {

            ans=min(ans,abs(i-low));

        }

    printf("%d\n",ans);

}


// Faster C++ solution for the same problem (measured speedup: 1.3856350248013434x):
#include<bits/stdc++.h>

using namespace std;

const int N=85,low=81*81;

int a[N][N],b[N][N];

bitset<N*N*2>dp[N][N];

int mx[N][N];

int n,m;

int main()

{

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&a[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&b[i][j]);

    dp[1][1].set(a[1][1]-b[1][1]+low,1);

    dp[1][1].set(b[1][1]-a[1][1]+low,1);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

    {

        if(i==1&&j==1) continue;

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

        dp[i][j]=(dp[i-1][j]<<x)|(dp[i-1][j]>>x)|(dp[i][j-1]<<x)|(dp[i][j-1]>>x);

    }

    int ans=1e9;

    for(int i=0;i<=low*2;i++)

        if(dp[n][m][i])

        {

            ans=min(ans,abs(i-low));

        }

    printf("%d\n",ans);

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=85,low=80*80*3;

int a[N][N],b[N][N];

bitset<N*N*8>dp[N][N];

int mx[N][N];

int n,m;

int main()

{

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&a[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&b[i][j]);

    dp[1][1].set(a[1][1]-b[1][1]+low,1);

    dp[1][1].set(b[1][1]-a[1][1]+low,1);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

    {

        if(i==1&&j==1) continue;

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

        dp[i][j]=(dp[i-1][j]<<x)|(dp[i-1][j]>>x)|(dp[i][j-1]<<x)|(dp[i][j-1]>>x);

    }

    int ans=1e9;

    for(int i=0;i<=low*3;i++)

        if(dp[n][m][i])

        {

            ans=min(ans,abs(i-low));

        }

    printf("%d\n",ans);

}


// Faster C++ solution for the same problem (measured speedup: 1.141992767568876x):
#include<bits/stdc++.h>

using namespace std;

const int N=85,low=81*81;

int a[N][N],b[N][N];

bitset<N*N*2>dp[N][N];

int mx[N][N];

int n,m;

int main()

{

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&a[i][j]);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

        scanf("%d",&b[i][j]);

    dp[1][1].set(a[1][1]-b[1][1]+low,1);

    dp[1][1].set(b[1][1]-a[1][1]+low,1);

    for(int i=1;i<=n;i++)

        for(int j=1;j<=m;j++)

    {

        if(i==1&&j==1) continue;

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

        dp[i][j]=(dp[i-1][j]<<x)|(dp[i-1][j]>>x)|(dp[i][j-1]<<x)|(dp[i][j-1]>>x);

    }

    int ans=1e9;

    for(int i=0;i<=low*2;i++)

        if(dp[n][m][i])

        {

            ans=min(ans,abs(i-low));

        }

    printf("%d\n",ans);

}