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


N = int(input())
A = list(map(int, input().split()))

buka = [0] * N
for i in range(1, N+1, 1):
    buka[i-1] = str(A.count(i))

print("\n".join(buka))

### FIXED CODE ###


# TLE
"""
N = int(input())
A = list(map(int, input().split()))

buka = [str(A.count(i)) for i in range(1, N+1, 1)] # countメソッドのためTLE

print("\n".join(buka))
"""
"""
# sys（入力メイン）-----------------------------------------
import sys
read = sys.stdin.buffer.read # 全読み込み
readline = sys.stdin.buffer.readline # 一行読み込み
readlines = sys.stdin.bufer.readlines # 一行ずつ全読み込み
sys.setrecursionlimit(10**7) # 再帰限界変更（デフォルトは1000）

# 文字列（数値変換も含む）
n = readline().rstrip() # １行に１文字列のみ
lis = readline().rstrip().split() # # １行に複数の文字列

# 数値（文字変換も含む）
n = int(readline().rstrip()) # １行に数値１個のみ
a, b, c = int(readline().rstrip().split()) # １行に複数の数値
lis = [readline().rstrip().split() for _ in range(n)] # １行に複数の数値(m個)かつ、n行続く場合（出力は二次元リスト：[['1', '2'], ['4', '5']]）

-----------------------------------------
"""

import sys
read = sys.stdin.buffer.read # 全読み込み
readline = sys.stdin.buffer.readline # 一行読み込み
readlines = sys.stdin.buffer.readlines # 一行ずつ全読み込み
sys.setrecursionlimit(10**7) # 再帰限界変更（デフォルトは1000）

N = int(readline().rstrip()) 
A = list(map(int,readline().rstrip().split()))

buka=[0]*N

for i in range(N-1):
    buka[A[i]-1]+=1

L=[str(a) for a in buka]
L="\n".join(L)
print(L)
### BUGGY CODE ###

a = input()
a_list = list(map(int, input().split()))
ans = 0
checked_list = []

for i in a_list:
  if i in checked_list:
    pass
  else:
    ans = max(ans, a_list.count(i-1) + a_list.count(i) + a_list.count(i+1))
    checked_list.append(i)
  
print(ans)

### FIXED CODE ###

from collections import Counter
a = input()
a_list = list(map(int, input().split()))
ans = 0
b_list = []
c_list = []

for i in a_list:
    b_list.append(i+1)
    c_list.append(i-1)

ans_list = a_list + b_list + c_list
counter = Counter(ans_list)
print(counter.most_common()[0][1])
### BUGGY CODE ###

import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import math
#import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def main():
	n,m=map(int,input().split())
	li=[[] for _ in range(n+1)]
	for i in range(m):
		l,r,d=map(int,input().split())
		li[l].append((r,d))
		li[r].append((l,-d))
	
	q=deque()
	a=[None]*(n+1)

	for i in range(1,n+1):
		if a[i]==None:
			q=[i]
			a[i]=0
			while q:
				now=q.pop()
				for r,d in li[now]:
					if r in q:
						if a[r]-a[now]!=d:
							print("No")
							exit(0)
						else:
							continue
					else:
						a[r] = a[now] + d
						q.append(r)
					
	print("Yes")
	
	
if __name__ == '__main__':
	main()

### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import math
#import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def main():
	n,m=map(int,input().split())
	li=[[] for _ in range(n+1)]
	for i in range(m):
		l,r,d=map(int,input().split())
		li[l].append((r,d))
		li[r].append((l,-d))
	
	q=deque()
	a=[None]*(n+1)

	for i in range(1,n+1):
		if a[i]==None:
			q=[i]
			a[i]=0
			while q:
				now=q.pop()
				for r,d in li[now]:
					if a[r]==None:
						a[r] = a[now] + d
						q.append(r)
					else:
						if a[r]-a[now]!=d:
							print("No")
							exit(0)
						else:
							continue
					
	print("Yes")
	
	
if __name__ == '__main__':
	main()
### BUGGY CODE ###

n=int(input())

cnt=0

for i in range(1,n):
    for j in range(i,n-i+1):
        if i*j<n and i==j:
            cnt+=1
        elif i*j<n:
            cnt+=2
        else:
            pass

print(cnt)

### FIXED CODE ###

n=int(input())

m=int((n-1)**(1/2))

cnt=0
for i in range(1,m+1):
    cnt+=((n-1)//i-i)*2+1

print(cnt)
### BUGGY CODE ###

from itertools import accumulate

def main():
    n, k = map(int, input().strip().split())
    A = [*map(int, input().strip().split())]
    
    # for _ in range(min(k,int(math.log(n,2)))):
    
    for _ in "_"*k:
        b=[0]*(n+1)
        
        for i,v in enumerate(A):
            b[max(0,i-v)]+=1
            b[min(n,i+v+1)]-=1
        
        # update ans
        # cnt=0
        # for i in range(1,n):
        #     b[i]+=b[i-1]
        A=[*accumulate(b)][:-1]
        
        if min(A)==n:
            break
    print(" ".join(map(str,A)))

if '__main__' == __name__:
    main()

### FIXED CODE ###

import sys
from itertools import accumulate
input=sys.stdin.buffer.readline

def main():
    n, k = map(int, input().split())
    A = [*map(int, input().split())]
    
    # for _ in range(min(k,int(math.log(n,2)))):
    
    for _ in "_"*k:
        b=[0]*(n)
        
        for i,v in enumerate(A):
            if i>v:
            # b[max(0,i-v)]+=1
                b[i-v]+=1
            else:
                b[0]+=1
            j=i-~v
            # if i+v+1<n:
            #     b[i+v+1]-=1
            if j<n:
                b[j]-=1
        
        # update ans
        # cnt=0
        # for i in range(1,n):
        #     b[i]+=b[i-1]
        A=[*accumulate(b)]
        
        if min(A)==n:
            break
    print(" ".join(map(str,A)))

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

from sys import stdin
S = input()
N = int(input())
Q =  [list(map(str, stdin.readline().split())) for _ in range(N)]
flag = True
for i in range(N):
    if Q[i][0]=="1":
        flag = not(flag)                
    else: 
        if (Q[i][1] == "2") == (flag == True):
            S += Q[i][2]#先頭
        else :
            S1 = Q[i][2]+ S
            S = S1

if flag == True:
    print(S)
else:
    print(S[::-1])  

### FIXED CODE ###

from sys import stdin
S = input()
x = S
y =str()   
N = int(input())
Q =  [list(map(str, stdin.readline().split())) for _ in range(N)]
flag = True
for i in range(N):
    if Q[i][0]=="1":
        flag = not(flag)                
    else: 
        if (Q[i][1] == "2") == (flag == True):
            x += Q[i][2]#先頭
        else :
            y += Q[i][2]
M = "".join([y[::-1],x])
if flag == True:
    print(M)
else:
    print(M[::-1])
### BUGGY CODE ###

def main(n: int, k: int, a: list):
    calc = [0 for i in range(n)]
    ans = 0 
    for _a in a:
        calc[_a - 1] += 1

    calc = [i for i in calc if i != 0]
    calc.sort()

    while len(calc) > k:
        ans += calc.pop(0)
    
    print(ans)

if __name__ == '__main__':
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    main(n, k, a)


### FIXED CODE ###

from collections import Counter


def main(n: int, k: int, a: list):
    calc = Counter(a)
    calc = sorted(calc.values())

    print(sum(calc[:len(calc) - k]))


if __name__ == '__main__':
    n, k = map(int, input().split())
    a = list(map(int, input().split()))

    main(n, k, a)
### BUGGY CODE ###

# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

# 約数
N = ir()
A = lr()
A.sort()
answer = 0
se = set()

def make_divisors(n):  # nの約数を列挙
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors

for i, a in enumerate(A):
    if i < N-1 and a == A[i+1]:
        se.add(a)
        continue
    divs = make_divisors(a)
    bl = True
    for x in divs:
        if x in se:
            bl = False
            break
    if bl:
        answer += 1
    se.add(a)

print(answer)


### FIXED CODE ###

# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
A.sort()
bl = [True] * (A[-1]+1)
answer = 0
for i, a in enumerate(A):
    if i < N-1 and A[i+1] == a:
        pass
    elif bl[a]:
        answer += 1
    if bl[a]:
        bl[a] = False
        next = a + a
        while next <= A[-1]:
            bl[next] = False
            next += a

print(answer)
### BUGGY CODE ###

n = int(input())
list_S = list(input())
list_R = []
list_B = []
list_G = []
ans = 0
for i, c in enumerate(list_S):
    if c == "R":
        list_R.append(i+1)
    if c == "B":
        list_B.append(i+1)
    if c == "G":
        list_G.append(i+1)

for i in list_R:
    for j in list_G:
        for k in list_B:
            l = [i,j,k]
            l.sort()
            if l[1]-l[0] != l[2]-l[1]:
                ans += 1

print(ans)

### FIXED CODE ###

n = int(input())
list_S = list(input())
list_R = []
list_B = []
list_G = []
ans = 0
for i, c in enumerate(list_S):
    if c == "R":
        list_R.append(i+1)
    if c == "B":
        list_B.append(i+1)
    if c == "G":
        list_G.append(i+1)
set_R = set(list_R)
set_G = set(list_G)
set_B = set(list_B)
cnt = 0
for i in list_R:
    for j in list_G:
        max_value = max(i, j)
        min_value = min(i, j)
        d = (max_value - min_value)
        if d % 2 == 0:
            if max_value-d//2 in set_B:
                cnt += 1

        if max_value+d in set_B:
            cnt += 1
        if min_value-d in set_B:
            cnt += 1
        ans += len(set_B)

print(ans-cnt)
### BUGGY CODE ###

n = int(input())
alist = list(map(int,input().split()))
binlist = [0 for _ in range(60)]
mod_number = 10**9+7
for a in alist:
    a_bin = bin(a)
    for i in range(len(a_bin)-2):
        if int(a_bin[-(i+1)])==1:
            binlist[-(i+1)] += 1
            #print(a,a_bin,i,binlist)
ans = 0
for i in range(60):
    ans = (ans + binlist[-(i+1)]*(n-binlist[-(i+1)])*pow(2,i))%mod_number
    
print(ans)

### FIXED CODE ###

n = int(input())
alist = list(map(int,input().split()))
binlist = [0]*60
mod_number = 10**9+7

def dec2bin(target):
    amari = []
    while target != 0:
        amari.append(target%2)
        target = target//2
    amari.reverse()
    return amari

for a in alist:
    a_bin = dec2bin(int(a))
    for i in range(len(a_bin)):
        if int(a_bin[-(i+1)])==1:
            binlist[-(i+1)] += 1
            #print(a,a_bin,i,binlist)
ans = 0
for i in range(60):
    ans = (ans + binlist[-(i+1)]*(n-binlist[-(i+1)])*pow(2,i))%mod_number
    
print(ans)
### BUGGY CODE ###

import copy

N = int(input())
B = [int(i) for i in input().split()]


def dfs(arr, indexes):
    if len(arr) == 0:
        return indexes

    for i in range(len(arr)):
        if i+1 == arr[i]:
            _indexes = copy.copy(indexes)
            _indexes.append(i)
            res = dfs(arr[:i] + arr[i+1:], _indexes)
            if res:
                return res
    return False


res = dfs(B, [])
if res:
    for i in reversed(res):
        print(i+1)
else:
    print(-1)


### FIXED CODE ###

import copy

N = int(input())
B = [int(i) for i in input().split()]


def dfs(arr, indexes):
    if len(arr) == 0:
        return indexes

    for idx, elm in enumerate(arr):
        if idx+1 < elm:
            return False

    for i in reversed(range(len(arr))):
        if i+1 == arr[i]:
            _indexes = copy.copy(indexes)
            _indexes.append(i)
            res = dfs(arr[:i] + arr[i+1:], _indexes)
            if res:
                return res
    return False


res = dfs(B, [])
if res:
    for i in reversed(res):
        print(i+1)
else:
    print(-1)
### BUGGY CODE ###

N = int(input())
a = [int(i) for i in input().split()] #横にNこ
num = 0
for j in range(N):
    k = j + 1 
    while k < N+1:
        if (sum(a[j:k])==0):
            num = num + 1
        k = k+1
print(num)

### FIXED CODE ###

N = int(input())
a = [int(i) for i in input().split()] 
b =[0]*(N+1)
for i in range(N):
    b[i+1]= b[i] + a[i]
b.sort()
j = 0
num = 0
v = 0
while j < N:
    if b[j+1] == b[j]:
        v = v+1
    else:
        num = num + (v*(v+1))//2
        v = 0
    j = j +1
num = num + (v*(v+1))//2
print(num)
### BUGGY CODE ###

S = list(input())
cnt = 0
for i in range(len(S)):
    if S[i] == "W":
        cnt += S[:i].count("B")
print(cnt)

### FIXED CODE ###

S = list(input())
cntb = 0
cntw = 0
for i in range(len(S)):
    if S[i] == "B":
        cntb +=1
    if S[i] == "W":
        cntw += cntb
print(cntw)
### BUGGY CODE ###

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)]

k_max = 81 * (h + w)
dp = [[[0] * (k_max+1) for _ in range(w+1)] for _ in range(h+1)]
for i in range(h + 1):
    dp[i][0][0] = 1

for j in range(w + 1):
    dp[0][j][0] = 1

for i in range(1, h + 1):
    for j in range(1, w + 1):
        ab = abs(a[i-1][j-1] - b[i-1][j-1])
        for k in range(k_max + 1):
            d = dp[i-1][j][abs(k-ab)]
            r = dp[i][j-1][abs(k-ab)]
            if k + ab <= k_max:
                d |= dp[i-1][j][k+ab]
                r |= dp[i][j-1][k+ab]
            dp[i][j][k] = d or r

ans = 0
for k in range(k_max + 1):
    if dp[h][w][k]:
        ans = k
        break

print(ans)


### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H,W = map(int,readline().split())
A = [list(map(int,readline().split())) for _ in range(H)]
B = [list(map(int,readline().split())) for _ in range(H)]

X = (H + W) * 80
# X点持った状態から始める
dp = [[0] * W for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0] = (1 << (X - d))
for h in range(H):
    for w,(a,b) in enumerate(zip(A[h],B[h])):
        if h == w == 0:
            continue
        d = abs(a - b)
        x = 0
        if h != 0:
            x |= (dp[h-1][w] << d) 
            x |= (dp[h-1][w] >> d)
        if w != 0:
            x |= (dp[h][w-1] << d) 
            x |= (dp[h][w-1] >> d)
        dp[h][w] = x

dp = dp[-1][-1]

B = bin(dp)[2:]
can_make = [i - X for i,b in enumerate(B[::-1]) if b == '1']

answer = min(x if x >= 0 else -x for x in can_make)
print(answer)
### BUGGY CODE ###

n, *aa = map(int, open(0).read().split())

for i in range(1, n+1):
    have = 0
    for a in aa:
        if a == i:
            have += 1
    
    print(have)


### FIXED CODE ###

n, *aa = map(int, open(0).read().split())

from collections import Counter
ca = Counter(aa)

for i in range(1, n+1):
    have = ca[i]
    print(have)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**9)
n, m, k = map(int, input().split())
color = [i for i in range(m)]
ans = 0
def dfs(i, daburi, num):
    global ans
    if i == n-1:
        if daburi == k:
            ans += num % 998244353
            return 
        elif daburi < k:
            ans += num % 998244353
            return
    if daburi > k:
        return
    dfs(i+1, daburi, num*(m-1)% 998244353)
    dfs(i+1, daburi+1, num% 998244353)
dfs(0, 0, m)
print(ans% 998244353)

### FIXED CODE ###

import math
n, m, k = map(int, input().split())

p = 998244353

def comb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n-r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]

for i in range(2, n+1):
    fact.append(fact[-1]*i%p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1]*inv[i])%p)
ans = 0

for i in range(k+1):
    row = comb(n-1, i, p)
    column = m * pow(m-1, n-i-1, p)
    ans += row * column
    
print(ans % p)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
N,M=map(int,input().split())
list1=list(map(int,input().split()))
listBC=[]
for _ in range(M):
  B,C=map(int,input().split())
  listBC+=[[C,B]]  
listBC.sort(reverse=True)
i=0
while min(list1)<listBC[i][0]:  
  list1+=[listBC[i][0]]*listBC[i][1]
  for k in range(listBC[i][1]):
    list1.remove(min(list1))
  i+=1
  if i==len(listBC):
    break
SUM=sum(list1)
print(SUM)

### FIXED CODE ###

N,M = map(int, input().split())
listA = list(map(int, input().split()))
listA.sort()
 
listBC = [list(map(int, input().split())) for i in range(M)]
listBC = sorted(listBC, key=lambda x: x[1], reverse=True)
countB = 0
 
for i in range(M):
  B, C = listBC[i][0], listBC[i][1]
  listA += [C]*B
  countB += B
  if countB >= N:
    break

listA.sort(reverse=True)
print(sum(listA[:N]))
### BUGGY CODE ###

def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    n, a = reads()
    x = reads()
    s = sum(x)
    dp = [[[0]*3500 for j in range(n+1)] for i in range(n+1)]
    dp[0][0][0] = 1
    for i in range(n):
        for j in range(n):
            for k in range(s):
                dp[i][j+1][k] += dp[i][j][k]
                dp[i+1][j+1][k+x[j]] += dp[i][j][k]
    r = 0
    for j in range(1, n+1):
        r += dp[j][n][j*a]
    print(r)

main()


### FIXED CODE ###

def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    n, a = reads()
    x = reads()
    s = sum(x)
    dp = [[[0]*3500 for j in range(n+1)] for i in range(n+1)]
    dp[0][0][0] = 1
    for i in range(n):
        for j in range(n):
            for k in range(s):
                dp[i][j+1][k] += dp[i][j][k]
                dp[i+1][j+1][k+x[j]] += dp[i][j][k]
    r = 0
    for j in range(1, n+1):
        r += dp[j][n][j*a]
    print(r)

main()
### BUGGY CODE ###

X, N = input().split()
X = int(X)
N = int(N)

if N == 0:
    print(X)
    exit()

P = input().split()
for i in range(len(P)):
    P[i] = int(P[i])

if X not in P:
    print(X)

flag_p = True
flag_n = True

abs_num = 0
abs_num_p = X + 1
abs_num_n = X - 1
while True:

    if abs_num_p in P:
        flag_p = True
    else:
        flag_p = False

    if abs_num_n in P:
        flag_n = True
    else:
        flag_n = False

    if(flag_p == True & flag_n == True):
        abs_num_p += 1
        abs_num_n -= 1
        continue;
    elif(flag_p == True & flag_n == False):
        print(abs_num_n)
        break;
    elif(flag_p == False):
        print(abs_num_p)
        break;


### FIXED CODE ###

X, N = input().split()
X = int(X)
N = int(N)

if N == 0:
    print(X)
    exit()

P = input().split()
for i in range(len(P)):
    P[i] = int(P[i])

if X not in P:
    print(X)
    exit()


abs_num_p = X + 1
abs_num_n = X - 1
while True:

    if abs_num_p in P:
        flag_p = True
    else:
        flag_p = False

    if abs_num_n in P:
        flag_n = True
    else:
        flag_n = False
    if(  flag_p == True  and flag_n == True ):
        abs_num_p += 1
        abs_num_n -= 1
        continue;
    elif(flag_p == True  and flag_n == False):
        print(abs_num_n)
        break;
    elif(flag_p == False and flag_n == True ):
        print(abs_num_p)
        break;
    elif(flag_p == False and flag_n == False):
        print(abs_num_n)
        break;

    i += 1
### BUGGY CODE ###

import sys
input = sys.stdin.readline
I=lambda:int(input())
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]

M=80*(H+W)
dp=[[[False]*(M+1) for _ in [0]*W] for _ in [0]*H]
dp[0][0][0]=True

m=M
for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in range(M+1):
            if dp[h][w][a]:
                #配る
                if h!=H-1:
                    if 0<=abs(a+d)<=M:
                        dp[h+1][w][abs(a+d)]=True
                        if (h+1)*w==(H-1)*(W-1):
                            m=min(abs(a+d),m)
                    if 0<=abs(a-d)<=M:
                        dp[h+1][w][abs(a-d)]=True
                        if (h+1)*w==(H-1)*(W-1):
                            m=min(abs(a-d),m)
                if w!=W-1:
                    if 0<=abs(a+d)<=M:
                        dp[h][w+1][abs(a+d)]=True
                        if h*(w+1)==(H-1)*(W-1):
                            m=min(abs(a+d),m)
                    if 0<=abs(a-d)<=M:
                        dp[h][w+1][abs(a-d)]=True
                        if h*(w+1)==(H-1)*(W-1):
                            m=min(abs(a-d),m)

res=dp[H-1][W-1][:]
for a in range(M+1):
    if dp[H-1][W-1][a]:
        d=A[H-1][W-1]-B[H-1][W-1]
        if 0<=abs(a+d)<=M:
            res[abs(a+d)]=True
            m=min(abs(a+d),m)
        if 0<=abs(a-d)<=M:
            res[abs(a-d)]=True
            m=min(abs(a-d),m)
            
print(m)

### FIXED CODE ###

import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]

G=[[set() for i in range(W)] for j in range(H)]
G[0][0].add(0)

for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in G[h][w]:
            if h!=H-1:
                G[h+1][w].add(abs(a+d))
                G[h+1][w].add(abs(a-d))
            if w!=W-1:
                G[h][w+1].add(abs(a+d))
                G[h][w+1].add(abs(a-d))

res=set()
d=A[H-1][W-1]-B[H-1][W-1]
for a in G[H-1][W-1]:
    res.add(abs(a+d))
    res.add(abs(a-d))

res=sorted(list(res))
print(min(res))