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

import numpy as np
N = int(input())
dtype = [('number',int),('height',int)]
A = np.array(list(enumerate(list(map(int, input().split())))),dtype=dtype)
count = 0
B = np.sort(A,order='height')
mem = [i+1 for i in range(N-1)]
Min = B[0][1] + B[1][1]
Max = B[-1][1] + B[-2][1]
for i in range(max(2,Min),min(N,Max+1)):
    for j in range(N-1):
        c = B[j][1]
        d = i - c
        for k in range(mem[j],N):
            e = B[k][1]
            if e == d:
                if abs(B[j][0]-B[k][0]) == i:
                    count += 1
            elif e > d:
                mem[j] = k
                break
print(count)

### FIXED CODE ###

from collections import Counter
N = int(input())
A = list(enumerate(list(map(int, input().split()))))
b = Counter([x[0]+x[1]+1 for x in A])
c = Counter([x[0]-x[1]+1 for x in A])
count = 0
for x in b.items():
    count += x[1] * c[x[0]]
print(count)
### BUGGY CODE ###

import math

N,M = map(int,input().split())
if abs(N-M) >= 2:
    print(0)
    exit()
elif abs(N-M) == 1:
    print(math.factorial(N)*math.factorial(M))
else:
    print(math.factorial(N)*math.factorial(M)*2)

### FIXED CODE ###

import math

N,M = map(int,input().split())
if abs(N-M) >= 2:
    print(0)
    exit()
elif abs(N-M) == 1:
    res = math.factorial(N)*math.factorial(M)
    print(res%(10**9+7))
else:
    res = math.factorial(N)*math.factorial(M) * 2
    print(res%(10**9+7))
### BUGGY CODE ###

a,b,n=map(int,input().split())
x=[]
for i in range(n+1):
  x.append(int(a*i/b)-a*int(i/b))
print(max(x))

### FIXED CODE ###

a,b,n=map(int,input().split())
x=[]
if n>=b:
  x.append(int(a*(b-1)/b))
else:
  x.append(int(a*n/b))
print(max(x))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import numpy as np

def get_gcd(a, b, c):
    min_ab = min(a, b)
    max_ab = max(a, b)
    amari = max_ab % min_ab
    if amari == 0:
        gcd1 = min_ab
    else:
        x = min_ab
        y = amari
        while y != 0:
            amari = x % y
            if amari == 0:
                gcd1 = y
                break
            x = y
            y = amari
    min_2 = min(c, gcd1)
    max_2 = max(c, gcd1)
    amari = max_2 % min_2
    if amari == 0:
        gcd2 = min_2
    else:
        x = min_2
        y = amari
        while y != 0:
            amari = x % y
            if amari == 0:
                gcd2 = y
                break
            x = y
            y = amari

    return gcd2


k = int(input())
ans = 0

for i in range(1, k+1):
    for j in range(1, k+1):
        for l in range(1, k+1):
            ans += get_gcd(i, j, l)

print(ans)

### FIXED CODE ###

# -*- coding: utf-8 -*-
import numpy as np

def get_gcd(a, b, c):
    min_ab = min(a, b)
    max_ab = max(a, b)
    amari = max_ab % min_ab
    if amari == 0:
        gcd1 = min_ab
    else:
        x = min_ab
        y = amari
        while y != 0:
            amari = x % y
            if amari == 0:
                gcd1 = y
                break
            x = y
            y = amari
    min_2 = min(c, gcd1)
    max_2 = max(c, gcd1)
    amari = max_2 % min_2
    if amari == 0:
        gcd2 = min_2
    else:
        x = min_2
        y = amari
        while y != 0:
            amari = x % y
            if amari == 0:
                gcd2 = y
                break
            x = y
            y = amari

    return gcd2


k = int(input())
ans = 0

for a in range(1, k+1):
    for b in range(a+1, k+1):
        if b == a:
            continue
        for c in range(b+1, k+1):
            if c == a or c == b:
                continue
            ans += get_gcd(a, b, c) * 6

for a in range(1, k+1):
    b = a
    for c in range(1, k+1):
        if c == a:
            continue
        ans += get_gcd(a, b, c) * 3

for a in range(1, k+1):
    b = a
    c = a
    ans += get_gcd(a, b, c)

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


def dp_table(*counts):  # dp[x][y]を作りたかったら dp_table(xのサイズ,yのサイズ)
    if counts:
        return [dp_table(*counts[1:]) for _ in range(counts[0])]
    else:
        return 0


def main():

    N = int(input())
    A = [int(input()) for i in range(N)]
    mod = 998244353
    ans = pow(3, N, mod)
    sum_A = sum(A)
    dp = dp_table(N, sum_A + 1)
    # dp[i][j]: i番目までの中から辺をいくつか選んで、長さがjになるパターン数

    # init
    dp[0][A[0]] = 1
    dp[0][0] = 2  # 他の色のうちどちらに入れるかで2通り

    # init2
    dp2 = dp_table(N, sum_A+1)  # ある二色でsum_Aを等分するパターンを数える
    dp2[0][A[0]] = 1
    dp2[0][0] = 1  # 今度は片方に集中したパターン

    # dp
    sum_now = A[0]
    for i in range(1, N):
        sum_now += A[i]
        for length in range(0, sum_now+1):
            # A[i]を追加しないのなら他の２食のうちどちらに入れるかで2倍選択肢がある
            if length >= A[i]:
                dp[i][length] = dp[i - 1][length - A[i]] + dp[i - 1][length] * 2
            else:
                dp[i][length] = dp[i-1][length]*2
            dp[i][length] %= mod
        for length in range(min(sum_now+1, sum_A//2+1)):
            if length >= A[i]:
                dp2[i][length] = dp2[i - 1][length - A[i]] + dp2[i - 1][length]
            else:
                dp2[i][length] = dp2[i-1][length]
            dp2[i][length] %= mod
    # print(sum(dp[N-1][sum_A//2]))
    # print(dp)
    ans -= 3*sum(dp[N-1][(sum_A+1)//2:])
    if sum_A % 2 == 0:
        ans += 3*dp2[N-1][sum_A//2]
    print(ans % mod)


if __name__ == '__main__':
    main()


### FIXED CODE ###


def dp_table(*counts):  # dp[x][y]を作りたかったら dp_table(xのサイズ,yのサイズ)
    if counts:
        return [dp_table(*counts[1:]) for _ in range(counts[0])]
    else:
        return 0


def main():

    N = int(input())
    A = [int(input()) for _ in range(N)]
    mod = 998244353
    ans = pow(3, N, mod)
    sum_A = sum(A)
    dp = [[0] * (sum_A+1) for i in range(N)]
    # dp[i][j]: i番目までの中から辺をいくつか選んで、長さがjになるパターン数

    # init
    dp[0][A[0]] = 1
    dp[0][0] = 2  # 他の色のうちどちらに入れるかで2通り

    # init2
    dp2 = [[0] * (sum_A+1) for i in range(N)]  # ある二色でsum_Aを等分するパターンを数える
    dp2[0][A[0]] = 1
    dp2[0][0] = 1  # 今度は片方に集中したパターン

    # dp
    sum_now = A[0]
    for i in range(1, N):
        a = A[i]
        sum_now += a
        for length in range(sum_now+1):
            # A[i]を追加しないのなら他の２食のうちどちらに入れるかで2倍選択肢がある
            if length >= a:
                dp[i][length] = dp[i - 1][length - a] + \
                    (dp[i - 1][length] << 1)
            else:
                dp[i][length] = dp[i-1][length] << 1
            dp[i][length] %= mod

        for length in range(min(sum_now + 1, sum_A // 2 + 1)):
            if length >= a:
                dp2[i][length] = dp2[i - 1][length - a] + dp2[i - 1][length]
            else:
                dp2[i][length] = dp2[i-1][length]
            dp2[i][length] %= mod
    # print(sum(dp[N-1][sum_A//2]))
    # print(dp)
    ans -= 3*sum(dp[N-1][(sum_A+1)//2:])
    if sum_A % 2 == 0:
        ans += 3*dp2[N-1][sum_A//2]
    print(ans % mod)


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

N = int(input())
P = list(map(int,input().split()))
ans = 1

for i in range(1,N):
  if P[i] <= min(P[:i]): 
    ans += 1
print(ans)

### FIXED CODE ###

N = int(input())
P = list(map(int,input().split()))
ans = 1
small = P[0]
for i in range(1,N):
  if P[i] <= small: 
    ans += 1
    small = P[i]
print(ans)
### BUGGY CODE ###

# E - Dividing Chocolate
import numpy as np

H, W, K = map(int, input().split())
S = np.zeros((H, W), dtype=np.int64)

ans = H*W

for i in range(H):
    S[i] = list(str(input()))

for m in range(2**(H-1)):
    wp = np.zeros((H, W), dtype=np.int64)
    wq = np.zeros((H, ), dtype=np.int64)
    wp[0] = S[0]
    h = 0
    tmp = 0
    for n in range(H-1):
        if m>>n&1:
            h += 1
            tmp += 1
        wp[h] += S[n+1]

    for j in range(W):
        wq += wp[:,j]
        if any([a > K for a in wq]):
            tmp += 1
            wq = wp[:,j]
    ans = min(ans, tmp)

print(ans)


### FIXED CODE ###

# E - Dividing Chocolate
import numpy as np

H, W, K = map(int, input().split())
S = np.zeros((H, W), dtype=np.int64)

ans = H+W

for i in range(H):
    S[i] = list(str(input()))

for m in range(2**(H-1)):
    wp = np.zeros((H, W), dtype=np.int64)
    wq = np.zeros((H, ), dtype=np.int64)
    wp[0] = S[0]
    cut = 0
    for n in range(H-1):
        if m>>n&1:
            cut += 1
        wp[cut] += S[n+1]

    if cut >= ans or np.count_nonzero(wp > K):
        continue

    for j in range(W):
        wq += wp[:,j]
        if np.count_nonzero(wq > K):
            cut += 1
            wq = wp[:,j]
    ans = min(ans, cut)

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

s = input()

ans = 0
for i, c in enumerate(s):
    # print(i, c)
    if c == 'B':
        num_W=0
        for ac in s[i:]:
            # print(ac)
            if ac == 'W':
                num_W+=1
            # print('num = ', num_W)
        ans += num_W
    # print("ans = ", ans)

print(ans)


### FIXED CODE ###

s = input()

ans = 0

num_B=0
num_W=0

for ele in s:
    # print(ele, num_W,num_B, ans)
    if ele == 'B':
        ans += num_B*num_W
        num_B+=1
        num_W=0
    else:
        num_W+=1
ans += num_B*num_W
print(ans)
### BUGGY CODE ###

# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))
flug = False
kaizen=0

for i in range(1,N):
    if A[i-1]>A[i]:
        kaizen=i
        flug=True
        break
    
if flug==False:
    print(0)
    exit()

keisu=0
keisuichi=0
for i in range(N):
    if abs(A[i])>keisu:
        keisu=A[i]
        keisuichi=i
if keisu>0:
    Ansx=[]
    Ansy=[]    
    while flug==True:
        Ansx.append(keisuichi)
        Ansy.append(kaizen)
        Ansx.append(keisuichi)
        Ansy.append(kaizen)
        A[kaizen]=A[kaizen]+A[keisuichi]*2
        keisuichi=kaizen
        flug=False
        for i in range(1,N):
            if A[i-1]>A[i]:
                kaizen=i
                flug=True
                break
        
    print(len(Ansx))
    for i in range(len(Ansx)):
        print(Ansx[i]+1,Ansy[i]+1)
    
else:
    Ansx=[]
    Ansy=[]
    for i in range(N):
        Ansx.append(keisuichi)
        Ansy.append(N-i)
        Ansx.append(keisuichi)
        Ansy.append(N-i)
        A[N-i-1]=A[N-i-1]+A[keisuichi]*2
        keisuichi=N-i-1
        
        
    print(len(Ansx))
    for i in range(len(Ansx)):
        print(Ansx[i]+1,Ansy[i])
    

### FIXED CODE ###

# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))
flug = False
kaizen=0

for i in range(1,N):
    if A[i-1]>A[i]:
        kaizen=i
        flug=True
        break
    
if flug==False:
    print(0)
    exit()

keisu=0
keisuichi=0
for i in range(N):
    if abs(A[i])>abs(keisu):
        keisu=A[i]
        keisuichi=i
if keisu>0:
    Ansx=[]
    Ansy=[]
    for i in range(N):
        Ansx.append(keisuichi)
        Ansy.append(i)
        Ansx.append(keisuichi)
        Ansy.append(i)
        A[i]=A[i]+A[keisuichi]*2
        keisuichi=i
        
        
    print(len(Ansx))
    for i in range(len(Ansx)):
        print(Ansx[i]+1,Ansy[i]+1)
    
else:
    Ansx=[]
    Ansy=[]
    for i in range(N):
        Ansx.append(keisuichi)
        Ansy.append(N-i)
        Ansx.append(keisuichi)
        Ansy.append(N-i)
        A[N-i-1]=A[N-i-1]+A[keisuichi]*2
        keisuichi=N-i-1
        
        
    print(len(Ansx))
    for i in range(len(Ansx)):
        print(Ansx[i]+1,Ansy[i])
### BUGGY CODE ###

S = input()

m = 0
while True:
	a = 0
	
	a += S.count('10')
	S = S.replace('10', '')

	a += S.count('01')
	S = S.replace('01', '')

	if a!=0:
		m += a
	else:
		break

print(2*m)


### FIXED CODE ###

S = input()

red = 0  # 0
blue = 0 # 1
m = 0
for s in S:
	if s == '0':
		if blue != 0:
			blue -= 1
			m += 2
		else:
			red += 1
	elif s == '1':
		if red != 0:
			red -= 1
			m += 2
		else:
			blue += 1

print(m)
### BUGGY CODE ###

import queue

def bfs(maze,h,w,sy,sx,gy,gx):
    dist=[[-1 for _ in range(w)] for _ in range(h)]
    q=queue.deque([[sy,sx]])
    dist[sy][sx]=0
    while q:
        y,x=q.popleft()
        for dy,dx in d:
            ny,nx=y+dy,x+dx
            if 0<=ny<h and 0<=nx<w and dist[ny][nx]==-1 and maze[ny][nx]!='#':
                dist[ny][nx]=dist[y][x]+1
                q.append([ny,nx])
    return dist[gy][gx]

h,w=map(int,input().split())
a=[input() for _ in range(h)]
d=[[1,0],[0,1],[-1,0],[0,-1]]
ans=0
for sy in range(h):
    for sx in range(w):
        for gy in range(h):
            for gx in range(w):
                if a[sy][sx]!='#' and a[gy][gx]!='#':
                    ans=max(ans,bfs(a,h,w,sy,sx,gy,gx))
print(ans)

### FIXED CODE ###

import queue

def bfs(maze,h,w,sy,sx):
    dist=[[-1 for _ in range(w)] for _ in range(h)]
    q=queue.deque([[sy,sx]])
    dist[sy][sx]=0
    while q:
        y,x=q.popleft()
        for dy,dx in d:
            ny,nx=y+dy,x+dx
            if 0<=ny<h and 0<=nx<w and dist[ny][nx]==-1 and maze[ny][nx]!='#':
                dist[ny][nx]=dist[y][x]+1
                q.append([ny,nx])
    return max([max(i) for i in dist])

h,w=map(int,input().split())
a=[input() for _ in range(h)]
d=[[1,0],[0,1],[-1,0],[0,-1]]
ans=0
for sy in range(h):
    for sx in range(w):
        if a[sy][sx]!='#':
            ans=max(ans,bfs(a,h,w,sy,sx))
print(ans)
### BUGGY CODE ###

from functools import lru_cache
import sys
sys.setrecursionlimit(10 ** 6)
Hitpoint, N = map(int, input().split())
magic_damage = []
magic_point = []
for _ in range(N):
    A, B = map(int, input().split())
    magic_damage.append(A)
    magic_point.append(B)

@lru_cache(maxsize=None)
def solve(i, now_HP):
    if now_HP <= 0:
        retval = 0
    elif i == N:
        retval = float('inf')
    else:
        retval = min(solve(i + 1, now_HP), solve(i, now_HP - magic_damage[i]) + magic_point[i])
    return retval


print(solve(0, Hitpoint))

### FIXED CODE ###

import math
H, N = map(int, input().split())
dp = [[0 for _ in range(H + 1)] for _ in range(N)]
A, B = map(int, input().split())
for i in range(H + 1):
    cnt = math.ceil(i / A)
    dp[0][i] = B * cnt

for i in range(1, N):
    A, B = map(int, input().split())
    for j in range(H + 1):
        dp[i][j] = min(dp[i - 1][j], dp[i][max(j - A, 0)] + B)
# print(*dp, sep='\n')
print(dp[-1][-1])
### BUGGY CODE ###

s = input()
while s.find('ST')!=-1:
    s = s.replace('ST','')
print(len(s))


### FIXED CODE ###

s = input()
stack = [s[0]]
for i in range(1,len(s)):
    if len(stack)==0:
        stack.append(s[i])
        continue
    else:
        cur_str = stack[-1]
        new_str = s[i]
        if cur_str+new_str=="ST":
            stack.pop()
        else:
            stack.append(new_str)
print(len(stack))
### BUGGY CODE ###

N, C = map(int, input().split())
D = []
c = []
for _ in range(C):
    D.append([int(i) for i in input().split()])

for _ in range(N):
    c.append([int(i) for i in input().split()])


ansD = []
for c1 in range(C):
    for c2 in range(C):
        if c1 == c2:
            continue
        for c3 in range(C):
            if c1 == c3 or c2 == c3:
                continue
            sumD = 0
            for i in range(N):
                for j in range(N):
                    if (i+j) % 3 == 0:
                        goodc = c1
                    if (i+j) % 3 == 1:
                        goodc = c2
                    if (i+j) % 3 == 2:
                        goodc = c3
                    sumD += D[c[i][j]-1][goodc]
            ansD.append(sumD)
ansD.sort()
print(ansD[0])

### FIXED CODE ###

import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()
### BUGGY CODE ###

# -*- coding:utf-8 -*-
N, M = map(int, input().split())
if M > 0:
    if N*2 <= M:
        print((N+M//2)//2)
    else:
        ans = 0
        while True:
            N -= 1
            M -= 2
            if M >= 0 and N >= 0:
               ans += 1
            else:
                break
        print(ans)
else:
    print(0)

### FIXED CODE ###

# -*- coding:utf-8 -*-
N, M = map(int, input().split())
if M > 0:
    if N*2 <= M:
        print((N+M//2)//2)
    else:
        print(M//2)
else:
    print(0)
### BUGGY CODE ###

n = int(input())
bbr = []
bbr.append([1,int(input())])
for i in range(n-1):
    p = int(input())
    for j in range(n):
        if bbr[j][1]+1 == p:
            bbr[j][1] += 1
            bbr[j][0] += 1
            break
        if j == len(bbr)-1:
            bbr.append([1,p])
            break

bbr.sort(reverse=True)
print(n-bbr[0][0])


### FIXED CODE ###

n = int(input())
cn = [0 for i in range(n+1)]
for i in range(n):
    p = int(input())
    cn[p] = cn[p-1]+1

print(n-max(cn))
### BUGGY CODE ###


def main():
    from fractions import gcd
    import sys
    n=int(input())
    a = tuple(map(int, input().split()))
    ans = a[0]

    #mmm=10**9+7
    for i in a[1:]:
        ans = ans *i // gcd(ans, i)

    result=0
    for item in a:
      result=(result+ans//item)%(10**9+7)

    print(result)

if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    from fractions import gcd
    import sys
    n=int(input())
    a = tuple(map(int, input().split()))
    ans = 1

    mmm=10**9+7
    for i in a:
        ans = ans *i // gcd(ans, i)

    result=0
    for item in a:
      result+=ans//item

    print(result%mmm)

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

import numpy as np

def main():
    n,k = map(int,input().split())
    s = input()
    li = np.array([])
    count = 1


    for i in range(n-1):
        if s[i] == s[i+1]:
            count += 1
        else:
            li = np.append(li,count)
            count = 1
    li = np.append(li,count)

    m = len(li)

    if s[0] == '1':
        t = int(m/2)
        if k >= t:
            print(int(sum(li)))
        else:
            max1 = 0
            for i in range(t-k+1):
                max1 = max(np.sum(li[2*i:2*(i+k)+1]),max1)
            print(int(max1))
    else:
        t = int(m/2+1)
        if k >= t:
            print(int(sum(li)))
        else:
            max1 = sum(li[:2*k])
            for i in range(t-k):
                max1 = max(np.sum(li[2*i+1:2*(i+k)+2]),max1)
            print(int(max1))
main()


### FIXED CODE ###

n,k = map(int,input().split())
s = input()
li = [0]
count = 1

for i in range(n-1):
    if s[i] != s[i+1]:
        li.append(count)
    count += 1
li.append(count)

m = len(li)

if s[0] == '1':
    li.append(li[-1])
    li.append(li[-1])
    t = int(m/2-1)
    if k >= t:
        print(n)
    else:
        max1 = 0
        for i in range(t-k+2):
            max1 = max(li[2*(i+k)+1] - li[2*i],max1)
        print(max1)
else:
    li.append(li[-1])
    t = int(m/2)
    if k >= t:
        print(n)
    else:
        max1 = li[2*k]
        for i in range(t-k):
            max1 = max(li[2*(i+k+1)] - li[2*i+1],max1)
        print(max1)
### BUGGY CODE ###

inputData = input().split()
N, K, Q = [int(x) for x in inputData]
points = [K]*N

for i in range(Q):
    A = int(input())
    points[A-1] += 1
    for k in range(N):
        points[k] -= 1
for j in range(N):
    if points[j] > 0:
        print('Yes')
    else:
        print('No')


### FIXED CODE ###

import numpy as np

inputData = input().split()
N, K, Q = [int(x) for x in inputData]
points = np.array([K]*N)

for i in range(Q):
    A = int(input())
    points[A-1] += 1
points -= Q
for j in range(N):
    if points[j] > 0:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

import fractions
n,m=map(int,input().split())
if m%n==0:
    print(int(m/n))
else:
    ans=1
    for i in range(2,m//n+1):
        c=fractions.gcd(i,m-i*n)
        if c>ans:
            ans=c
    print(ans)




### FIXED CODE ###

n, m = map(int, input().split())
l = []
for i in range(1, int(m**(1/2)) + 1):
	if m % i == 0:
		l.append(i)
		l.append(m // i)
l.sort()
l=l[::-1]
for i in l:
    if m//n>=i:
        print(i)
        break
### BUGGY CODE ###

n = int(input())

x_org = list(map(int, input().split()))

for i in range(n) :
    x = x_org[:]
    del x[i]
    x.sort()
    print(x[n // 2 - 1])

### FIXED CODE ###

n = int(input())

x_org = list(map(int, input().split()))

x = sorted(x_org)

mid = x[n // 2 - 1]
midr = x[n // 2]

for i in range(n) :
    if x_org[i] <= mid :
        print(midr)
    else :
        print(mid)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):
    base = [[None]*W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            base[h][w] = abs(A[h][w]-B[h][w])

    table = [[0 for _ in range(W)] for _ in range(H)]
    table[0][0] |= (1 << base[0][0])
    for h in range(H):
        for w in range(W):
            if h > 0:
                for i in range(6401):
                    j = (table[h-1][w] >> i) & 1
                    if j == 0:
                        continue
                    if base[h][w]+i < 6401:
                        table[h][w] |= 1 << abs(i+base[h][w])
                    table[h][w] |= 1 << abs(i-base[h][w])
            if w > 0:
                for i in range(6401):
                    j = (table[h][w-1] >> i) & 1
                    if j == 0:
                        continue
                    if base[h][w]+i < 6401:
                        table[h][w] |= 1 << abs(i+base[h][w])
                    table[h][w] |= 1 << abs(i-base[h][w])
    for j in range(6401):
        i = (table[H-1][W-1] >> j) & 1
        if i == 1:
            print(j)
            return

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [[int(next(tokens)) for _ in range(W)]
         for _ in range(H)]  # type: "List[List[int]]"
    B = [[int(next(tokens)) for _ in range(W)]
         for _ in range(H)]  # type: "List[List[int]]"
    solve(H, W, A, B)


if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):
    base = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
    mx = (1 << 12800) - 1
    table = [[0]*W for _ in range(H)]
    table[0][0] = (1 << (6400 + base[0][0])) | (1 << (6400-base[0][0]))
    for h in range(H):
        for w in range(W):
            if h > 0:
                table[h][w] |= (table[h-1][w] << base[h][w])
                table[h][w] |= (table[h-1][w] >> base[h][w])
            if w > 0:
                table[h][w] |= (table[h][w-1] << base[h][w])
                table[h][w] |= (table[h][w-1] >> base[h][w])
            table[h][w] &= mx

    m = INF
    for j in range(12800):
        i = (table[H-1][W-1] >> j) & 1
        if i == 1:
            m = min(m, abs(j-6400))
    print(m)
    # print(table[H-1])

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [[int(next(tokens)) for _ in range(W)]
         for _ in range(H)]  # type: "List[List[int]]"
    B = [[int(next(tokens)) for _ in range(W)]
         for _ in range(H)]  # type: "List[List[int]]"
    solve(H, W, A, B)


if __name__ == '__main__':
    main()