stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<iostream>

#include<memory.h>

#include<stdio.h>

using namespace std;

char name[1000][1000];

int n,m;



int main(){

    while(cin>>n>>m && (n+m)!=0){

        long long japan[10000];

        long long japan2[10000];

        long long a,b;



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

        scanf("%s",name[i]);

        cin>>a>>b;

        japan[i] = b;

        japan2[i] = b-a;

        }





        long long query;

        for(int i=1;i<=m;i++){

            cin>>query;

            int temp = 0;

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

                if(query <= japan[i] && query >japan2[i]){

                    cout<<name[i]<<" "<<( query - japan2[i]) <<endl;

                    temp = 1;

                    break;

                }

            }

            if(temp == 0){cout<<"Unknown"<<endl;}

        }







    }





    return 0;



}

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

#include <algorithm>

#define inf 1000000111

#define N 1005



using namespace std;



struct Calender

{

     int l, r;

     char st[22];

}cale[N];



int cmp(Calender x, Calender y)

{

     return x.l < y.l;

}



int main()

{

     int n, q, x;

     while(scanf("%d%d", &n, &q))

     {

          if(n == 0 && q == 0)

          {

               break ;

          }

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

          {

               scanf("%s%d%d", cale[i].st, &x, &cale[i].r);

               cale[i].l = cale[i].r - x;

          }

          sort(cale, cale + n, cmp);

          cale[n].l = inf;



          while(q--)

          {

               scanf("%d", &x);

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

               {

                    if(cale[i].l >= x)

                    {

                         if(i > 0 && cale[i - 1].l < x && cale[i - 1].r >= x)

                         {

                              printf("%s %d\n", cale[i - 1].st, x - cale[i - 1].l);

                         }

                         else

                         {

                              printf("Unknown\n");

                         }

                         break ;

                    }

               }

          }

     }

     return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstring>

#include <cstdio>



using namespace std;

const int Maxn = 1010;

struct Era

{

	char name[20];

	int n;

	int year;

}era[Maxn];

int n, m;



void solve(int year)

{

	int ret = -1;

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

	{

		if (era[i].year < year) continue;

		if (era[i].year - era[i].n < year)

		{

			if (ret == -1) ret = i;

			else ret = -2;

		

		}

	}

	if (ret < 0) 

		puts("Unknown");

	else 	printf("%s %d\n", era[ret].name, era[ret].n + year - era[ret].year);

			

	

}

int main()

{

	while (scanf("%d%d", &n, &m) != EOF)

	{

		if (n == 0) break;

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

			scanf("%s%d%d", era[i].name, &era[i].n, &era[i].year);

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

		{

			int y;

			scanf("%d", &y);

			solve(y);

		}

		

	}

	return 0;

}

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

#include <algorithm>

#define inf 1000000111

#define N 1005



using namespace std;



struct Calender

{

     int l, r;

     char st[22];

}cale[N];



int cmp(Calender x, Calender y)

{

     return x.l < y.l;

}



int main()

{

     int n, q, x;

     while(scanf("%d%d", &n, &q))

     {

          if(n == 0 && q == 0)

          {

               break ;

          }

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

          {

               scanf("%s%d%d", cale[i].st, &x, &cale[i].r);

               cale[i].l = cale[i].r - x;

          }

          sort(cale, cale + n, cmp);

          cale[n].l = inf;



          while(q--)

          {

               scanf("%d", &x);

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

               {

                    if(cale[i].l >= x)

                    {

                         if(i > 0 && cale[i - 1].l < x && cale[i - 1].r >= x)

                         {

                              printf("%s %d\n", cale[i - 1].st, x - cale[i - 1].l);

                         }

                         else

                         {

                              printf("Unknown\n");

                         }

                         break ;

                    }

               }

          }

     }

     return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>

#include <vector>

#include <cmath>

#include <cstdlib>

#include <algorithm>

#include <map>

#define inf 1e9

#define eps 1e-8

#define MAXT 500000

#define MAXN 1010

using namespace std;



int n,q;

struct Node

{

    char nam[20];

    int st,ed;

    int id;

}a[MAXN];

int main()

{

    while(scanf("%d%d",&n,&q))

    {

        if(n==0&&q==0)break;

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

        {

            scanf("%s",a[i].nam);

            int x,y;

            scanf("%d%d",&x,&y);

            a[i].st=y-x+1;a[i].ed=y;

        }

        while(q--)

        {

            int tp;

            scanf("%d",&tp);

            int flag=0;

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

            {

                if(tp>=a[i].st&&tp<=a[i].ed)

                {

                    printf("%s %d\n",a[i].nam,tp-a[i].st+1);

                    flag=1;

                    break;

                }

            }

            if(!flag)printf("Unknown\n");

        }

    }

    return 0;

}

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

#include <algorithm>

#define inf 1000000111

#define N 1005



using namespace std;



struct Calender

{

     int l, r;

     char st[22];

}cale[N];



int cmp(Calender x, Calender y)

{

     return x.l < y.l;

}



int main()

{

     int n, q, x;

     while(scanf("%d%d", &n, &q))

     {

          if(n == 0 && q == 0)

          {

               break ;

          }

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

          {

               scanf("%s%d%d", cale[i].st, &x, &cale[i].r);

               cale[i].l = cale[i].r - x;

          }

          sort(cale, cale + n, cmp);

          cale[n].l = inf;



          while(q--)

          {

               scanf("%d", &x);

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

               {

                    if(cale[i].l >= x)

                    {

                         if(i > 0 && cale[i - 1].l < x && cale[i - 1].r >= x)

                         {

                              printf("%s %d\n", cale[i - 1].st, x - cale[i - 1].l);

                         }

                         else

                         {

                              printf("Unknown\n");

                         }

                         break ;

                    }

               }

          }

     }

     return 0;

}
// Slower C++ solution:
#include <algorithm>

#include <iostream>

#include <cstring>

#include <cstdlib>

#include <vector>

#include <queue>

#include <cstdio>

#include <cmath>

#include <string>

#include <stack>

#include <cctype>

using namespace std;



#define INF 0x7fffffff

#define eps 1e-12

#define MOD 10000007





struct point

{

    char name[25];

    int a,e;



};

point p[1015];





bool cmp(point a,point b)

{

    return a.a<b.a;

}



int main()

{

    int n,q;

    while(~scanf("%d%d",&n,&q))

    {



        if(n==0&&q==0)

            break;

               int x,y;

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

        {

            cin>>p[i].name;

            cin>>x>>y;

            p[i].a=y-x+1;

            p[i].e=y;



        }

        sort(p,p+n,cmp);

        int ss;

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

        {

            cin>>ss;

            int id=-1;

            bool k=1;

            for(int j=0; j<n; j++)

                if(p[j].a<=ss&&p[j].e>=ss)

                {

                    id=j;

                    break;

                }

            if(id==-1)

                k=0;

            if(!k)

                puts("Unknown");



            else

                cout<<p[id].name<<" "<<ss-p[id].a+1<<endl;

        }



    }



    return 0;

}

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

#include <algorithm>

#define inf 1000000111

#define N 1005



using namespace std;



struct Calender

{

     int l, r;

     char st[22];

}cale[N];



int cmp(Calender x, Calender y)

{

     return x.l < y.l;

}



int main()

{

     int n, q, x;

     while(scanf("%d%d", &n, &q))

     {

          if(n == 0 && q == 0)

          {

               break ;

          }

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

          {

               scanf("%s%d%d", cale[i].st, &x, &cale[i].r);

               cale[i].l = cale[i].r - x;

          }

          sort(cale, cale + n, cmp);

          cale[n].l = inf;



          while(q--)

          {

               scanf("%d", &x);

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

               {

                    if(cale[i].l >= x)

                    {

                         if(i > 0 && cale[i - 1].l < x && cale[i - 1].r >= x)

                         {

                              printf("%s %d\n", cale[i - 1].st, x - cale[i - 1].l);

                         }

                         else

                         {

                              printf("Unknown\n");

                         }

                         break ;

                    }

               }

          }

     }

     return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <vector>

#include <cmath>



using namespace std;



typedef long long i64;



i64 f(i64 b, i64 n) {

    i64 s=0;

    while (n) {

        s += n % b;

        n /= b;

    }

    return s;

}



int main(void) {

    i64 n, s;

    cin >> n >> s;



    if (n == s) {

        cout << n+1 << endl;

        return 0;

    }



    i64 ub=sqrt(n);

    i64 b;

    for (b=2; b<=ub; b++)

        if (f(b, n) == s) {

            cout << b << endl;

            return 0;

        }



    i64 p;

    for (p=ub+1; --p;) {

        b = (n-s)/p + 1;

        if (b < 2) continue;

        if (f(b, n) == s) {

            cout << b << endl;

            return 0;

        }

    }

    cout << "-1\n";

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.040603145804025x):
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <vector>



using namespace std;



using i64=long long;



i64 f(i64 b, i64 n) {

    i64 s=0;

    while (n) {

        s += n % b;

        n /= b;

    }

    return s;

}



int main() {

    i64 n, s;

    scanf("%lld %lld", &n, &s);



    if (n == s)

        return !printf("%lld\n", n+1);



    i64 ub=sqrt(n);

    for (i64 b=2; b<=ub; ++b)

        if (f(b, n) == s)

            return !printf("%lld\n", b);



    for (i64 p=ub+1; --p;) {

        i64 b=(n-s)/p+1;

        if (b < 2) continue;

        if (f(b, n) == s)

            return !printf("%lld\n", b);

    }



    return !printf("-1\n");

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <vector>

#include <cmath>



using namespace std;



typedef long long i64;



i64 f(i64 b, i64 n) {

    i64 s=0;

    while (n) {

        s += n % b;

        n /= b;

    }

    return s;

}



int main(void) {

    i64 n, s;

    cin >> n >> s;



    if (n == s) {

        cout << n+1 << endl;

        return 0;

    }



    i64 ub=sqrt(n);

    i64 b;

    for (b=2; b<=ub; b++)

        if (f(b, n) == s) {

            cout << b << endl;

            return 0;

        }



    i64 p;

    for (p=ub+1; --p;) {

        b = (n-s)/p + 1;

        if (b < 2) continue;

        if (f(b, n) == s) {

            cout << b << endl;

            return 0;

        }

    }

    cout << "-1\n";

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.456316837751898x):
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <vector>



using namespace std;



using i64=long long;



i64 f(i64 b, i64 n) {

    return n<b? n:f(b, n/b)+(n%b);

}



int main() {

    i64 n, s;

    scanf("%lld %lld", &n, &s);



    if (n == s)

        return !printf("%lld\n", n+1);



    i64 ub=sqrt(n);

    for (i64 b=2; b<=ub; ++b)

        if (f(b, n) == s)

            return !printf("%lld\n", b);



    for (i64 p=ub+1; --p;) {

        i64 b=(n-s)/p+1;

        if (b < 2) continue;

        if (f(b, n) == s)

            return !printf("%lld\n", b);

    }



    return !printf("-1\n");

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

 * @author yuzining

 * @date 2020/08/17 16:13

 */



#ifdef local

#include "D:\vscode\acm\local.hpp"

#else

#include<bits/stdc++.h>

using namespace std;

#define debug(...) do { } while(false)

void Main();

int main() {

  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

  Main();

  return 0;

}

#endif



int64_t Function(int64_t b, int64_t n) {

  if (n < b) return n;

  return Function(b, n / b) + n % b;

}



void Main() {

  int64_t n, s; cin >> n >> s;

  if (n == s) {

    cout << (n + 1) << '\n';

    return;

  }

  for (int64_t b = 2; b <= n / b; b++) {

    if (Function(b, n) == s) {

      cout << b << '\n';

      return;

    }

  }

  if (s > n) {

    cout << "-1\n";

    return;

  }

  int64_t ans = LLONG_MAX;

  for (int64_t x = 1; x <= (n - s) / x; x++) {

    if ((n - s) % x == 0) {

      const int64_t b = (n - s) / x + 1;

      if (Function(b, n) == s) {

        ans = min(ans, b);

      }

    }

  }

  if (ans == LLONG_MAX) {

    ans = -1;

  }

  cout << ans << '\n';

}

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

#include <math.h>

typedef long long LL;



LL f(LL x, LL n)

{

	if(n < x) return n;

	return f(x, n / x) + n % x; 

}



int main()

{		

	LL n, s, i, d, k, x, y;

	

	scanf("%lld%lld", &n, &s);

	if(s > n){

		puts("-1");

		return 0;

	}

	if(s == n){

		printf("%lld\n", n + 1);

		return 0;

	}

	for(i = 2; n / i >= i; i++){

		if(f(i, n) == s){

			printf("%lld\n", i);

			return 0;

		}

	}

	d = n - s;

	for(i = sqrt(d); i > 0; i--){

		if(d % i == 0){

			x = d / i + 1, y = s - i;

			if(y >= 0 && y < x && n / x < x){

				printf("%lld\n", x);

				return 0;

			}

		}

	}

	puts("-1");

				

	return 0;

}
// 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.1863756302255741x):
/**

 * 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';

}
// 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.1731851496595007x):
/**

 * 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';

}
// 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.1682951448866898x):
/**

 * 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';

}