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

import itertools
import sys
 
N, M, R = map(int, sys.stdin.readline().split())
 
r = [i - 1 for i in map(int, sys.stdin.readline().split())]
 
d = [[float('inf')]*N for _ in range(N)]
 
for _ in range(M):
    a, b, c = map(int,sys.stdin.readline().split())
    d[a-1][b-1] = c
    d[b-1][a-1] = c
 
for a in range(N):
    d[a][a] = 0
 
res = float('inf')
 
for k in range(N):
    for i in range(N):
        for j in range(N):
            if d[i][k]+d[k][j] < d[i][j]:
                d[i][j] = d[i][k]+d[k][j]
 
for perm in itertools.permutations(r):
    a = 0
    for i in range(len(perm)-1):
        a += d[perm[i]][perm[i+1]]
    res = min(a, res)
 
print(res)

### FIXED CODE ###

from collections import defaultdict
from itertools import permutations
import heapq

INF = float('inf')
N, M, R = map(int, input().split())
r = list(map(int, input().split()))
memo = [[INF for _ in range(N)] for _ in range(N)]

Adj_list = defaultdict(set)

for i in range(M):
    a, b, c = map(int, input().split())
    if memo[a-1][b-1]:
        memo[a-1][b-1] = min(memo[a-1][b-1], c)
        
    Adj_list[a-1].add((b-1, memo[a-1][b-1]))
    Adj_list[b-1].add((a-1, memo[a-1][b-1]))


def dijkstra(x, vertex):
    d = [INF] * x
    d[vertex] = 0
    hq = [(0, vertex)]
    
    while hq:
        u = heapq.heappop(hq)[1]
        for (v, c) in Adj_list[u]:
            if d[v] > d[u] + c:
                d[v] = d[u] + c
                heapq.heappush(hq, (d[u]+c, v))

    return d

ans = INF
t_dis = 0

table = {}
for i in r:
    table[i-1] = dijkstra(N, i-1)

for each in permutations(r):
    t_dis = 0
    for i in range(R-1):
        t_dis += table[each[i]-1][each[i+1]-1]
    #print(each, t_dis)
    ans = min(ans, t_dis)

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

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

def calc(x):
    y = math.floor(A * x / B) - A * math.floor(x / B)
    return y

max_v = 0
for x in range(min(B,N+1)):
    max_v = max(max_v, calc(x))

print(max_v)


### FIXED CODE ###

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

def calc(x):
    y = math.floor(A * x / B) - A * math.floor(x / B)
    return y

max_v = 0

if N < B:
    max_v = calc(N)
else:
    max_v = calc(B-1)

# for x in range(N+1):
#     print(x,max_v) # x＝Bでさちる
#     max_v = max(max_v, calc(x))

print(max_v)
### BUGGY CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    H, N = map(int, input().split())
    AB = [tuple(map(int, input().split())) for _ in range(N)]

    dp = [10**9+1] * (H+1)
    dp[0] = 0
    for a, b in AB:
        for i in range(1, H+1):
            if i - a >= 0:
                v = dp[i-a] + b
            else:
                v = b
            if dp[i] > v:
                dp[i] = v

    print(dp[H])

if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
def input(): return sys.stdin.readline().rstrip()

def main():
    H, N = map(int, input().split())
    AB = [tuple(map(int, input().split())) for _ in range(N)]

    dp = [10**9+1] * (H+1)
    dp[0] = 0
    for a, b in AB:
        for i in range(1, H+1):
            if i - a >= 0:
                v = dp[i-a] + b
            else:
                v = b
            if dp[i] > v:
                dp[i] = v

    print(dp[H])

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

n, m = map(int, input().split())
edges = [tuple(map(int, input().split())) for _ in range(m)]

par = [i for i in range(n + 1)]
rank = [1 for _ in range(n + 1)]

def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

def same(x, y):
    return root(x) == root(y)

def unite(x, y):
    x = root(x)
    y = root(y)
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1

for a, b in edges:
    unite(a, b)
memo = []
ans = 0
for i in range(1, n + 1):
    if root(i) not in memo:
        memo.append(root(i))
        ans += 1
print(ans - 1)

### FIXED CODE ###

n, m = map(int, input().split())
edges = [tuple(map(int, input().split())) for _ in range(m)]

par = [i for i in range(n + 1)]
rank = [1 for _ in range(n + 1)]

def root(x):
    if par[x] == x:
        return x
    else:
        par[x] = root(par[x])
        return par[x]

def same(x, y):
    return root(x) == root(y)

def unite(x, y):
    x = root(x)
    y = root(y)
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1

for a, b in edges:
    unite(a, b)
memo = [False for _ in range(n + 1)]
ans = 0
for i in range(1, n + 1):
    r = root(i)
    if memo[r] == False:
        memo[r] = True
        ans += 1

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

import bisect

n = int(input())
a = list(map(int, input().split()))
a.sort()
k = len(a)
ok = [0] * k
for i in range(k):
    if i < k - 1 and a[i] == a[i + 1]:
        ok[i] = 1
        ok[i+1] = 1;
        continue
    times = 2
    while a[i] * times <= a[-1]:
        r = bisect.bisect_left(a, a[i] * times + 1)
        l = bisect.bisect_left(a, a[i] * times)
        if r != l:
            for idx in range(l, r):
                ok[idx] = 1
        times += 1
print(len([i for i in ok if i == 0]))
# print(ok) WAはどこ？


### FIXED CODE ###

import bisect

n = int(input())
a = list(map(int, input().split()))
a.sort()
k = len(a)
ok = [0] * k
for i in range(k):
    if i < k - 1 and a[i] == a[i + 1]:
        ok[i] = 1
        ok[i+1] = 1;
        continue

    if a[i] < 2 * 100:
        for j in range(i + 1, k):
            if a[j] % a[i] == 0:
                ok[j] = 1
    else:   
        times = 2
        while a[i] * times <= a[-1]:
            r = bisect.bisect_left(a, a[i] * times + 1)
            l = bisect.bisect_left(a, a[i] * times)
            if r != l:
                for idx in range(l, r):
                    ok[idx] = 1
            times += 1
print(len([i for i in ok if i == 0]))
# print(ok) WAはどこ？
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
b = list()
for i in range(n):
	if i % 2 == n % 2:
		b.append(a[i])
	else:
		b.insert(0, a[i])

for x in b:
	print(x, end=' ')
print()

### FIXED CODE ###

from collections import deque
d = deque()
n = int(input())
a = input().split()

for i in range(n):
	if i % 2 == n % 2:
		d.append(a[i])
	else:
		d.appendleft(a[i])

print(' '.join(d))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
s = 0
for i in range(len(a)-1):
  s_tmp = sum(a[i+1:len(a)])
  s += a[i] * s_tmp
print(s % 1000000007)

### FIXED CODE ###

n = int(input())
s = 0
a = list(map(int, input().split()))
sum_tmp = sum(a[1:len(a)])
for i in range(len(a)-1):
  s += a[i] * sum_tmp
  sum_tmp -= a[i + 1]
print(s % 1000000007)
### BUGGY CODE ###

N = int(input())
s = [sorted((input())) for i in range(N)]
count = 0

for i in range(N):
    for j in range(N):
        if(i<j):
            if(s[i]==s[j]):
                
                count += 1
print(count)

### FIXED CODE ###

N = int(input())
s = sorted(["".join(sorted((input()))) for i in range(N)])
count = 0

num=1
for i in range(N-1):
    if(s[i]==s[i+1]):
        num += 1
    elif(num>1):
        count += num*(num-1)/2
        num = 1
    
if(num>1):
    count += num*(num-1)/2
print(int(count))
### BUGGY CODE ###

n,k =map(int,input().split())
p = list(map(int,input().split()))
kitai = []
for i in range(1,n+1):
    sum_k = 0
    for j in range(1,p[i-1]+1):
        sum_k += j *(1/p[i-1])
    kitai.append(sum_k)
ans = []
for i in range(0,n-k+1):
    sum_ans = 0
    for j in range(k):
        sum_ans += kitai[j+i]
    ans.append(sum_ans)
print(max(ans))


### FIXED CODE ###

n,k =map(int,input().split())
p = list(map(int,input().split()))
kitai = []
for i in range(n):
    sum_k = 0
    sum_k = (1 + p[i])/2
    kitai.append(sum_k)
ans = []
sum_ans = 0
for i in range(k):
    sum_ans += kitai[i]
ans.append(sum_ans)
j = 0
cnt = 0
for i in range(k,len(kitai)):
    sum_ans = ans[cnt]
    sum_ans -=kitai[j] 
    sum_ans += kitai[i]
    j +=1
    cnt+=1
    ans.append(sum_ans)
print(max(ans))
### BUGGY CODE ###

import sys
input=sys.stdin.readline

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

a = []
i = 0
for h in range(H):
    s = list(input().strip())
    for c in s:
        if c == "#":
            a.append(i)
        i += 1
#print(a)

m = 0
for i in range(H*W):
    ni = H+W-2
    for j in a:
        ni = min(ni, abs(j//W-i//W) + abs(j%W -i%W))
#        print(ni, abs(j-i)//W, abs(j%W -i%W))
    m = max(m, ni)
print(m)


### FIXED CODE ###

import sys
input=sys.stdin.readline
H,W = map(int, input().split())

b = [[True]*(W+2)]
for _ in range(H):
    b.append([True] + [x == '#' for x in input().strip()] + [True])
b.append([True]*(W+2))

check = []
for i in range(1,H+1):
    for j in range(1,W+1):
        if b[i][j]:
            check.append((i,j))
t = 0
while check:
    tmp = []
    for i,j in check:
        if not b[i-1][j]:
            b[i-1][j] = True
            tmp.append((i-1,j))
        if not b[i][j-1]:
            b[i][j-1] = True
            tmp.append((i,j-1))
        if not b[i][j+1]:
            b[i][j+1] = True
            tmp.append((i,j+1))
        if not b[i+1][j]:
            b[i+1][j] = True
            tmp.append((i+1,j))
    check = tmp
    t += 1

print(t-1)
### BUGGY CODE ###

import sys
n = int(input())
ls = [int(input()) for _ in range(n)]
if 2 not in ls:
    print(-1)
    sys.exit()
i = 1
k = 0
di = []
while True:
    i = ls[i-1]
    if i in di:
        print(-1)
        sys.exit()
    di.append(i)
    k += 1
    if i == 2:
        break
print(k)

### FIXED CODE ###

import sys
n = int(input())
ls = [int(input()) for _ in range(n)]
if 2 not in ls:
    print(-1)
    sys.exit()
i = 1
k = 0
for j in range(n):
    i = ls[i-1]
    k += 1
    if i == 2:
        print(k)
        sys.exit()
print(-1)
### BUGGY CODE ###

import sys
input=sys.stdin.readline

def is_prime(n):
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            return False
    return True

def make(n):
    ans=[]
    for i in range(2,n+1):
        if is_prime(i):
            ans.append(i)
    return ans

if __name__ == '__main__':
    Q=int(input())
    Queries=[list(map(int,input().split())) for i in range(Q)]
    upper=max(Queries,key=lambda x:x[1])
    p_list=make(upper[1])

    for l,r in Queries:
        count=0
        for i in range(l,r+1,2):
            if i in p_list:
                if (i+1)//2 in p_list:
                    count+=1
        print(count)

### FIXED CODE ###

import sys
input=sys.stdin.readline

def Eratosthenes():
    num=[True for i in range(100001)]
    num[0]=False
    num[1]=False
    for i in range(2,100001):
        if num[i]:
            for j in range(2*i,100001,i):
                num[j]=False
    return num

if __name__ == '__main__':
    Q=int(input())
    Queries=[list(map(int,input().split())) for i in range(Q)]
    p_list=Eratosthenes()
    dp=[0 for _ in range(100001)]
    for i in range(3,100000):
        if p_list[i] and p_list[(i+1)//2]:
            dp[i+1]=dp[i]+1
        else:
            dp[i+1] = dp[i]
    for l,r in Queries:
        print(dp[r+1]-dp[l])
### BUGGY CODE ###

import statistics
import math

N = int(input())
a = input().split()
ans = 0
for i in range(0,N):
	a[i] = int(a[i])-i
ave = sum(a)/len(a)
for i in range(0,N):
	ans += abs(a[i]-int(statistics.median(a)))
print(int(ans))

### FIXED CODE ###

import statistics
import math

N = int(input())
a = input().split()
ans = 0
for i in range(0,N):
	a[i] = int(a[i])-i
med = int(statistics.median(a))
for i in range(0,N):
	ans += abs(a[i]-med)
print(int(ans))
### BUGGY CODE ###

N,W=map(int,input().split())
MV=[list(map(int, input().split())) for _ in range(N)]
dp=[0]*(W+1)

for w,v in MV:
    for tw in range(W,w-1,-1):
        dp[tw]=max(dp[tw],dp[tw-w]+v)
        #print(dp,tw)

print(max(dp))

### FIXED CODE ###

N,W=map(int,input().split())
MV=[list(map(int, input().split())) for _ in range(N)]
dp=[0]*(W+1)

for w,v in MV:
    for tw in range(W,w-1,-1):
        dp[tw]=max(dp[tw],dp[tw-w]+v)
        #print(dp,tw)

print(max(dp))
### BUGGY CODE ###

X = int(input())
Acount = 0
Bcount = 0
di= {}
def func(i):
    return i**5
for i in range(5000):
    di[str(func(i))] = str(i)
preA = int(X **(1/5))
while True:
    funcA = preA**5
    if funcA-X>=0 and str(funcA-X) in di.keys():
        print(str(preA)+" "+di[str(funcA-X)])
        break
    elif X-funcA>= 0 and str(X-funcA) in di.keys():
        print(str(preA)+" -"+di[str(X-funcA)])
        break
    preA += 1

### FIXED CODE ###

X = int(input())
Acount = 0
Bcount = 0
di= {}
def func(i):
    return i**5
for i in range(5000):
    di[str(func(i))] = str(i)
preA = int(X **(1/5))-1000
while True:
    funcA = preA**5
    if funcA-X>=0 and str(funcA-X) in di.keys():
        print(str(preA)+" "+di[str(funcA-X)])
        break
    elif X-funcA>= 0 and str(X-funcA) in di.keys():
        print(str(preA)+" -"+di[str(X-funcA)])
        break
    preA += 1
### BUGGY CODE ###

N, K = map(int, input().split())
A = [int(i) for i in input().split()]

MOD = 10**9 + 7

ans = 0
for i in range(N):
    for j in range(N):
        l, r = 0, 0
        if A[i] > A[j] and i < j:
            l += 1
        if A[i] > A[j] and i > j:
            r += 1
        ans += (1+K)*K//2*l
        ans += K*(K-1)//2*r
        ans %= MOD

print(ans)


### FIXED CODE ###

N, K = map(int, input().split())
A = [int(i) for i in input().split()]

MOD = 10**9 + 7

ans = 0
for i in range(N):
    for j in range(N):
        l, r = 0, 0
        if A[i] > A[j] and i < j:
            l += 1
        if A[i] > A[j] and i > j:
            r += 1
        ans += (1+K)*K//2*l
        ans += K*(K-1)//2*r
        ans %= MOD

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

from functools import reduce

def solve():
    MOD = 10**9 + 7

    def gcd(a, b):
        while b:
            a, b = b, a%b
        return a
    def lcm(a, b):
        return a // gcd(a, b) * b

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

    lcmA = reduce(lcm, As)
    #print('lcmA:', lcmA)

    ans = 0
    for A in As:
        ans += lcmA // A
        ans %= MOD

    print(ans)

solve()


### FIXED CODE ###

from collections import Counter

def solve():
    MOD = 10**9 + 7
    maxA = 10**6

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

    isPrimes = [False]*2 + [True]*(maxA-1)
    minFs = [1] * (maxA+1)
    for i in range(2, int(maxA**0.5) + 1):
        if isPrimes[i]:
            for x in range(i*i, maxA+1, i):
                isPrimes[x] = False
                minFs[x] = i

    def getPrimeFactors(x):
        anss = []
        while minFs[x] > 1:
            anss.append(minFs[x])
            x //= minFs[x]
        if x != 1:
            anss.append(x)
        return anss

    PFss = []
    Primes = set()
    for A in As:
        PFs = getPrimeFactors(A)
        cnt = Counter(PFs)
        PFss.append(cnt)
        Primes |= cnt.keys()

    cntP = Counter()
    for PFs in PFss:
        for key, num in PFs.items():
            cntP[key] = max(cntP[key], num)

    lcmA = 1
    for key, num in cntP.items():
        lcmA *= pow(key, num, MOD)
        lcmA %= MOD

    def getInvs(n, MOD):
        invs = [1] * (n+1)
        for x in range(2, n+1):
            invs[x] = (-(MOD//x) * invs[MOD%x]) % MOD
        return invs

    invs = getInvs(10**6, MOD)

    ans = 0
    for A in As:
        ans += lcmA * invs[A]
        ans %= MOD

    print(ans)

solve()
### BUGGY CODE ###

import collections
N = int(input())
A = list(map(int,input().split()))
for i in range(N):
    x = A[i]
    A[i] = 0
    CA = collections.Counter(A)
    B = CA.most_common()
    S = 0
    for j in range(len(B)):
        L = B[j][1]
        if L == 1:
            break
        S += int(L*(L-1)/2)
    print(S)
    A[i] = x

### FIXED CODE ###

import collections
N = int(input())
A = list(map(int,input().split()))
CA = collections.Counter(A)
S = 0
for i in CA.values():
    S += i*(i-1)//2
for x in A:
    print(S - CA[x] + 1)
### BUGGY CODE ###

import sys,math,copy
# import numpy as np
input = sys.stdin.readline
S = reversed(input().strip())

# dp = np.zeros((13), dtype=int)
# dp_ = np.zeros((13), dtype=int)
dp = [0] * 13
dp_ = [0] * 13
mod13_list = [i % 13 for i in range(12 ** 2 + 1)]
list10 = list(range(10))
list13 = list(range(13))
divisor = 10 ** 9 + 7
dp_[0] = 1
mul = 1
for s in S:
    if s == '?':
        for d in list10:
            m = mod13_list[(d * mul)]
            for pm in list13:
                dp[mod13_list[(pm+m)]] += dp_[pm]

    else:
        si = int(s)
        m = mod13_list[(si * mul)]
        for pm in list13:
            dp[mod13_list[(pm+m)]] = dp_[pm]

    # dp_ = dp % divisor
    # dp = dp * 0 # np.zeros((13), dtype=int)
    for i in list13:
        dp_[i] = dp[i] % divisor
        dp[i] = 0
    mul = mod13_list[(mul * 10)]
print(dp_[5])

### FIXED CODE ###

import sys,math,copy

def main():
    # import numpy as np
    input = sys.stdin.readline
    S = ''.join(reversed(input().strip()))

    # dp = np.zeros((13), dtype=int)
    # dp_ = np.zeros((13), dtype=int)
    dp = [0] * 13
    dp_ = [0] * 13
    mod13_list = [i % 13 for i in range(12 ** 2 + 1)]
    list10 = list(range(10))
    list13 = list(range(13))
    divisor = 10 ** 9 + 7
    dp_[0] = 1
    mul = 1
    for i in range(len(S)):
        s = S[i]
        if s == '?':
            for d in list10:
                m = mod13_list[(d * mul)]
                for pm in list13:
                    dp[mod13_list[(pm+m)]] += dp_[pm]

        else:
            si = int(s)
            m = mod13_list[(si * mul)]
            for pm in list13:
                dp[mod13_list[(pm+m)]] = dp_[pm]

        # dp_ = dp % divisor
        # dp = dp * 0 # np.zeros((13), dtype=int)
        for j in list13:
            dp_[j] = dp[j] % divisor
            dp[j] = 0
        mul = mod13_list[(mul * 10)]
    print(dp_[5])

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

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

dp = [float('inf')] * N
dp[0] = 0
for i in range(1,len(A)):
    jmp = min(i,K)
    for j in range(1,jmp+1):
        # dp[i] = min(dp[i], dp[i-j]+abs(A[i]-A[i-j]))
        dp[i] = min([dp[i-j]+abs(A[i]-A[i-j]) for j in range(1, jmp+1)])
            
print(dp[N-1])

### FIXED CODE ###

def main():
    N,K=map(int, input().split())
    A = list(map(int, input().split()))

    dp = [float('inf')] * N
    dp[0] = 0
    for i in range(1,len(A)):
        jmp = max(0,i-K)
        dp[i] = min([dp[j]+abs(A[i]-A[j]) for j in range(jmp, i)])
    print(dp[N-1])

main()
### BUGGY CODE ###

from collections import deque


def solve():
    n = int(input())
    a = list(map(int, input().split(" ")))
    d = deque()
    for i in range(n):
        d.append(a[i])
        d.reverse()

    for element in list(d):
        print(element, end = " ")


solve()

### FIXED CODE ###

from collections import deque


def solve():
    n = int(input())
    a = list(map(str, input().split(" ")))
    ans = deque()

    i = 0
    if n % 2:
        while i != n:
            if i % 2:
                ans.append(a[i])
            else:
                ans.appendleft(a[i])
            i += 1
    else:
        while i != n:
            if i % 2:
                ans.appendleft(a[i])
            else:
                ans.append(a[i])
            i += 1

    print(" ".join(ans))


solve()
### BUGGY CODE ###

# いくつか入力
n,p = (int(i) for i in input().split())
# 空白区切りの数列をリストに代入
s = [int(i) for i in list(input())] 

answer = 0
#print(s)
for i in range(n):
    q=0
    for j in range(i,n):
        q=q*10+s[j]
        if q%p==0:
            answer += 1
print(answer)



### FIXED CODE ###

#
import sys
import math
#import numpy as np
import itertools
from collections import Counter
# いくつか入力
n,p = (int(i) for i in input().split())
# 空白区切りの数列をリストに代入
s = [int(i) for i in list(input())] 

answer = 0
#print(s)
q=0
x=[]
if p == 2 or p == 5:
    for i in range(n-1,-1,-1):
        if s[i]%p == 0:
            answer += i+1
else:
    d=1
    for i in range(n-1,-1,-1):
        q = (d*s[i]+q)%p
        x.append(q)
        d = (d * 10)%p
    #print(x)
    counter = Counter(x)
    #print(counter)

    for k in counter:
    #    print(k)
        if k==0:
            answer += counter[k] * (counter[k]+1) // 2
        else:
            answer += counter[k] * (counter[k]-1) // 2

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

H,W = map(int,input().split())
A = [list(map(int,input().split()))for _ in range(H)]
for i in range(H):
    b = list(map(int,input().split()))
    for j in range(W):
        A[i][j] = abs(A[i][j]-b[j])
NUM = 20000
DP = [[[0]*NUM for i in range(W)]for j in range(H)]
DP[0][0][A[0][0]] = 1
i = 0
for j in range(W-1):
    a = A[i][j+1]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i][j+1][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i][j+1][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i][j+1][k+a] = 1
j = 0
for i in range(H-1):
    a = A[i+1][j]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i+1][j][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i+1][j][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i+1][j][k+a] = 1
for i in range(H-1):
    for j in range(W-1):
        a = A[i+1][j+1]
        for k in range(NUM):
            if DP[i+1][j][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1
            if DP[i][j+1][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1

print(DP[-1][-1].index(1))

### FIXED CODE ###

H,W = map(int,input().split())
A = [list(map(int,input().split()))for _ in range(H)]
for i in range(H):
    b = list(map(int,input().split()))
    for j in range(W):
        A[i][j] = abs(A[i][j]-b[j])
NUM = 6400
DP = [[[0]*NUM for i in range(W)]for j in range(H)]
DP[0][0][A[0][0]] = 1
i = 0
for j in range(W-1):
    a = A[i][j+1]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i][j+1][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i][j+1][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i][j+1][k+a] = 1
j = 0
for i in range(H-1):
    a = A[i+1][j]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i+1][j][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i+1][j][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i+1][j][k+a] = 1
for i in range(H-1):
    for j in range(W-1):
        a = A[i+1][j+1]
        for k in range(NUM):
            if DP[i+1][j][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1
            if DP[i][j+1][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1

print(DP[-1][-1].index(1))