Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###


from sys import stdin
import math

a,b = [int(x) for x in stdin.readline().rstrip().split()]
 
def is_prime(n):
    if n == 1: return False
    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False
    return True
 
if a == 1:
    print(1)
    exit()
 
def cf(x1,x2):
    cf=[]
    cf.append(1)
    k = min(x1,x2)
    for i in range(2,k+1):
        if is_prime(i) == True:
            if x1 % i == 0 and x2 % i == 0:
                cf.append(i)
    return len(cf)
 
print(cf(a,b))

### FIXED CODE ###

from sys import stdin
import fractions
import bisect

a,b = [int(x) for x in stdin.readline().rstrip().split()]

def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

ali = set(prime_factorize(a))
bli = set(prime_factorize(b))

ali = list(ali)
bli = list(bli)

point = 0
for i in range(len(ali)):
    if ali[i] in bli:
        point += 1
print(point+1)
### BUGGY CODE ###

n=int(input())
a=list(input().split())
ans=[]
i=1
for A in a:
    if i%2: ans.insert(0,A)
    else: ans.append(A)
    i+=1
if i%2: ans.reverse() 
print(*ans,sep=' ')

### FIXED CODE ###

import queue as que
n=int(input())
a=list(input().split())
q=que.Queue()
s=[]
i=1
if n%2:
    for A in a:
        if i%2: s.append(A)
        else: q.put(A)
        i+=1
else:
    for A in a:
        if i%2: q.put(A)
        else: s.append(A)
        i+=1
for _ in range(len(s)): print(s.pop())
while not q.empty() :print(q.get())
### BUGGY CODE ###

import sys

def max_heapify(A, i):
  l = i * 2
  r = l + 1
  largest = i
  H = len(A) -1
  if l <= H and A[l] > A[i]:
      largest = l
  if r <= H and A[r] > A[largest]:
      largest = r
  if largest != i:
    A[i], A[largest] = A[largest], A[i]
    max_heapify(A, largest)

def build_max_heap(A):
    for i in range(len(A)-1 // 2, 0, -1):
        max_heapify(A, i)
    
def extract(A):
    print(A[1])
    A[1] = A[-1]
    del A[-1]
    max_heapify(A, 1)
    
_input = sys.stdin.readlines()
H = 0
A = [0]
def main():
    for l in _input:
        if l[0] == "e":
            if l[1] == "n":
                #end
                return
            else:
                #print(A)
                extract(A)
        else:
            #insert k
            k = int(l.split()[1])
            A.append(k)
            build_max_heap(A)
        #print(A)

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys

#@profile
def max_heapify(A, i):
    j = i
    largest = i
    H = len(A) -1
    while True:
        l = j * 2
        r = l + 1
        if l <= H and A[l] > A[j]:
            largest = l
        if r <= H and A[r] > A[largest]:
            largest = r
        if largest != j:
            A[j], A[largest] = A[largest], A[j]
            #max_heapify(A, largest)
            j = largest
            
        else:
            break
        

#@profile
def build_max_heap(A):
    i = len(A)-1
    last = A[i]
    parent = i//2
    while  parent > 0 and A[parent] < last:
        A[i] = A[parent]
        i = parent
        parent = i //2
    A[i] = last

#@profile
def extract(A):
    print(A[1])
    A[1] = A[-1]
    del A[-1]
    max_heapify(A, 1)
    
A = [0]

#@profile
def main():
    out = ""
    for l in sys.stdin.readlines():
        if l[0] == "i":
            #insert k
            A.append(int(l[7:]))
            build_max_heap(A)
        #print(A)
        elif l[1] == "n":
            #end
            print(out, end = "")
            return
        else:
            if len(A) <= 1:
                pass
            elif len(A) == 2:
                out += str(A.pop()) + "\n"
                #print(A.pop())
            #extract(A)
            else:
                out += str(A[1]) + "\n"
                A[1] = A.pop()
                max_heapify(A, 1)
                
if __name__ == "__main__":
    main()
### BUGGY CODE ###

n = int(input())
s =input()
s = "0" + s
ans = 0

for i in range(1, n - 1):
    for j in range(i + 1 , n):
        if s[i] == s[j]:
            continue
        elif (s[i] == "R" and s[j] == "G") or (s[i] == "G" and s[j] == "R"):
            ans += s.count("B", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "B":
                    ans -=1
        elif (s[i] == "R" and s[j] == "B") or (s[i] == "B" and s[j] == "R"):
            ans += s.count("G", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "G":
                    ans -=1
        elif (s[i] == "B" and s[j] == "G") or (s[i] == "G" and s[j] == "B"):
            ans += s.count("R", j , n + 1)
            if 2 * j - i <= n:
                if s[2 *j - i] == "R":
                    ans -=1
print(ans)

### FIXED CODE ###

n = int(input())
s = input()
s = "0" + s
ans = s.count("R") * s.count("G") * s.count("B")

for i in range(1,n):
    for j in range(i + 1, n + 1):
        if 2 * j - i <= n:
            if s[i] != s[j] and s[i] != s[2*j -i] and s[j] != s[2*j -i]:
                ans -= 1
print(ans)
### BUGGY CODE ###

def f(x):
    k = int(x)
    return c[k], k


c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = list(map(int, input().split()))
A = list(map(f, input().split()))
A = sorted(A, key=lambda x: x[0] * 10 - x[1])

# 数字を作成するのに必要なマッチ数のリストを作成
b = []
dic = {}
n = 0
for d in A:
    if n != d[0]:
        b.append(d[0])
        n = d[0]
        dic[n] = d[1]

ans = [0] * b[0]
n = b[0]
while True:
    max_ = 0
    for d in b:
        if n - d == 0:
            if dic[d] > max_:
                max_ = dic[d]
        elif n - d > 0 and ans[n - d] > 0:
            t = dic[d] * (10 ** len(str(ans[n - d]))) + ans[n - d]
            max_ = max(max_, t)

    if n == N:
        print(max_)
        break
    ans.append(max_)
    n += 1

### FIXED CODE ###

def f(x):
    k = int(x)
    return c[k], k


c = [0, 2, 5, 5, 4, 5, 6, 3, 7, 6]
N, M = list(map(int, input().split()))
A = list(map(f, input().split()))
A = sorted(A, key=lambda x: x[0] * 10 - x[1])

# 数字を作成するのに必要なマッチ数のリストを作成
b = []
dic = {}
n = 0
for d in A:
    if n != d[0]:
        b.append(d[0])
        n = d[0]
        dic[n] = d[1]

ans = [0] * b[0]
n = b[0]
while True:
    max_ = 0
    for d in b:
        if n - d == 0:
            if dic[d] > max_:
                max_ = dic[d]
        elif n - d > 0 and ans[n - d] > 0:
            t = ans[n - d] * 10 + dic[d]
            max_ = max(max_, t)

    if n == N:
        print(max_)
        break
    ans.append(max_)
    n += 1
### BUGGY CODE ###

import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]
for i in range(m):
    a += bc[i][0] * [bc[i][1]]
a = list(map(lambda x: x*(-1), a))
heapq.heapify(a)
ans = 0
for i in range(n):
    ans += heapq.heappop(a)*(-1)
print(ans)


### FIXED CODE ###

import heapq

n, m = map(int, input().split())
a = list(map(int, input().split()))
heapq.heapify(a)
bc = [list(map(int, input().split())) for _ in range(m)]
bc = sorted(bc, key=lambda x: -x[1])
for b, c in bc:
    for _ in range(b):
        x = heapq.heappop(a)
        if c > x:
            heapq.heappush(a, c)
        else:
            heapq.heappush(a, x)
            break
print(sum(a))
### BUGGY CODE ###

import itertools

N,K=list(map(int,raw_input().split()))
x=[]
y=[]
for i in range(N):
	a1,a2=map(int,raw_input().split())
	x.append(a1)
	y.append(a2)
#print x
#print y


ans=100000000000000000000
for i in range(N):
	for j in range(N):
		for k in range(N):
			for l in range(N):
				if i<=j and k<=l:
					cnt=0
					for m in range(N):
						x_t=x[m]
						y_t=y[m]
						if (x_t>=x[i] and x_t<=x[j]) or (x_t>=x[j] and x_t<=x[i]):
							if (y_t>=y[k] and y_t<=y[l]) or (y_t>=y[l] and y_t<=y[k]):
								cnt+=1
					#print x[i],y[k],x[j],y[l],cnt,abs(x[i]-x[j])*abs(y[k]-y[l])
					#print cnt
					if cnt>=K:
						#print x[i],y[k],x[j],y[l],cnt,abs(x[i]-x[j])*abs(y[k]-y[l])
						ans=min(ans,abs(x[i]-x[j])*abs(y[k]-y[l]))


print ans

### FIXED CODE ###

import itertools

N,K=list(map(int,raw_input().split()))
x=[]
y=[]
pts=[]
for i in range(N):
	a1,a2=map(int,raw_input().split())
	x.append(a1)
	y.append(a2)
	pts.append([a1,a2])

#print "# x,y"
x.sort()
y.sort()
#print x
#print y

#print "# pts"
#print pts
pts.sort(key=lambda x:x[0])
#print pts

ans=10**20


for y1 in y:
	for y2 in y:
		if y1>=y2:
			continue
		for x1 in x:
			cnt=0
			for p in pts:
				if p[0]>=x1 and p[1]>=y1 and p[1]<=y2:
					#print p[0],p[1],x1,y1,y2
					cnt+=1
					if cnt==K:
						x2=p[0]
						#print x1,y1,x2,y2,cnt,abs(x2-x1)*abs(y2-y1)
						ans=min(ans,abs(x2-x1)*abs(y2-y1))
						break

print ans
### BUGGY CODE ###

N,K=map(int,input().split())
H=list(map(int,input().split()))

import numpy as np
dp = np.ones(N+1,dtype=float)*float('inf')
dp[0]=0
dp[1]=0
    

for i in range(N):
    if i!=0:
        for k in range(K):
            if i-k>=1:
                dp[i+1] = min ( dp[i+1] ,dp[i-k] + abs(H[i]-H[i-(k+1)]) )
print(int(dp[N]))
# print(dp)

### FIXED CODE ###

N,K=map(int,input().split())
H=list(map(int,input().split()))

dp = [float('inf')]*(N+1)
dp[0]=0
dp[1]=0
    

for i in range(N):
    if i!=0:
        for k in range(K):
            if i-k>=1:
                dp[i+1] = min ( dp[i+1] ,dp[i-k] + abs(H[i]-H[i-(k+1)]) )
print(int(dp[N]))
# print(dp)
### BUGGY CODE ###

k, s  = map(int, input().split())

ans = 0

for i in range(k+1):
    for j in range(k+1):
        for l in range(k+1):
            if i + j + l == s:
                ans += 1

print(ans)

### FIXED CODE ###

k, s  = map(int, input().split())

ans1 = 0
ans2 = 0
ans3 = 0

for i in range(k+1):
    for j in range(i, k+1):    
        if 0 <= s-i-j <= k and j <= s-i-j:
            if i == j == s-i-j:
                ans1 +=1
            elif i == s-i-j or i == j or j == s-i-j:
                ans2 += 1
            else:
                ans3 += 1
                    

print(ans1+ans2*3+ans3*6)
#print(ans1, ans2, ans3)
### BUGGY CODE ###

n = int(input())
A=list(map(int,input().split()))
ans=[]

for i in range(n):
    D=min(A[j] for j in range(i+1))
    ans.append(D)
    
print(len(set(ans)))


### FIXED CODE ###

n = int(input())
A=list(map(int,input().split()))
cnt=0
tmp=n+1
for i in range(n):
    if A[i]<=tmp:
        tmp=A[i]
        cnt+=1
    else:
        pass
print(cnt)
### BUGGY CODE ###

import statistics

a = int(input())
l = list(map(int, input().split()))

for i in range(a):
  b = l.pop(0)
  print(statistics.median(l))
  l.append(b)


### FIXED CODE ###

import copy

a = int(input())
l = list(map(int, input().split()))
l2 = sorted(copy.deepcopy(l))

#print(l2)

for i in l:
  if i < l2[a//2]:
    print(l2[a//2])
  else:
    print(l2[a//2 - 1])
### BUGGY CODE ###

N = int(input())
A = [int(i) for i in input().split()]
arr = [[False for i in range(N)] for j in range(N)]
cnt = 0
for ind, val in enumerate(A):
  arr[ind][val-1] = True
  if arr[val-1][ind] == True:
    cnt += 1

print(cnt)

### FIXED CODE ###

N = int(input())
A = [int(i) for i in input().split()]
cnt = 0
for i, a in enumerate(A):
  if A[a-1]-1 == i:
    cnt += 1
  
print(cnt//2)
### BUGGY CODE ###

d, g = map(int, input().split())
pc = [list(map(int, input().split())) for _ in range(d)]
n = sum([pc[i][0] for i in range(d)])
prob = []
comp = [0 for _ in range(len(pc))]
for i in range(len(pc)):
    prob += [100*(i+1)] * (pc[i][0])
for i in range(len(pc)):
    comp[i] = pc[i][0]
ans = float('inf')
for i in range(2 ** n):
    total = 0
    comp_bag = [0 for _ in range(len(comp))]
    count = 0
    for j in range(n):
        if (i>>j) & 1:
            total += prob[j]
            comp_bag[prob[j]//100-1] += 1
        if comp_bag[prob[j]//100-1] == comp[prob[j]//100-1] and count == 0:
            total += pc[prob[j]//100-1][1]
            count += 1

    if total >= g:
        ans = min(ans, sum(comp_bag))
    #print(total, comp_bag)

print(ans)

### FIXED CODE ###

import math
d, g = map(int, input().split())
pc = [list(map(int, input().split())) for _ in range(d)]
n = sum([pc[i][0] for i in range(d)])
prob = []
comp = [0 for _ in range(len(pc))]
for i in range(len(pc)):
    prob += [100*(i+1)] * (pc[i][0])
for i in range(len(pc)):
    comp[i] = pc[i][0]
ans = float('inf')

for i in range(2 ** d):
    score = 0
    num = 0
    max_num = 0
    for j in range(d):
        #print("score:", score, "num:", num, "j:", j, "i:", i)
        if (i>>j) & 1:
            score += 100*(j+1)*pc[j][0] + pc[j][1]
            num += pc[j][0]
            
        else:
            max_num = max(max_num, j+1)
            #if max_num == j+1:
                #max_num -= 1 
    if score < g:
        add_n = math.ceil((g-score)/(100*(max_num)))
        #if i == 24:
            #print(num, add_n, score, max_num)
        #print("add_n:", add_n, max_num)
        if add_n < pc[max_num-1][0]:
            num += add_n
            score += add_n * (100*(max_num+1))

    #print(score, num)
    if score >= g:
        ans = min(ans, num)
        #print(num,i)

print(ans)
### BUGGY CODE ###

N = input()
ans = 0
for i in range(len(N)):
    for j in range(i,len(N)):
        if int(N[i:j+1])%2019 == 0:
            ans += 1
print(ans)

### FIXED CODE ###

import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def combinations_count(n, r):
    if n == 1:
        return 0
    else:
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def main():
    S = SI()
    
    ls = []
    len_S = len(S)
    rem = 0

    for s in range(len_S-1,-1,-1):
        rem = (rem+int(S[s])* pow(10, len_S-s-1, 2019))%2019
        ls.append(rem)
    
    import collections

    cls = collections.Counter(ls)
    clsv= list(cls.values())

    ans = 0

    for p in clsv:
        ans += combinations_count(p,2)
    ans += cls[0]
    print(ans)



if __name__ == '__main__':
    main()
### BUGGY CODE ###

s = input()
t = input()
if t in s:
    print(0)
else:
    ans = len(t)
    for i in range(len(t)):
        for j in range(len(s)):
            if t[i] == s[j]:
                if ((len(t) - i) <= (len(s) - j)) and i <= j:
                    k = i + 1
                    l = j + 1
                    cnt1 = 0
                    while k < len(t):
                        if t[k] != s[l]:
                            cnt1 += 1
                        k += 1
                        l += 1
                    k = i - 1
                    l = j - 1
                    cnt2 = 0
                    while k >= 0:
                        if t[k] != s[l]:
                            cnt2 += 1
                        k -= 1
                        l -= 1
                    ans = min(ans, cnt1 + cnt2)  
                else:
                  if ((len(t) - i) > (len(s) - j)):
                    break
               
    print(ans)


### FIXED CODE ###

s = input()
t = input()
if t in s:
    print(0)
else:
    ans = len(t)
    for i in range(len(s) - len(t) + 1):
        cnt = 0
        k = i
        l = 0
        while l < len(t) and k < len(s):
            if s[k] != t[l]:
                cnt += 1 
            k += 1
            l += 1    
        ans = min(ans, cnt)
    print(ans)
### BUGGY CODE ###

n,m = map(int, input().split())
l = []
for _ in range(m):
    a,b = map(int, input().split())
    cl = [x-1 for x in map(int, input().split())]
    b = 0
    for i in cl:
        b |= 1<<i
    l.append([a,b])

inf = 10**9
dp = [inf]*2**n
dp[0] = 0
for x in range(2**n):
    for i in range(m):
        a,b = l[i]
        dp[x|b] = min(dp[x|b], dp[x]+a)

if dp[-1]==inf:
    print(-1)
else:
    print(dp[-1])

### FIXED CODE ###

n,m = map(int, input().split())
l = []
for _ in range(m):
    a,b = map(int, input().split())
    cl = [x-1 for x in map(int, input().split())]
    b = 0
    for i in cl:
        b |= 1<<i
    l.append([a,b])

inf = 10**9
dp = [inf]*2**n
dp[0] = 0
for x in range(2**n):
    for i in range(m):
        a,b = l[i]
        dp[x|b] = min(dp[x|b], dp[x]+a)

if dp[-1]==inf:
    print(-1)
else:
    print(dp[-1])
### BUGGY CODE ###

cond=list(map(int,input().split()))
X=int(cond[1]/1000 - cond[0])
T=True
x=0
while T and x<=int(cond[1]/10000):
    for y in range(int(cond[1]/5000)+1):
        if ((X-9*x)%4!=0):
           continue
        else:
           XX=x
           Y=(cond[1]/1000 - cond[0] - 9*x)/4
           Z=cond[0]-XX-Y
           if Z<0:
               XX=Y=Z=None
               continue
           elif Y<0:
               XX=Y=Z=None
               continue
           else:
               T=False
               break
    x+=1
if len(str(Z))==0 or Z==None:
    print('-1 -1 -1')
else:
    print(int(XX), int(Y), int(Z))

### FIXED CODE ###

cond=list(map(int,input().split()))
X=int(cond[1]/1000 - cond[0])
T=True
x=0
while T and x<=int(cond[1]/10000):
    for y in range(int(cond[1]/5000)+1):
        if ((X-9*x)%4!=0):
           continue
        else:
           XX=x
           Y=(cond[1]/1000 - cond[0] - 9*x)/4
           Z=cond[0]-XX-Y
           if Z<0:
               XX=Y=Z=None
               continue
           elif Y<0:
               XX=Y=Z=None
               continue
           else:
               T=False
               break
    x+=1
if len(str(Z))==0 or Z==None:
    print('-1 -1 -1')
else:
    print(int(XX), int(Y), int(Z))
### BUGGY CODE ###

n, p = map(int, input().split())

for i in range(1, int(p**(1/n) + 10))[::-1]:
    if p%(i**n) == 0:
        print(i)
        exit()

### FIXED CODE ###

n, p = map(int, input().split())
l = []
ans = 1

for i in range(2, int(p**0.5) + 2):
    if p%i == 0:
        cnt = 0
        while p%i == 0:
            p /= i
            cnt += 1
        l.append([i, cnt])

if p != 1:
    l.append([p, 1])

for i, j in l:
    while j >= n:
        ans *= i
        j -= n

print(int(ans))
### BUGGY CODE ###

# coding: utf-8
import sys
input=lambda : sys.stdin.readline().rstrip()

class SqrtDecomposition:
    # RMQ用平方分割
    
    # n:要素数 init:配列初期値
    def __init__(self,n,init=0):
        self.data=[init for i in range(n)]
        #self.sqrt_n=int(n**0.5)+1
        self.sqrt_n=int(n**0.5)+1
        self.bucket=[init for i in range(self.sqrt_n)]
    
    # x を y にする
    def update(self,x,y):
        self.data[x] = y
        tmp = self.data[x//self.sqrt_n * self.sqrt_n]
        for i in range(x//self.sqrt_n * self.sqrt_n, min(x//self.sqrt_n * self.sqrt_n + self.sqrt_n, n)):
            if tmp > self.data[i]:
                tmp = self.data[i]
        self.bucket[x//self.sqrt_n] = tmp
    
    # [x,y) の最小を求める
    def find(self,x,y):
        tmp = 10**19
        for i in range(self.sqrt_n):
            l,r=i*self.sqrt_n,(i+1)*self.sqrt_n
            if r <= x or y <= l :
                continue
            if x <= l and r <= y :
                tmp = min(tmp, self.bucket[i]);
            else:
                for j in range(max(x, l),min(y, r)):
                    tmp = min(tmp, self.data[j]);
        return tmp


n,q = map(int,input().split())
sd=SqrtDecomposition(n,2**31-1)
for i in range(q):
    c,x,y=map(int,input().split())
    if c==0:
        sd.update(x,y)
    else:
        print(sd.find(x,y+1))





### FIXED CODE ###

# coding: utf-8
class seg_tree:
    def __init__(self,n,minimum=True):
        self.inf = 2**31-1
        self.size = 2**(len(bin(n-1))-2)*2-1
        self.minimum = minimum
        if self.minimum:
            self.tree = [self.inf for i in range(self.size)]
        else:
            self.tree = [-1 for i in range(self.size)]
     
    def data(self):
        return self.tree[self.size//2:]
 
    def update(self,i,x):
        i=self.size//2+i
        self.tree[i]=x
        if self.minimum:
            while 1:
                i=(i-1)//2
                self.tree[i]=min(self.tree[i*2+1],self.tree[i*2+2])
                if i<=0:
                    break
        else:
            while 1:
                i=(i-1)//2
                self.tree[i]=max(self.tree[i*2+1],self.tree[i*2+2])
                if i<=0:
                    break
 
    def _mincheck(self,s,t,l,r,_p=0):
        if s<=l and r<=t:
            return self.tree[_p]
        elif r<s or t<l:
            return self.inf
        else:
            return min(self._mincheck(s,t,l,l+(r-l)//2,_p*2+1),self._mincheck(s,t,l+(r-l)//2+1,r,_p*2+2))
     
    def _maxcheck(self,s,t,l,r,_p=0):
        if s<=l and r<=t:
            return self.tree[_p]
        elif r<s or t<l:
            return -1
        else:
            return max(self._maxcheck(s,t,l,l+(r-l)//2,_p*2+1),self._maxcheck(s,t,l+(r-l)//2+1,r,_p*2+2))
 
    def find(self,s,t):
        if self.minimum:
            return self._mincheck(s,t,0,self.size//2)
        else:
            return self._maxcheck(s,t,0,self.size//2)
 
n,q=map(int,input().split())
st=seg_tree(n)
ans=[]
for i in range(q):
    c,x,y=map(int,input().split())
    if c:
        ans.append(st.find(x,y))
    else:
        st.update(x,y)
print(*ans,sep='\n')
### BUGGY CODE ###

def main():
    h, w = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(h)]
    b = [list(map(int, input().split())) for _ in range(h)]
    n = 2000
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            q = min(min((i+j+2), (h-1-i)+(w-1-j)+2)*80, m)
            if 0 < i and 0 < h:
                for k in range(m-q, m+q):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(m-q, m+q):
                    if dp[i-1][j][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(m-q, m+q):
                    if dp[i][j-1][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(m-80-1, m+80+2):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

if __name__ == "__main__":
    main()


### FIXED CODE ###

import numpy as np
def main():
    h, w = map(int, input().split())
    a = [list(map(int, input().split())) for _ in range(h)]
    b = [list(map(int, input().split())) for _ in range(h)]
    n = 80*(80+80) + 10
    m = n//2
    dp = [[None]*w for i in range(h)]
    for i in range(h):
        for j in range(w):
            dp[i][j] = np.zeros(n, np.bool)
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            if 0 < i and 0 < h:
                dp[i][j][x:] |= dp[i-1][j][:n-x]
                dp[i][j][:n-x] |= dp[i-1][j][x:]
                dp[i][j][x:] |= dp[i][j-1][:n-x]
                dp[i][j][:n-x] |= dp[i][j-1][x:]
            elif 0 < i:
                dp[i][j][x:] |= dp[i-1][j][:n-x]
                dp[i][j][:n-x] |= dp[i-1][j][x:]
            elif 0 < j:
                dp[i][j][x:] |= dp[i][j-1][:n-x]
                dp[i][j][:n-x] |= dp[i][j-1][x:]
    ans = float("inf")
    for i in range(m-80-1, m+80+2):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

if __name__ == "__main__":
    main()