CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.readline

def main():
    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)]

    dp = [[[False]*4000 for j in range(w)] for i in range(h)]

    dp[0][0][2000+a[0][0]-b[0][0]] = True
    dp[0][0][2000-a[0][0]+b[0][0]] = True


    for i in range(1,h):
        for k in range(4000):
            if 0 <= k+a[i][0]-b[i][0] <= 3999 and dp[i-1][0][k+a[i][0]-b[i][0]]:
                dp[i][0][k] = True
            elif 0 <= k-a[i][0]+b[i][0] <= 3999 and dp[i-1][0][k-a[i][0]+b[i][0]]:
                dp[i][0][k] = True

    for j in range(1,w):
        for k in range(4000):
            if 0 <= k+a[0][j]-b[0][j] <= 3999 and dp[0][j-1][k+a[0][j]-b[0][j]]:
                dp[0][j][k] = True
            elif 0 <= k-a[0][j]+b[0][j] <= 3999 and dp[0][j-1][k-a[0][j]+b[0][j]]:
                dp[0][j][k] = True       


    for i in range(1,h):
        for j in range(1,w):
            for k in range(4000):
                if 0 <= k+a[i][j]-b[i][j] <= 3999 and dp[i-1][j][k+a[i][j]-b[i][j]]:
                    dp[i][j][k] = True
                elif 0 <= k+a[i][j]-b[i][j] <= 3999 and dp[i][j-1][k+a[i][j]-b[i][j]]:
                    dp[i][j][k] = True
                elif 0 <= k-a[i][j]+b[i][j] <= 3999 and dp[i-1][j][k-a[i][j]+b[i][j]]:
                    dp[i][j][k] = True
                elif 0 <= k-a[i][j]+b[i][j] <= 3999 and dp[i][j-1][k-a[i][j]+b[i][j]]:
                    dp[i][j][k] = True

    res = 10**9
    for k in range(4000):
        if dp[-1][-1][k]:
            res = min(res,abs(2000-k))

    print(res)

if __name__ == '__main__':
    main()


# Optimized solution
import sys
input = sys.stdin.readline

h,w =list(map(int,input().split()))
a = [tuple(map(int,input().split())) for _ in [0]*h]
b = [tuple(map(int,input().split())) for _ in [0]*h]


mid = 1<<(80*160)
dp = [[0]*w for _ in [0]*h]

for i in range(h):
    for j in range(w):
        diff = abs(a[i][j]-b[i][j])
        new = 0
        if i > 0:
            new |= dp[i-1][j]
        if j > 0:
            new |= dp[i][j-1]
        if (i,j) == (0,0):
            new = mid
        dp[i][j] = (new<<diff)|(new>>diff)


res = 10**9
p = dp[-1][-1]
for i in range(80*321):
    if p&1 == 1:
        res = min(res,abs(80*160-i))
    p = p>> 1
print(res)
# 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)]

C = [[] for _ in range(H)]
for i in range(H):
  for j in range(W):
    C[i].append(abs(A[i][j] - B[i][j]))
#print(C)

dp = [[[0 for _ in range((H + W) * 80 + 1)] for _ in range(W)] for _ in range(H)]
#print(dp)

dp[0][0][C[0][0]] = 1
for i in range(H):
  for j in range(W):
    for k in range((H+W)*80+1):
      if dp[i-1][j][k]:
        dp[i][j][k + C[i][j]] = 1
        dp[i][j][abs(k - C[i][j])] = 1
      if dp[i][j-1][k]:
        dp[i][j][k + C[i][j]] = 1
        dp[i][j][abs(k - C[i][j])] = 1

for i in range((H+W)*80+1):
  if dp[H-1][W-1][i]:
    print(i)
    exit()

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

dp = [[0 for _ in range(W)] for _ in range(H)]

SIZE = 20000

D = abs(A[0][0] - B[0][0])
dp[0][0] = 1 << SIZE - D | 1 << SIZE + D
#print("dp:", dp)

for i in range(H):
  for j in range(W):
    D = abs(A[i][j] - B[i][j])
    dp[i][j] |= dp[i - 1][j] << D | dp[i - 1][j] >> D
    dp[i][j] |= dp[i][j - 1] << D | dp[i][j - 1] >> D

#for i in range(H):
#  a = list(map(lambda x: bin(x)[2::], dp[i]))
#  print(a)

for i in range(SIZE):
  if (dp[-1][-1] >> (SIZE + i)) & 1:
    print(i)
    exit()
# Original solution
from collections import deque
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] = A[i][j]-B[i][j]
M = 80*(H+W)
dp = [[[0]*(M*2+10) for i in range(W)] for j in range(H)]
dp[0][0][0] = 1
que = deque([(0, 0)])
while que:
    a, b = que.pop()
    c = C[a][b]
    if a<=H-2:
        for i in range(-M+c, M+1-c):
            if dp[a][b][i]:
                dp[a+1][b][i+c] = 1
                dp[a+1][b][i-c] = 1
        que.append((a+1, b))
    if b<=W-2:
        for i in range(-M+c, M+1-c):
            if dp[a][b][i]:
                dp[a][b+1][i+c] = 1
                dp[a][b-1][i-c] = 1
        que.append((a, b+1))
c = C[-1][-1]
ans = 10**10
for i in range(-M, M+1):
    if dp[-1][-1][i]:
        ans = min(ans, abs(i+c), abs(i-c))
print(ans)






# Optimized solution
from collections import deque
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] = A[i][j]-B[i][j]
M = 80*(H+W)
dp = [[set() for i in range(W)] for j in range(H)]
c = C[0][0]
dp[0][0].add(c)
dp[0][0].add(-c)
for s in range(1, H+W):
    for b in range(W):
        a = s-b
        if a<0 or a>=H:
            continue
        c = C[a][b]
        if a>=1:
            for i in dp[a-1][b]:
                dp[a][b].add(i+c)
                dp[a][b].add(i-c)

        if b>=1:
            for i in dp[a][b-1]:
                dp[a][b].add(i+c)
                dp[a][b].add(i-c)
ans = min(abs(i) for i in dp[-1][-1])
print(ans)
# Original solution
from collections import deque
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] = A[i][j]-B[i][j]
M = 80*(H+W)
dp = [set() for j in range(H)]
c = C[0][0]
dp[0].add(c)
dp[0].add(-c)
for i in range(1, H):
    c = C[i][0]
    for j in dp[i-1]:
        dp[i].add(j+c)
        dp[i].add(j-c)
for i in range(1, W):
    dp1 = [set() for j in range(H)]
    c = C[0][i]
    for k in dp[0]:
        dp1[0].add(k+c)
        dp1[0].add(k-c)
    for j in range(1, H):
        c = C[j][i]
        for k in dp[j]:
            dp1[j].add(k+c)
            dp1[j].add(k-c)
        for k in dp1[j-1]:
            dp1[j].add(k+c)
            dp1[j].add(k-c)
    dp = dp1

ans = min(abs(i) for i in dp[-1])
print(ans)

        









# Optimized solution
from collections import deque
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])
M = 80*(H+W)
dp = [[0 for i in range(W)] for j in range(H)]
c = C[0][0]
dp[0][0]+=(1<<(c+M))|(1<<(M-c))
for s in range(1, H+W):
    for b in range(max(s-H+1, 0), min(s+1, W)):
        a = s-b
        c = C[a][b]
        if a>=1:
            d = dp[a-1][b]
            dp[a][b]|=(d<<c)|(d>>c)
        if b>=1:
            d = dp[a][b-1]
            dp[a][b]|=(d<<c)|(d>>c)
ans = 10**10
d = dp[-1][-1]
for i in range(0, M*2+1):
    if 1<<i&d:
        ans = min(ans, abs(i-M))
print(ans)
# Original solution
import sys
def main():
    input = sys.stdin.readline
    H, W = list(map(int, input().split()))
    A = []
    B = []
    M = 80 * (H + W)
    for _ in range(H):
        a = tuple(map(int, input().split()))
        A.append(a)
    for _ in range(H):
        b = tuple(map(int, input().split()))
        B.append(b)
    AB = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            AB[i][j] = abs(A[i][j] - B[i][j])
    l = [[set([]) for _ in range(M) for _ in range(W)] for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                l[i][j].add(AB[0][0])
            else:
                t = AB[i][j]
                if i > 0:
                    for k in l[i-1][j]:
                        l[i][j].add(k+t)
                        l[i][j].add(abs(k-t))
                if j > 0:
                    for k in l[i][j-1]:
                        l[i][j].add(k+t)
                        l[i][j].add(abs(k-t))
    return min(l[i][j])
print((main()))



# Optimized solution
import sys
def main():
    input = sys.stdin.readline
    H, W = list(map(int, input().split()))
    A = []
    B = []
    M = 80 * (H + W)
    for _ in range(H):
        a = tuple(map(int, input().split()))
        A.append(a)
    for _ in range(H):
        b = tuple(map(int, input().split()))
        B.append(b)
    AB = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            AB[i][j] = abs(A[i][j] - B[i][j])
    l = [set([]) for _ in range(W)]
    lt = [set([]) for _ in range(W)]
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                lt[j].add(AB[0][0])
            else:
                t = AB[i][j]
                if i > 0:
                    for k in l[j]:
                        lt[j].add(k+t)
                        lt[j].add(abs(k-t))
                if j > 0:
                    for k in lt[j-1]:
                        lt[j].add(k+t)
                        lt[j].add(abs(k-t))
        l = lt
        lt = [set([]) for _ in range(W)]
    return min(l[-1])
print((main()))
# 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)]
 
C = [[0] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        C[i][j] = abs(A[i][j]-B[i][j])
 
V = [[2] * w for i in range(h)]
for i in range(h):
    V[i][0] -= 1
for i in range(w):
    V[0][i] -= 1
 
l = 7000
m = 0
 
M = [[[0] * l for i in range(w)] for j in range(h)]
N = [[0 for i in range(w)] for j in range(h)]
N[0][0] = C[0][0]
 
s = 0
Q = []
for i in range(h):
    Q.append([i, 0])
for i in range(1, w):
    Q.append([0, i])
for i in range(1, h):
    for j in range(1, w):
        Q.append([i, j])

M[0][0][C[0][0]] = 1
# M[0][0][m-C[0][0]] = 1
 
for s in range(len(Q)):
    i = Q[s][0]
    j = Q[s][1]
    if i+1 < h:
        for ii in range(min(N[i][j]+1, l)):
            if M[i][j][ii] == 1:
                if abs(ii+C[i+1][j]) < l:
                    M[i+1][j][abs(ii+C[i+1][j])] = 1
                if abs(ii-C[i+1][j]) < l:
                    M[i+1][j][abs(ii-C[i+1][j])] = 1

        N[i+1][j] = max(N[i+1][j], N[i][j] + C[i+1][j])

    if j+1 < w:
        for ii in range(min(N[i][j]+1, l)):
            if M[i][j][ii] == 1:
                if abs(ii+C[i][j+1]) < l:
                    M[i][j+1][abs(ii+C[i][j+1])] = 1
                if abs(ii-C[i][j+1]) < l:
                    M[i][j+1][abs(ii-C[i][j+1])] = 1

        N[i][j+1] = max(N[i][j+1], N[i][j] + C[i][j+1])
 
# print(M[-1][-1])
 
ans = 10**5
for i in range(len(M[-1][-1])):
    if M[-1][-1][i] == 1:
        ans = i
        break
print(ans)

# Optimized 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)]
 
C = [[0] * w for i in range(h)]
for i in range(h):
    for j in range(w):
        C[i][j] = abs(A[i][j]-B[i][j])
 
M = [[0] * w for i in range(h)]
 
s = 0
Q = []
for i in range(h):
    Q.append([i, 0])
for i in range(1, w):
    Q.append([0, i])
for i in range(1, h):
    for j in range(1, w):
        Q.append([i, j])

m = 80
M[0][0] = (1 << m + C[0][0]) | (1 << m - C[0][0])

for s in range(len(Q)):
    i = Q[s][0]
    j = Q[s][1]
    if i+1 < h:
        M[i+1][j] |= (M[i][j] << m + C[i+1][j]) | (M[i][j] << m - C[i+1][j])
    if j+1 < w:
        M[i][j+1] |= (M[i][j] << m + C[i][j+1]) | (M[i][j] << m - C[i][j+1])

        # N[i][j+1] = max(N[i][j+1], N[i][j] + C[i][j+1])
 
# print(M[-1][-1])
b = bin(M[-1][-1])
ans = float("inf")
for i in range(len(b)):
    if b[-1-i] == "1":
        ans = min(ans, abs(i - m*(h+w-1)))
print(ans)
# 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)]
grid = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        m = abs(A[i][j] - B[i][j])
        grid[i][j] = m

bias_inf = 2000
dp = [[[0 for i in range(-bias_inf,bias_inf)] for j in range(w)] for k in range(h)]
dp[0][0][0 - grid[0][0]] = 1
dp[0][0][0 + grid[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k in range(-bias_inf,bias_inf):
            if i == h-1 and j == w-1:
                break
            if dp[i][j][k]:
                if i != h-1 and j != w-1:
                    dp[i][j+1][k + grid[i][j+1]] = 1
                    dp[i+1][j][k + grid[i+1][j]] = 1
                    dp[i][j+1][k - grid[i][j+1]] = 1
                    dp[i+1][j][k - grid[i+1][j]] = 1
                elif j == w-1:
                    dp[i+1][j][k - grid[i+1][j]] = 1
                    dp[i+1][j][k + grid[i+1][j]] = 1
                else:
                    dp[i][j+1][k - grid[i][j+1]] = 1
                    dp[i][j+1][k + grid[i][j+1]] = 1
ans = bias_inf
for i in range(-bias_inf,bias_inf):
    if dp[-1][-1][i]:
        ans = min(ans, abs(i))
print(ans)

# Optimized solution
def solve(h, w, A, B):
    center = 80 * (h + w - 1)
    C = [[abs(A[r][c] - B[r][c]) for c in range(w)] for r in range(h)]
    dp = [[0] * w for r in range(h)]
    dp[0][0] = (1 << (center + C[0][0])) | (1 << (center - C[0][0]))
    for r in range(h):
        for c in range(w):
            if r > 0:
                dp[r][c] |= (dp[r-1][c] << C[r][c]) | (dp[r-1][c] >> C[r][c])
            if c > 0:
                dp[r][c] |= (dp[r][c-1] << C[r][c]) | (dp[r][c-1] >> C[r][c])
    mask = bin(dp[-1][-1] >> center)
    return mask[::-1].find("1")
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)]
print((solve(h, w, A, B)))
# Original solution
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def main():
    inf = 10 ** 16
    h, w = MI()
    aa = LLI(h)
    bb = LLI(h)
    cc = [[a - b for a, b in zip(ar, br)] for ar, br in zip(aa, bb)]
    dp = [[set() for _ in range(w)] for _ in range(h)]
    dp0 = dp[0]
    dp0[0].add(abs(cc[0][0]))
    for j in range(1, w):
        c = cc[0][j]
        for a in dp0[j - 1]:
            dp0[j].add(abs(a + c))
            dp0[j].add(abs(a - c))
    for i in range(1, h):
        c = cc[i][0]
        for a in dp[i - 1][0]:
            dp[i][0].add(abs(a + c))
            dp[i][0].add(abs(a - c))
    for i in range(1, h):
        dpi=dp[i]
        dpi1=dp[i-1]
        cci=cc[i]
        for j in range(1, w):
            c = cci[j]
            for a in dpi1[j]:
                dpi[j].add(abs(a + c))
                dpi[j].add(abs(a - c))
            for a in dpi[j - 1]:
                dpi[j].add(abs(a + c))
                dpi[j].add(abs(a - c))
    print(min(dp[h-1][w-1]))
main()


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def bin_out(aa):
    bb = [format(a, "b") for a in aa]
    print(bb)

def main():
    h, w = MI()
    aa = LLI(h)
    bb = LLI(h)
    max_val=6400
    zero = 1 << max_val
    dp = [0] * w
    dp[0] = zero
    #bin_out(dp)
    for i, (ar, br) in enumerate(zip(aa, bb)):
        for j, (a, b) in enumerate(zip(ar, br)):
            d = abs(a - b)
            if j > 0: dp[j] |= dp[j - 1]
            dp[j] = dp[j] << d | dp[j] >> d
        #bin_out(dp)
    goal = dp[-1]
    for ans in range(max_val + 1):
        if goal & zero << ans or goal & zero >> ans:
            print(ans)
            break

main()
# 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)]
d=[]
for i in range(h):
    lis=[]
    for j in range(w):
        lis.append(abs(a[i][j]-b[i][j]))
    d.append(lis)

dp = [[[0 for _ in range(80*(h+w))] for _ in range(w)] for _ in range(h)]
dp = [[0 for _ in range(6400)] for _ in range(h*w)]
dp[0][d[0][0]]=1

for i in range(h*w):
    nowh,noww = divmod(i,w)
    for k in range(6400):
        if not dp[nowh*w + noww][k]==1:
            continue
        nxth,nxtw=nowh+1, noww+1
        if nxtw<w:
            nxt=d[nowh][nxtw]
            if k+nxt < 6400:
                dp[nowh*w + nxtw][k+nxt] = 1
            dp[nowh*w + nxtw][abs(k-nxt)] = 1
        if nxth<h:
            nxt=d[nxth][noww]
            if k+nxt < 6400:
                dp[nxth*w + noww][k+nxt] = 1
            dp[nxth*w + noww][abs(k-nxt)] = 1


for k in range(6400):
    if dp[-1][k]==1:
        print(k)
        exit()

# Optimized 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)]
d=[]
for i in range(h):
    lis=[]
    for j in range(w):
        lis.append(abs(a[i][j]-b[i][j]))
    d.append(lis)

dp = [[0 for _ in range(6400)] for _ in range(h*w)]
dp[0][d[0][0]]=1

for i in range(h*w):
    nowh,noww = divmod(i,w)
    for k in range(6400):
        if not dp[nowh*w + noww][k]==1:
            continue
        nxth,nxtw=nowh+1, noww+1
        if nxtw<w:
            nxt=d[nowh][nxtw]
            if k+nxt < 6400:
                dp[nowh*w + nxtw][k+nxt] = 1
            dp[nowh*w + nxtw][abs(k-nxt)] = 1
        if nxth<h:
            nxt=d[nxth][noww]
            if k+nxt < 6400:
                dp[nxth*w + noww][k+nxt] = 1
            dp[nxth*w + noww][abs(k-nxt)] = 1


for k in range(6400):
    if dp[-1][k]==1:
        print(k)
        exit()
# 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)]
d = [[0 for j in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        d[i][j] = abs(a[i][j]-b[i][j])

dp = [[0 for _ in range(6400)] for _ in range(h*w)]
dp[0][d[0][0]]=1

for i in range(h*w):
    nowh,noww = divmod(i,w)
    for k in range(6400):
        if not dp[nowh*w + noww][k]==1:
            continue
        nxth,nxtw=nowh+1, noww+1
        if nxtw<w:
            nxt=d[nowh][nxtw]
            if k+nxt < 6400:
                dp[nowh*w + nxtw][k+nxt] = 1
            dp[nowh*w + nxtw][abs(k-nxt)] = 1
        if nxth<h:
            nxt=d[nxth][noww]
            if k+nxt < 6400:
                dp[nxth*w + noww][k+nxt] = 1
            dp[nxth*w + noww][abs(k-nxt)] = 1


for k in range(6400):
    if dp[-1][k]==1:
        print(k)
        exit()

# Optimized 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)]
d = [[0 for j in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        d[i][j] = abs(a[i][j]-b[i][j])

dp = [[0 for _ in range(6400)] for _ in range(h*w)]
dp[0][d[0][0]]=1

for i in range(h*w):
    nowh,noww = divmod(i,w)
    for k in range(6400):
        if not dp[nowh*w + noww][k]==1:
            continue
        nxth,nxtw=nowh+1, noww+1
        if nxtw<w:
            if k+d[nowh][nxtw] < 6400:
                dp[nowh*w + nxtw][k+d[nowh][nxtw]] = 1
            dp[nowh*w + nxtw][abs(k-d[nowh][nxtw])] = 1
        if nxth<h:
            if k+ d[nxth][noww]< 6400:
                dp[nxth*w + noww][k+d[nxth][noww]] = 1
            dp[nxth*w + noww][abs(k-d[nxth][noww])] = 1


for k in range(6400):
    if dp[-1][k]==1:
        print(k)
        exit()
# 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)]
d = [[0 for j in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        d[i][j] = abs(a[i][j]-b[i][j])

dp = [[0 for _ in range(6400)] for _ in range(h*w)]
dp[0][d[0][0]]=1

for i in range(h*w):
    nowh,noww = divmod(i,w)
    for k in range(6400):
        if not dp[nowh*w + noww][k]==1:
            continue
        nxth,nxtw=nowh+1, noww+1
        if nxtw<w:
            if k+d[nowh][nxtw] < 6400:
                dp[nowh*w + nxtw][k+d[nowh][nxtw]] = 1
            dp[nowh*w + nxtw][abs(k-d[nowh][nxtw])] = 1
        if nxth<h:
            if k+ d[nxth][noww]< 6400:
                dp[nxth*w + noww][k+d[nxth][noww]] = 1
            dp[nxth*w + noww][abs(k-d[nxth][noww])] = 1


for k in range(6400):
    if dp[-1][k]==1:
        print(k)
        exit()

# 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)]
d = [[0 for j in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        d[i][j] = abs(a[i][j]-b[i][j])

dp = [[0 for _ in range(6400)] for _ in range(h*w)]
dp[0][d[0][0]]=1

for i in range(h*w):
    nowh,noww = divmod(i,w)
    for k in range(6400):
        if not dp[nowh*w + noww][k]==1:
            continue
        nxth,nxtw=nowh+1, noww+1
        if nxtw<w:
            if k+d[nowh][nxtw] < 6400:
                dp[nowh*w + nxtw][k+d[nowh][nxtw]] = 1
            dp[nowh*w + nxtw][abs(k-d[nowh][nxtw])] = 1
        if nxth<h:
            if k+ d[nxth][noww]< 6400:
                dp[nxth*w + noww][k+d[nxth][noww]] = 1
            dp[nxth*w + noww][abs(k-d[nxth][noww])] = 1


for k in range(6400):
    if dp[-1][k]==1:
        print(k)
        exit()
# Original solution
H, W = list(map(int, input().split()))
A = []
B = []
for _ in range(H):
    A.append(list(map(int, input().split())))
for _ in range(H):
    B.append(list(map(int, input().split())))
d = [[0]*W for _ in range(H)]
for h in range(H):
    for w in range(W):
        d[h][w] = abs(A[h][w]-B[h][w])

dp = [[[] for _ in range(W)] for _ in range(H)]
dp[0][0].append(d[0][0])
for h in range(H):
    for w in range(W):
        if h < H-1:
            dp[h+1][w] += list([x+d[h+1][w] for x in dp[h][w]])
            dp[h+1][w] += list([abs(x-d[h+1][w]) for x in dp[h][w]])
        if w < W-1:
            dp[h][w+1] += list([x+d[h][w+1] for x in dp[h][w]])
            dp[h][w+1] += list([abs(x-d[h][w+1]) for x in dp[h][w]])
print((min(dp[-1][-1])))

# Optimized solution
def max2(x,y):
    return x if x > y else y

H, W = list(map(int, input().split()))
A = []
B = []
ofs = 80*H*W
for _ in range(H):
    A.append(list(map(int, input().split())))
for _ in range(H):
    B.append(list(map(int, input().split())))
d = [[0]*W for _ in range(H)]
for h in range(H):
    for w in range(W):
        d[h][w] = abs(A[h][w]-B[h][w])

dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1<<ofs
dp[0][0] = (dp[0][0]<<d[0][0])|(dp[0][0]>>d[0][0])
for h in range(H):
    for w in range(W):
        if h < H-1:
            dp[h+1][w] |= (dp[h][w]<<d[h+1][w])|(dp[h][w]>>d[h+1][w])
        if w < W-1:
            dp[h][w+1] |= (dp[h][w]<<d[h][w+1])|(dp[h][w]>>d[h][w+1])
res = dp[-1][-1]>>ofs
print(((-res&res).bit_length()-1))