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

N = int(input())
A = list(map(int, input().strip().split()))
from collections import Counter
C=Counter(A)
K=set(A)

culc = {}
# print(K)
# print(C)

for n in K:
    tmp = 0
    # print("n=",n)
    for i in C:
        if C[i] != 1:
            if i == n and C[i]-1 == 1:
                continue
            elif i == n :
                tmp += (C[i]-1)*(C[i]-2)//2
            else:
                tmp += C[i]*(C[i]-1)//2
    #     print("C=",i,"count",C[i],tmp)
    # print("K=",n,"の時、",tmp)
    culc[n]=tmp

# print(culc)

for i in range(N):
    print(culc[A[i]])
    

### FIXED CODE ###

N = int(input())
A = list(map(int, input().strip().split()))
from collections import Counter
C=Counter(A)
K=set(A)
ans = 0
for i in K:
    ans += C[i]*(C[i]-1)//2

for i in A:
    print(ans-(C[i]-1)) # nシグマk = n(n-1)/2 ......
### BUGGY CODE ###

N, K = list(map(int,input().split()))
 
A = list(map(int,input().split()))
 
for i in range(N - K):
  list_a = list(A[i:i+K])
  list_b = list(A[i+1:i+K+1])
  if list_a[0] < list_b[-1]:
    print("Yes")
  else:
    print("No")

### FIXED CODE ###

N, K = list(map(int,input().split()))
 
A = list(map(int,input().split()))
 
for i in range(N - K):
  if A[i] < A[i + K]:
    print("Yes")
  else:
    print("No")
### BUGGY CODE ###

from collections import deque
H,W,K= map(int,input().split())
sx,sy,gx,gy = (i-1 for i in map(int,input().split()))
grid = []
inf = 1<<30
reach = [[inf for _ in range(W)] for _ in range(H)]
reach[sx][sy]=0
dif = [(1,0),(-1,0),(0,1),(0,-1)] #向かう方向

for i in range(H):
    grid.append(list(input()))

que = deque([(sx,sy)])
while que:
    x,y = que.popleft()
    for i in range(4): #方向決め
        for k in range(1,K+1):
            nx = x+k*dif[i][0]
            ny = y+k*dif[i][1]
            if nx<0 or nx>=H or ny<0 or ny>= W or grid[nx][ny] == "@":
                break
            if reach[x][y]+1 < reach[nx][ny]:
                reach[nx][ny] = min(reach[x][y]+1,reach[nx][ny])
                que.append((nx,ny))
            if nx == gx and ny == gy:
                print(reach[nx][ny])
                exit()
print(-1)


### FIXED CODE ###

from collections import deque
H,W,K= map(int,input().split())
sx,sy,gx,gy = (i-1 for i in map(int,input().split()))
grid = []
inf = 1<<30
reach = [[inf for _ in range(W)] for _ in range(H)]
reach[sx][sy]=0
dif = [(1,0),(-1,0),(0,1),(0,-1)] #向かう方向

for i in range(H):
    grid.append(list(input()))

que = deque([(sx,sy)])
while que:
    x,y = que.popleft()
    for i in range(4): #方向決め
        for k in range(1,K+1):
            nx = x+k*dif[i][0]
            ny = y+k*dif[i][1]
            if nx<0 or nx>=H or ny<0 or ny>= W or grid[nx][ny] == "@":
                break
            if reach[x][y]+1 > reach[nx][ny]:
                break
            if reach[x][y]+1 < reach[nx][ny]:
                reach[nx][ny] = min(reach[x][y]+1,reach[nx][ny])
                que.append((nx,ny))
            if nx == gx and ny == gy:
                print(reach[nx][ny])
                exit()
print(-1)
### BUGGY CODE ###

n, k, q = map(int, input().split())
#print(n,k,q)
l = [0] * n
#print(l)
katen = []
for i in range(q):
    katen.append(int(input()))
#print(katen)
for j in katen:
    x = l.pop(j-1)
    l.insert(j-1, x+1)
#print(l)

for y in l:
    if y > q-k:
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

n, k, q = map(int, input().split())
#print(n,k,q)
l = [0] * n
#print(l)
katen = []
my_append = katen.append
for i in range(q):
    my_append(int(input()))
#print(katen)

    
for j in katen:
    l[j-1] += 1 

    
#print(l)

for y in range(n):
    if l[y] > q-k:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

N = int(input())

if N%2 == 0:
  print(N, N, N//2)
else:
  for h in range(1, 3501):
    for n in range(1, 3501):
      num = N*h*n
      dem = 4*h*n - N*(h+n)
      if dem > 0 and num%dem == 0:
        print(h, n, num//dem)
        quit()

### FIXED CODE ###

N = int(input())

if N%2 == 0:
  print(N, N, N//2)
else:
  for h in range(1, 3501):
    for n in range(1, 3501):
      num = N*h*n
      dem = 4*h*n - N*(h+n)
      if dem > 0 and num%dem == 0:
        print(h, n, num//dem)
        quit()
### BUGGY CODE ###

N = int(input())
points = [list(map(int, input().split())) for _ in range(N)]

possible = 1
for i in range(N):
  if i == 0:
    parity_0 = sum(points[i]) % 2
  else:
    if sum(points[i]) % 2 != parity_0:
      print(-1)
      possible = 0
      break

if possible:
  farthest = max([abs(point[0]) + abs(point[1]) for point in points])
  print(farthest)
  d = ""
  for i in range(farthest):
    if i < farthest - 1:
      d = d + "1" + " "
    else:
      d = d + "1"
  print(d)
  for i in range(N):
    w = ""
    X, Y = points[i][0], points[i][1]
    if X > 0:
      w += "R" * X
    else:
      w += "L" * abs(X)
    if Y > 0:
      w += "U" * Y
    else:
      w += "D" * abs(Y)
    w += "UD" * int((farthest - abs(X) - abs(Y)) / 2)
    print(w)

### FIXED CODE ###

import math

N = int(input())
points = [list(map(int, input().split())) for _ in range(N)]

possible = 1
for i in range(N):
  if i == 0:
    parity_0 = sum(points[i]) % 2
  else:
    if sum(points[i]) % 2 != parity_0:
      print(-1)
      possible = 0
      break

if possible:
  farthest = max([abs(point[0]) + abs(point[1]) for point in points])
  m = math.ceil(math.log2(farthest + 1))
  d_list = [2**(m-i-1) for i in range(m)]
  if parity_0 == 0:
    m = m + 1
    d_list.append(1)
  print(m)
  print(" ".join(map(str,d_list)))
  uv = [[point[0] + point[1], point[0] - point[1]] for point in points]
  for i in range(N):
    u = uv[i][0]
    v = uv[i][1]
    w = ""
    for d in d_list:
      if u > 0:
        if v > 0:
          w += "R"
          u -= d
          v -= d
        else:
          w += "U"
          u -= d
          v += d
      else:
        if v > 0:
          w += "D"
          u += d
          v -= d
        else:
          w += "L"
          u += d
          v += d
    print(w)
### BUGGY CODE ###

import bisect

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort()
b.sort()
c.sort()

ans = 0
for i in a:
  bi = bisect.bisect_right(b,i)
  for j in b[bi:]:
    ci = bisect.bisect_right(c,j)
    ans += (n-ci)
print(ans)

### FIXED CODE ###

import bisect

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
a.sort()
b.sort()
c.sort()

ans = 0
for i in b:
  ai = bisect.bisect_left(a,i)
  ci = bisect.bisect_right(c,i)
  ans += ai*(n-ci)
print(ans)
### BUGGY CODE ###

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

for i in range(M):
  index = A.index(max(A))
  A[index] = A[index] // 2
  
print(sum(A))

### FIXED CODE ###

import heapq
import math

N, M = map(int, input().split())
A = list(map(int, input().split()))
A = [i*-1 for i in A]

heapq.heapify(A)

for i in range(M):
  kk = math.ceil(heapq.heappop(A) / 2)
  heapq.heappush(A, kk)

A = [i*-1 for i in A]

print(sum(A))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]

def main():
    n = int(input())
    xy = [list(map(int,input().split())) for i in range(n)]
    xy2 = [[x[0],x[1],i] for (i,x) in enumerate(xy)]
    xy2.sort()
    u = Unionfind(n)

    s = set()
    for i,X in enumerate(xy2):
        x,y,ind = X
        check = True
        cand = []
        mi = -1
        count = n+5
        for j in s:
            if y > xy[j][1]:
                check = False
                u.union(ind,j)
                if count > xy[j][1]:
                    count = xy[j][1]
                    mi = j
            
        if check:
            s.add(ind)
        else:
            for j in cand:
                if j == mi:
                    continue
                del s[j]
    for i in range(n):
        print(u.size(i))

if __name__ == "__main__":
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]

def main():
    n = int(input())
    xy = [list(map(int,input().split())) for i in range(n)]
    xy2 = [[x[0],x[1],i] for (i,x) in enumerate(xy)]
    xy2.sort()
    u = Unionfind(n)

    l = []
    for x,y,ind in xy2:
        M = y
        le = ind
        while l and xy[l[-1]][1] < y:
            v = l.pop()
            if M > xy[v][1]:
                M = xy[v][1]
                le = v
            u.union(ind,v)
        l.append(le)
    for i in range(n):
        print(u.size(i))

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

from collections import deque

n,m = map(int,input().split())
e = [[] for i in range(n)]

for i in range(m):
    a,b,c = map(int,input().split())
    a -= 1
    b -= 1
    e[a].append([b,c])
    e[b].append([a,-c])


dis = [False]*n

def search(x):

    q = deque([])
    dis[x] = 0
    q.append(x)
    while q:
        now = q.pop()
        for nex,d in e[now]:
            if dis[nex] == False:
                dis[nex] = dis[now]+d
                q.append(nex)
            else:
                if dis[nex] != dis[now]+d:
                    print("No")
                    exit()
        
for i in range(n):
    if e[i] and dis[i] == False:
        search(i)
print("Yes")

### FIXED CODE ###

def main():    
    from collections import deque
    import sys
    input = sys.stdin.readline
    n,m = map(int,input().split())
    e = [[] for i in range(n)]

    for i in range(m):
        a,b,c = map(int,input().split())
        a -= 1
        b -= 1
        e[a].append([b,c])
        e[b].append([a,-c])


    dis = [float("INF")]*n

    def search(x):

        q = deque([])
        dis[x] = 0
        q.append((x,0))
        while q:
            now,cnt = q.popleft()
            for nex,d in e[now]:
                if dis[nex] == float("INF"):
                    dis[nex] = cnt+d
                    q.append((nex,cnt+d))
                else:
                    if dis[nex] != cnt+d:
                        print("No")
                        exit()
            
    for i in range(n):
        if e[i] and dis[i] == float("INF"):
            search(i)
    print("Yes")

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

l,r = map(int,input().split())
if r-l<1:
    print(0)
else:
    con = list()
    for i in range(l,r):
        for j in range(i+1,r+1):
            con.append((i*j)%2019)
    print(min(con))

### FIXED CODE ###

l,r = map(int,input().split())
if r-l>=2019:
    print(0)
else:
    res = 2019
    for i in range(l,r):
        for j in range(i+1,r+1):
            res = min((i*j)%2019,res)
    print(res)
### BUGGY CODE ###

N = int(input())
a = [''.join(sorted(list(input()))) for i in range(N)]
b = 0
for i in a:
 b = b + a.count(i) - 1
print(int(b / 2))

### FIXED CODE ###

from collections import Counter

N = int(input())
a = [''.join(sorted(input())) for i in range(N)]
count = Counter(a).values()
b = 0
for i in count:
    b = b + i * (i-1)
b=int(b/2)
print(b)
### BUGGY CODE ###

import sys
input = sys.stdin.readline
from collections import deque

h, w = map(int, input().split())
ch, cw = map(int, input().split())
gh, gw = map(int, input().split())

s = []
for i in range(h):
    s.append(input().rstrip('\n'))

d = [[10 ** 18 for i in range(w)] for j in range(h)]

stack = deque([[ch-1, cw-1, 0]])

while stack:
    curh, curw, cnt = stack.popleft()
    if d[curh][curw] <= cnt:
        continue
    else:
        d[curh][curw] = cnt
    for dh, dw in ((-1,0), (1,0), (0,1), (0,-1)):
        nh = curh + dh
        nw = curw + dw
        if nh >= 0 and nh < h and nw >= 0 and nw < w and s[nh][nw] == '.':
            stack.append([nh, nw, cnt])
    for nh in range(curh-2, curh+3):
        if nh >= 0 and nh < h:
            for nw in range(curw-2,curw+3):
                if nw >= 0 and nw < w and s[nh][nw] == '.':
                     stack.append([nh, nw, cnt+1])

# for item in d:
#     print(item)

if d[gh-1][gw-1] == 10 ** 18:
    print(-1)
else:
    print(d[gh-1][gw-1])

### FIXED CODE ###

import sys
input = sys.stdin.readline

import heapq

INF = 10 ** 10

def dijkstra(n, G, s):
    dist = [INF] * n
    dist[s] = 0
    h = []
    heapq.heappush(h, (0, s))

    while h:
        # 使っていない頂点のうち、現時点で最も距離の近いものを選びvとする
        d, v = heapq.heappop(h)
        if dist[v] < d:
            continue
        # vから到達可能な頂点について、距離が短くなれば更新
        for end, cost in G[v]:
            if dist[end] > dist[v] + cost:
                dist[end] = dist[v] + cost
                heapq.heappush(h, (dist[end], end))
    return dist


H, W = map(int, input().split())
N = H * W
ch, cw = map(lambda x:int(x)-1, input().split())
gh, gw = map(lambda x:int(x)-1, input().split())

s = []
es = tuple([] for i in range(N))

for h in range(H):
   s.append(input().rstrip('\n'))

for h in range(H):
    for w in range(W):
        if s[h][w] != '.':
            continue
        a = h*W+w
        esappend = es[a].append
        for dh in (-2, -1, 0, 1, 2):
            for dw in (-2, -1, 0, 1, 2):
                if dh == 0 and dw == 0:
                    continue
                nh = h + dh
                nw = w + dw
                if 0 <= nh and nh < H and 0 <= nw and nw < W and s[nh][nw] == '.':
                    b = nh*W+nw
                    if (dh, dw) in [(-1,0),(1,0),(0,-1),(0,1)]:
                        esappend((b, 0))
                    else:
                        esappend((b, 1))

dist = dijkstra(N, es, ch*W+cw)
if dist[gh*W+gw] == INF:
    print(-1)
else:
    print(dist[gh*W+gw])
### BUGGY CODE ###

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

dp = [i for i in range(N+1)]

for i in range(1,N+1):
    for j in range(3):
        if i-A[j] >= 0:
            dp[i] = max(dp[i],dp[i-A[j]]+B[j])

M = dp[N]            
dp = [i for i in range(M+1)]

for i in range(1,M+1):
    for j in range(3):
        if i-B[j] >= 0:
            dp[i] = max(dp[i],dp[i-B[j]]+A[j])

print(dp[M])

### FIXED CODE ###

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

dp = [0]*(N+1)
for j in range(3):
    for i in range(1,N+1):
        if i-A[j] >= 0:
            dp[i] = max(dp[i],dp[i-1]+1,dp[i-A[j]]+B[j])
        else:
            dp[i] = max(dp[i],dp[i-1]+1)

M = dp[N]            
dp = [0]*(M+1)

for j in range(3):
    for i in range(1,M+1):
        if i-B[j] >= 0:
            dp[i] = max(dp[i],dp[i-1]+1,dp[i-B[j]]+A[j])
        else:
            dp[i] = max(dp[i],dp[i-1]+1)
    
print(dp[M])
### BUGGY CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

S = input().rstrip()
T = input().rstrip()

S,T

# Sの方が長くする
LS = len(S)
LT = len(T)
n = (LT + (-LT) % LS) // LS
S *= (n+1)
S = S[:LS+LT]

MOD = 10 ** 16 + 61
base = 12345
base_inv = pow(base,MOD-2,MOD) 
power = [1] * (LS+LT)
power_inv = [1] * (LS+LT)
for n in range(1,LS+LT):
    power[n] = power[n-1] * base % MOD
    power_inv[n] = power_inv[n-1] * base_inv % MOD

def to_rolling_hash(S):
    ret = [0] * len(S)
    for i,s in enumerate(S):
        ret[i] = (ret[i-1] + power[i] * ord(s)) % MOD
    return ret

S_hash = to_rolling_hash(S) + [0]
T_hash = to_rolling_hash(T)[-1] # 文字列全体

can_start = [(S_hash[i + LT - 1] - S_hash[i - 1]) * power_inv[i] % MOD == T_hash for i in range(LS)]

INF = 10 ** 18
visited = [False] * LS
dp = [None] * LS

def dfs(i):
    if visited[i]:
        if dp[i] is None:
            # ループ
            return INF
        return dp[i]
    visited[i] = True
    if not can_start[i]:
        dp[i] = 0
        return 0
    x = dfs((i + LT) % LS) + 1
    dp[i] = x
    return x

answer = max(dfs(i) for i in range(LS))

if answer >= INF:
    answer = -1

print(answer)

### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

S = input().rstrip()
T = input().rstrip()

S,T

# Sの方が長くする
LS = len(S)
LT = len(T)
n = (LT + (-LT) % LS) // LS
S *= (n+1)
S = S[:LS+LT]

MOD = 10 ** 16 + 61
base = 12345
base_inv = pow(base,MOD-2,MOD) 
power = [1] * (LS+LT)
power_inv = [1] * (LS+LT)
for n in range(1,LS+LT):
    power[n] = power[n-1] * base % MOD
    power_inv[n] = power_inv[n-1] * base_inv % MOD

def to_rolling_hash(S):
    ret = [0] * len(S)
    for i,s in enumerate(S):
        ret[i] = (ret[i-1] + power[i] * ord(s)) % MOD
    return ret

S_hash = to_rolling_hash(S) + [0]
T_hash = to_rolling_hash(T)[-1] # 文字列全体

can_start = [(S_hash[i + LT - 1] - S_hash[i - 1]) * power_inv[i] % MOD == T_hash for i in range(LS)]

INF = 10 ** 18
visited = [False] * LS
dp = [None] * LS

arr = [INF] * LS

q = [(i,0) for i,bl in enumerate(can_start) if not bl]

while q:
    qq = []
    for x,d in q:
        if arr[x] == INF:
            arr[x] = d
            qq.append(((x-LT)%LS, d+1))
    q = qq

answer = max(arr)

if answer >= INF:
    answer = -1

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

if __name__ == '__main__':
    n = int(input())
    lis = []
    for i in range(n):
        lis.append(input())
        lis=list(set(lis))
    print(len(lis))

### FIXED CODE ###

if __name__ == '__main__':
    n = int(input())
    print(len(set(input().rstrip('\n') for _ in range(n))))
### BUGGY CODE ###

def main():
    from bisect import bisect_right
    from collections import namedtuple
    from operator import attrgetter
    import sys
    input = sys.stdin.readline

    Param = namedtuple('Param', 'coef const')

    inf = 1 << 30
    L = 30

    N, T = map(int, input().split())

    ps = []
    coef_0 = []
    for _ in range(N):
        a, b = map(int, input().split())
        p = Param(coef=a, const=b)
        if a == 0:
            coef_0.append(p)
        else:
            ps.append(p)

    ps.sort(key=lambda p: p.coef / (p.const + 1), reverse=True)
    coef_0.sort(key=attrgetter('const'))

    dp = [inf] * (L + 1)
    dp[0] = 0
    for p in ps:
        for visited in reversed(range(L)):
            dp[visited + 1] = min(
                dp[visited + 1],
                dp[visited] + 1 + (dp[visited] + 1) * p.coef + p.const
            )

    def accumulate(a):
        s = 0
        for x in a:
            s += x.const + 1
            yield s

    *acc, = accumulate(coef_0)

    ans = -1
    for visited in range(L + 1):
        rest = T - dp[visited]
        if rest < 0: continue  # le->lt
        cnt = bisect_right(acc, rest)
        ans = max(ans, visited + cnt)
    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

# https://atcoder.jp/contests/hitachi2020/submissions/10704509

def main():
    import sys
    input = sys.stdin.readline

    inf = 1 << 30
    L = 30

    N, T = map(int, input().split())

    ps = []
    coef_0 = []
    for _ in range(N):
        a, b = map(int, input().split())
        if a == 0:
            coef_0.append(b + 1)
        else:
            ps.append((a, b))  # namedtupleやめた,coef,const

    ps.sort(key=lambda p: p[0] / (p[1] + 1), reverse=True)
    coef_0.sort()

    dp = [inf] * (L + 1)
    dp[0] = 0
    for coef, const in ps:
        for visited in reversed(range(L)):
            dp[visited + 1] = min(
                dp[visited + 1],
                (dp[visited] + 1) * (coef + 1) + const
            )

    def accumulate(a):
        s = 0
        yield s  # start=x->0
        for x in a:
            s += x
            yield s

    *acc, = accumulate(coef_0)

    ans = {0}
    cnt = len(acc) - 1
    for visited, consumption_time in enumerate(dp):
        rest = T - consumption_time
        if rest < 0: break  # le->lt,continue->break,restは狭義単調減少
        while cnt > 0 and acc[cnt] > rest:  # bisect_right->しゃくとり
            cnt -= 1
        ans.add(visited + cnt)
    print(max(ans))


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

import sys
input = sys.stdin.readline

H,W=map(int,input().split())

A=[list(map(int,input().split())) for i in range(H)]
B=[list(map(int,input().split())) for i in range(H)]

SA=[[0]*W for i in range(H)]

for i in range(H):
    for j in range(W):
        SA[i][j]=abs(A[i][j]-B[i][j])

DP=[[0 for i in range(W)] for i in range(H)]

DP[0][0]=1<<SA[0][0]

def next(i,j):
    RET=[]
    if i+j<=6400:
        RET.append(i+j)
    if abs(i-j)<=6400:
        RET.append(abs(i-j))

    return RET

for i in range(H):
    for j in range(W):
        for k in range(6400):
            if 1<<k & DP[i][j]==0:
                continue
            if i+1<H:
                for r in next(k,SA[i+1][j]):
                    DP[i+1][j]|=1<<r

            if j+1<W:
                for r in next(k,SA[i][j+1]):
                    DP[i][j+1]|=1<<r
                
for i in range(6401):
    if 1<<i & DP[-1][-1]!=0:
        print(i)
        break


### FIXED CODE ###

import sys
input = sys.stdin.readline

H,W=map(int,input().split())

A=[list(map(int,input().split())) for i in range(H)]
B=[list(map(int,input().split())) for i in range(H)]

SA=[[0]*W for i in range(H)]

for i in range(H):
    for j in range(W):
        SA[i][j]=abs(A[i][j]-B[i][j])

DP=[[0 for i in range(W)] for j in range(H)]

DP[0][0]=1<<(H+W)*80+SA[0][0]

for i in range(H):
    for j in range(W):
        tempbit=DP[i][j]
        if i+1<H:
            DP[i+1][j]|=tempbit<<SA[i+1][j]
            DP[i+1][j]|=tempbit>>SA[i+1][j]

        if j+1<W:
            DP[i][j+1]|=tempbit<<SA[i][j+1]
            DP[i][j+1]|=tempbit>>SA[i][j+1]

ANS=1<<30
for i in range((H+W)*160+1):
    if 1<<i & DP[-1][-1]!=0:
        ANS=min(ANS,abs(i-(H+W)*80))

print(ANS)