CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.readline
I=lambda:int(eval(input()))
MI=lambda:list(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)

# Optimized solution
import sys
input = sys.stdin.readline
MI=lambda:list(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)))
# Original solution
import sys
def main():
    input = sys.stdin.readline
    H,W = list(map(int, input().split()))
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]

    S = (80+80)*80//2
    dp = [[[0]*(S+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    for i in range(H):
        for j in range(W):
            for s in range(S+1):
                for ni,nj in [(i,j+1),(i+1,j)]:
                    if ni >= H or nj >= W: continue
                    t = abs(A[ni][nj] - B[ni][nj])
                    if s+t <= S:
                        dp[ni][nj][s+t] |= dp[i][j][s]
                    dp[ni][nj][abs(s-t)] |= dp[i][j][s]
    for s in range(S+1):
        if dp[H-1][W-1][s]:
            print(s)
            break

if __name__ == '__main__':
    main()

# Optimized solution
import sys
def main():
    input = sys.stdin.readline
    H,W = list(map(int, input().split()))
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()
# Original solution
import sys
def main():
    input = sys.stdin.readline
    H,W=list(map(int, input().split()))
    A=[list(map(int, input().split())) for _ in range(H)]
    B=[list(map(int, input().split())) for _ in range(H)]

    mx=80*(80+80)
    C=[[A[i][j]-B[i][j] for j in range(W)] for i in range(H)]
    dp=[[[0]*(mx*2+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][C[0][0]+mx]=1
    dp[0][0][-C[0][0]+mx]=1
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        for d in range(80*(i+j+1)*2):
            d=d-80*(i+j+1)+mx
            if i>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d+C[i][j]])
            if j>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d+C[i][j]])
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1][mx+ans] or dp[H-1][W-1][mx-ans]:
            print(ans)
            break

if __name__ == '__main__':
    main()

# Optimized solution
import sys
def main():
    input = sys.stdin.readline
    H,W=list(map(int, input().split()))
    A=[list(map(int, input().split())) for _ in range(H)]
    B=[list(map(int, input().split())) for _ in range(H)]

    mx=80*(H+W)
    C=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]
    dp=[[0 for _ in range(W)] for __ in range(H)]
    dp[0][0]=1<<(mx+C[0][0]) | 1<<(mx-C[0][0])
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        if i>0:
            dp[i][j] |= dp[i-1][j] << C[i][j]
            dp[i][j] |= dp[i-1][j] >> C[i][j]
        if j>0:
            dp[i][j] |= dp[i][j-1] << C[i][j]
            dp[i][j] |= dp[i][j-1] >> C[i][j]
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1] & (1<<(mx+ans) | 1<<(mx-ans)):
            print(ans)
            break

if __name__ == '__main__':
    main()
# Original solution
import sys
def main():
    input = sys.stdin.readline
    H,W=list(map(int, input().split()))
    A=[list(map(int, input().split())) for _ in range(H)]
    B=[list(map(int, input().split())) for _ in range(H)]

    mx=80*(H+W)
    C=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]
    dp=[[0 for _ in range(W)] for __ in range(H)]
    dp[0][0]=1<<(mx+C[0][0]) | 1<<(mx-C[0][0])
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        if i>0:
            dp[i][j] |= dp[i-1][j] << C[i][j]
            dp[i][j] |= dp[i-1][j] >> C[i][j]
        if j>0:
            dp[i][j] |= dp[i][j-1] << C[i][j]
            dp[i][j] |= dp[i][j-1] >> C[i][j]
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1] & (1<<(mx+ans) | 1<<(mx-ans)):
            print(ans)
            break

if __name__ == '__main__':
    main()

# Optimized solution
import sys
def main():
    input = sys.stdin.readline
    H,W=list(map(int, input().split()))
    A=[list(map(int, input().split())) for _ in range(H)]
    B=[list(map(int, input().split())) for _ in range(H)]

    mx=80*(H+W)
    C=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]
    dp=[[0 for _ in range(W)] for __ in range(H)]
    dp[0][0]=1<<(mx+C[0][0])
    for i in range(H):
        for j in range(W):
            for ni,nj in ((i+1,j),(i,j+1)):
                if ni>=H or nj>=W: continue
                dp[ni][nj] |= dp[i][j] << C[ni][nj]
                dp[ni][nj] |= dp[i][j] >> C[ni][nj]

    for ans in range(mx+1):
        if dp[H-1][W-1] & (1<<(mx+ans) | 1<<(mx-ans)):
            print(ans)
            break

if __name__ == '__main__':
    main()
# Original solution
def main():
    H, W = [int(i) for i in input().split()]
    A = [[int(i) for i in input().split()] for _ in range(H)]
    B = [[int(i) for i in input().split()] for _ in range(H)]
    M = (H + W) * 80 + 1

    u = [[False] * M for _ in range(W)]
    v = [[False] * M for _ in range(W)]
    v[0][0] = True
    for h in range(H):
        v, u = u, v
        for w in range(W):
            a, b = A[h][w], B[h][w]
            d = a - b if a > b else b - a
            for i in range(M):
                t = False
                if 0 <= i + d < M:
                    t = t or u[w][i + d] or (w > 0 and v[w - 1][i + d])
                if 0 <= i - d < M:
                    t = t or u[w][i - d] or (w > 0 and v[w - 1][i - d])
                if 0 <= d - i < M:
                    t = t or u[w][d - i] or (w > 0 and v[w - 1][d - i])
                v[w][i] = t
    vv = v[-1]
    for i in range(M):
        if vv[i]:
            print(i)
            return
    raise

main()


# Optimized solution
def main():
    H, W = [int(i) for i in input().split()]
    A = [[int(i) for i in input().split()] for _ in range(H)]
    B = [[int(i) for i in input().split()] for _ in range(H)]
    M = (H + W) * 80 + 1

    u = [[False] * M for _ in range(W)]
    v = [[False] * M for _ in range(W)]
    F = [False] * M
    v[0][0] = True
    for h in range(H):
        v, u = u, v
        vl = F
        for w in range(W):
            a, b = A[h][w], B[h][w]
            d = a - b if a > b else b - a
            uu = u[w]
            vv = v[w]
            for i in range(M):
                di = i - d if i > d else d - i
                vv[i] = ((i + d < M and (uu[i + d] or vl[i + d])) or
                           (uu[di] or vl[di]))
            vl = vv
    va = v[-1]
    for i in range(M):
        if va[i]:
            print(i)
            return
    raise

main()
# Original solution
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    h,w = list(map(int, input().split()))
    a = [list(map(int, input().split())) for _ in range(h)]
    b = [list(map(int, input().split())) for _ in range(h)]
    c = [[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            c[i][j] = abs(a[i][j] -b[i][j])

    mid = 12800
    dp = [[0]*w for _ in range(h)]
    dp[0][0] = (1<<(mid+c[0][0])) | (1<<(mid-c[0][0]))
    for x in range(h):
        for y in range(w):
            for dx, dy in [[0, 1], [1, 0]]:
                nx = x + dx
                ny = y + dy
                if 0<=nx<h and 0<=ny<w:
                    dp[nx][ny] = dp[nx][ny] | (dp[x][y]<<c[nx][ny])
                    dp[nx][ny] = dp[nx][ny] | (dp[x][y]>>c[nx][ny])
    
    for i in range(mid+1):
        if (dp[-1][-1]>>i) & 1:
            res = mid - i

    print(res)

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    h,w = list(map(int, input().split()))
    a = [list(map(int, input().split())) for _ in range(h)]
    b = [list(map(int, input().split())) for _ in range(h)]
    c = [[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            c[i][j] = abs(a[i][j] -b[i][j])

    mid = 3200
    dp = [[0]*w for _ in range(h)]
    dp[0][0] = (1<<(mid+c[0][0])) | (1<<(mid-c[0][0]))
    for x in range(h):
        for y in range(w):
            for dx, dy in [[0, 1], [1, 0]]:
                nx = x + dx
                ny = y + dy
                if 0<=nx<h and 0<=ny<w:
                    dp[nx][ny] = dp[nx][ny] | (dp[x][y]<<c[nx][ny])
                    dp[nx][ny] = dp[nx][ny] | (dp[x][y]>>c[nx][ny])
    
    for i in range(mid+1):
        if (dp[-1][-1]>>i) & 1:
            res = mid - i

    print(res)

if __name__ == '__main__':
    main()
# Original solution
import sys

input = sys.stdin.readline

ri = lambda: int(eval(input()))
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

h, w = ril()
ls0 = rils(h)
ls1 = rils(h)

nums = [[set() for _ in range(w)] for _ in range(h)]
nums[0][0].add(abs(ls0[0][0] - ls1[0][0]))
for i in range(h):
    for j in range(w):
        x = abs(ls0[i][j] - ls1[i][j])
        if i > 0 and j > 0:
            s = nums[i - 1][j] | nums[i][j - 1]
            for y in s:
                nums[i][j].add(abs(x + y))
                nums[i][j].add(abs(x - y))
        elif i > 0:
            for y in nums[i - 1][j]:
                nums[i][j].add(abs(x + y))
                nums[i][j].add(abs(x - y))
        elif j > 0:
            for y in nums[i][j - 1]:
                nums[i][j].add(abs(x + y))
                nums[i][j].add(abs(x - y))
print((min(nums[h - 1][w - 1])))

# Optimized solution
import sys

input = sys.stdin.readline

ri = lambda: int(eval(input()))
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

h, w = ril()
ls0 = rils(h)
ls1 = rils(h)

nums = [[set() for _ in range(w)] for _ in range(h)]
nums[0][0].add(abs(ls0[0][0] - ls1[0][0]))
for i in range(h):
    for j in range(w):
        x = abs(ls0[i][j] - ls1[i][j])
        if i > 0:
            for y in nums[i - 1][j]:
                z = abs(x + y)
                if z <= 80 * 81:
                    nums[i][j].add(abs(x + y))
                nums[i][j].add(abs(x - y))
        if j > 0:
            for y in nums[i][j - 1]:
                z = abs(x + y)
                if z <= 80 * 81:
                    nums[i][j].add(abs(x + y))
                nums[i][j].add(abs(x - y))
print((min(nums[h - 1][w - 1])))
# Original solution
def main():
    B_MAX = (80 + 80 - 1) * 80
    H, W = list(map(int, input().split(' ')))
    A = [list(map(int, input().split(' '))) for _ in range(H)]
    B = [list(map(int, input().split(' '))) for _ in range(H)]
    dp = [[[0 for _ in range(B_MAX + 1)] for _ in range(W)] for _ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    max_b = abs(A[0][0] - B[0][0])
    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            d = abs(A[h][w] - B[h][w])
            max_b += d
            for b in range(min(B_MAX, max_b) + 1):
                v = 0
                if h > 0:
                    v = max(v, dp[h - 1][w][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h - 1][w][b + d])
                if w > 0:
                    v = max(v, dp[h][w - 1][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h][w - 1][b + d])
                dp[h][w][b] = v
    ans = 0
    while dp[H - 1][W - 1][ans] == 0:
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    B_MAX = (80 + 80 - 1) * 80 // 2
    H, W = list(map(int, input().split(' ')))
    A = [list(map(int, input().split(' '))) for _ in range(H)]
    B = [list(map(int, input().split(' '))) for _ in range(H)]
    dp = [[[0 for _ in range(B_MAX + 1)] for _ in range(W)] for _ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    max_b = abs(A[0][0] - B[0][0])
    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            d = abs(A[h][w] - B[h][w])
            max_b += d
            for b in range(min(B_MAX, max_b) + 1):
                v = 0
                if h > 0:
                    v = max(v, dp[h - 1][w][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h - 1][w][b + d])
                if w > 0:
                    v = max(v, dp[h][w - 1][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h][w - 1][b + d])
                dp[h][w][b] = v
    ans = 0
    while dp[H - 1][W - 1][ans] == 0:
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
h, w = list(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)


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H,W = list(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)
# Original solution
import sys
input = sys.stdin.readline
h,w = list(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)]
grid = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    grid[i][j] = abs(a[i][j]-b[i][j])
dp = [[[False for i in range(12881)] for j in range(w)] for k in range(h)]
for i in range(h):
  for j in range(w):
    for k in range(12801):
      t = grid[i][j]
      if i == j == 0 and t == k:
        dp[i][j][k] = True
      if j > 0:
        dp[i][j][k] = dp[i][j][k] or dp[i][j-1][k+t] or dp[i][j-1][abs(k-t)]
      if i > 0:
        dp[i][j][k] = dp[i][j][k] or dp[i-1][j][k+t] or dp[i-1][j][abs(k-t)]
for i in range(12801):
  if dp[h-1][w-1][i]:
    print(i)
    break


# Optimized solution
h,w = list(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)]
grid = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    grid[i][j] = abs(a[i][j]-b[i][j])
dp = [[0 for j in range(w)] for k in range(h)]
buf = (h+w)*80
for i in range(h):
  for j in range(w):
      t = grid[i][j]
      if i == j == 0:
        dp[i][j] = 1 << buf+t | 1 << buf-t
      if j > 0:
        dp[i][j] = dp[i][j] | dp[i][j-1] << t | dp[i][j-1] >> t
      if i > 0:
        dp[i][j] = dp[i][j] | dp[i-1][j] << t | dp[i-1][j] >> t
for i in range(buf):
  if (dp[h-1][w-1] & 1 << buf+i) or (dp[h-1][w-1] & 1 << buf-i):
    print(i)
    exit()
# Original solution
h,w = list(map(int,input().split()))
a = []
b = []
for i in range(h):
    a1 = list(map(int,input().split()))
    a.append(a1)
for i in range(h):
    b1 = list(map(int,input().split()))
    b.append(b1)
l = [[] for i in range(h)]
for i in range(h):
    for j in range(w):
        l[i].append(abs(a[i][j]-b[i][j]))
dp = [[[0 for i in range(81*(h+w))] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        #print(dp)
        for k in range(81*(h+w)):
            if i == 0:
                if j == 0:
                    dp[i][j][l[i][j]] = 1
                else:
                    if dp[i][j-1][k] == 1:
                        dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
            else:
                if j == 0:
                    if dp[i-1][j][k] == 1:
                        dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                else:
                    if dp[i-1][j][k] == 1:
                        dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                    if dp[i][j-1][k] == 1:
                        dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
ans = 80
for i in range(81):
    if dp[h-1][w-1][i] == 1:
        ans = i
        break
print(ans)

# Optimized solution
h,w = list(map(int,input().split()))
a = []
b = []
for i in range(h):
    a1 = list(map(int,input().split()))
    a.append(a1)
for i in range(h):
    b1 = list(map(int,input().split()))
    b.append(b1)
l = [[] for i in range(h)]
for i in range(h):
    for j in range(w):
        l[i].append(abs(a[i][j]-b[i][j]))
dp = [[[0 for i in range(81*(h+w)//2+1)] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        #print(dp)
        for k in range(81*(h+w)//2+1):
            if i == 0:
                if j == 0:
                    dp[i][j][l[i][j]] = 1
                else:
                    if dp[i][j-1][k] == 1:
                        if k+l[i][j] <= 81*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
            else:
                if j == 0:
                    if dp[i-1][j][k] == 1:
                        if k+l[i][j] <= 81*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                else:
                    if dp[i-1][j][k] == 1:
                        if k+l[i][j] <= 81*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                    if dp[i][j-1][k] == 1:
                        if k+l[i][j] <= 81*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
ans = 80
for i in range(81):
    if dp[h-1][w-1][i] == 1:
        ans = i
        break
print(ans)