stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<iostream>

#include <cstring>

#include<stdio.h>

#include <cctype>



using namespace std;



char s1[11],s2[11],s3[11];

int a,b,c,j,k,loop,i,temp = 0;



int main()

{

    cin>>s1;

    cin>>s2;

    cin>>s3;



    a = strlen(s1);

    b = strlen(s2);

    c = strlen(s3);



    if(a>0 && a<=10 && b>0 && b<=10 && c>0 &&c <=10)

    {

            if(s1[a-1]==s2[0] && s2[b-1]==s3[0])

            {

                printf("YES\n");

            }

            else

            {

                printf("NO\n");

            }

    }

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

 

int main(){

  char a[100];

  scanf("%[^\n]", a);

	int ctr = 0;

	

  for(int i= 0; a[i] != '\0'; i++){

    if(a[i] == ' '){

		if(a[i - 1] == a[i + 1]){

			ctr++;

		}

    }

  }

  if(ctr == 2){

  	printf("YES\n");

  }

  else{

  	printf("NO\n");

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>



using namespace std;



int main()

{

    char a[11],b[11],c[11];

    char n,m,p,q;

    cin>>a>>b>>c;





    n=a[strlen(a)-1];

    m=b[0];

    p=b[strlen(b)-1];

    q=c[0];



    if(n==m && p==q)

    {

        cout<<"YES"<<endl;

    }

    else

    {



        cout<<"NO"<<endl;



    }







    return 0;





}

// Optimized solution to the same problem (faster):
#include<stdio.h>

 

int main(){

  char a[100];

  scanf("%[^\n]", a);

	int ctr = 0;

	

  for(int i= 0; a[i] != '\0'; i++){

    if(a[i] == ' '){

		if(a[i - 1] == a[i + 1]){

			ctr++;

		}

    }

  }

  if(ctr == 2){

  	printf("YES\n");

  }

  else{

  	printf("NO\n");

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main()

{

    string a,b,c;

    cin>>a>>b>>c;



    if(a[a.length()-1] == b[0] && b[b.length()-1] == c[0])

        cout<<"YES";

    else

        cout<<"NO";

    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

 

int main(){

  char a[100];

  scanf("%[^\n]", a);

	int ctr = 0;

	

  for(int i= 0; a[i] != '\0'; i++){

    if(a[i] == ' '){

		if(a[i - 1] == a[i + 1]){

			ctr++;

		}

    }

  }

  if(ctr == 2){

  	printf("YES\n");

  }

  else{

  	printf("NO\n");

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#include <iostream>

using namespace std;

int main()

{ 

      string c,d,e;

       

      cin>>c>>d>>e;

 

      

      

        int n = c.length(); 

    char g[n + 1]; 

    strcpy(g, c.c_str()); 

    

     int m = d.length(); 

    char h[m + 1]; 

    strcpy(h, d.c_str()); 

    

    

     int o = e.length(); 

    char k[o + 1]; 

    strcpy(k, e.c_str()); 



     if((g[n-1]==h[0])&&(h[m-1]==k[0]))

        cout<<"YES";



     else  cout<<"NO";

     

   return 0;



}

// Optimized solution to the same problem (faster):
#include<stdio.h>

 

int main(){

  char a[100];

  scanf("%[^\n]", a);

	int ctr = 0;

	

  for(int i= 0; a[i] != '\0'; i++){

    if(a[i] == ' '){

		if(a[i - 1] == a[i + 1]){

			ctr++;

		}

    }

  }

  if(ctr == 2){

  	printf("YES\n");

  }

  else{

  	printf("NO\n");

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):


#include<bits/stdc++.h>

#define lln long long int

#define llu unsigned lln

#define sc(n) scanf("%d",&n);

#define scl(n) scanf("%lld",&n);

#define scd(n) scanf("%lf",&n);

#define pf(res) printf("%d\n",res);

#define pfl(res) printf("%lld\n",res);

#define pfd(res) printf("%lf\n",res);

#define maxii 100005

using namespace std;

typedef pair<int,int> pii;

typedef pair<lln,lln> pll;

typedef vector<int> vi;

typedef vector<lln> vl;

typedef vector<pii> vii;

typedef vector<pll> vll;





int arr[maxii];

int arr2[maxii];

int gcd(int a,int b)

{

    if(b==0){

      return a;

     }

    return gcd(b,a%b);

}

int lcm(int a,int b)

{

    int res=(a*b)/gcd(a,b);

    return res;

}

int po(int a,int b)

{

    int res=1;

    while(b)

    {

        if(b%2!=0)

        {

            res*=a;

            b--;

        }

        else

        {

            a*=a;

            b/=2;

        }

    }

    return res;

}



lln modMul(lln  a, lln b,lln mod){

    lln ans = 0;

    a = a % mod;

    while (b > 0){

        if ( b % 2 )ans = (ans%mod+ a%mod) % mod;

        a = (a%mod * 2%mod) % mod;

        b /= 2;

    }

    return ans % mod;

}

lln powerMod(lln a,lln b,lln mod){

	if(b==0)return 1LL%mod;

	lln x=powerMod( a,b/2,mod );

	x = ( x%mod * x%mod ) % mod;

	if( b%2==1 )x = ( x%mod * a%mod ) % mod;

	return x%mod;

}



int main()

{

   string a,b,c;

   cin>>a>>b>>c;

   int len=a.size();

   int len2=b.size();

   int len3=c.size();

   if(a[len-1]==b[0] && b[len2-1]==c[0] )cout<<"YES\n";

   else cout<<"NO";



}










// Optimized solution to the same problem (faster):
#include<stdio.h>

 

int main(){

  char a[100];

  scanf("%[^\n]", a);

	int ctr = 0;

	

  for(int i= 0; a[i] != '\0'; i++){

    if(a[i] == ' '){

		if(a[i - 1] == a[i + 1]){

			ctr++;

		}

    }

  }

  if(ctr == 2){

  	printf("YES\n");

  }

  else{

  	printf("NO\n");

  }

  return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

int i,j,k,n,m,x,y,z,flag;

struct yea{

	int x,y;

	char num[20];

} a[10000];

char ch;

bool cmp(struct yea a,struct yea b)

{

	if (a.x<b.x) return true;

 	return false;

}





int main()

{

	

	while(1)

	{

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

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

		scanf("%c",&ch);

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

		{

			scanf("%s%d%d",&a[i].num,&x,&y);

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

			a[i].y=y;

		}

		sort(a,a+n,cmp);

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

		{

			scanf("%d",&x);

			flag=-1;

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

			{

				if (a[j].x<=x && x<= a[j].y)

				{

					flag=j;

					break;

				}

				if (a[j].x>x) break;

			}

			if (flag==-1) printf("Unknown\n");

			else {

				printf("%s %d\n",a[flag].num,x-a[flag].x+1);

			}

		}

	}

	return 0;

	

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;



}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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");

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
/**

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

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/**

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

}

// Optimized solution to the same problem (faster):
/**

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

}

// Fastest known solution for this problem: