CodeEff/ECCO huggingface dataset:
# 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)]
num=(H+W)*80+1
#dp[i][j]=マス(i,j)における可能なポイントのリスト
dp=[[[False]*(num) for _ in range(W)] for _ in range(H)]
dp[0][0][0]=True

for i in range(W+H-1):
  I,J=0,0
  if i < H:
    I=i
    J=0
  else:
    I=H-1
    J=i-H+1
  while I>=0 and J<W:
    sa = abs(A[I][J] - B[I][J])
    for n in range(num):
      if not dp[I][J][n]:
        continue
      if I!=H-1:
        dp[I+1][J][n+sa]=True
        dp[I+1][J][abs(n-sa)]=True
      if J!=W-1:
        dp[I][J+1][n+sa]=True
        dp[I][J+1][abs(n-sa)]=True
    I-=1
    J+=1

sa = abs(A[H-1][W-1] - B[H-1][W-1])
ans=float("inf")
for n in range(num):
  if not dp[-1][-1][n]:
    continue
  ans = min(ans,abs(n-sa),n+sa)
print(ans)

# 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[i][j]=マス(i,j)における可能なポイントのリスト
dp=[[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(0)

for i in range(W+H-1):
  I,J=0,0
  if i < H:
    I=i
    J=0
  else:
    I=H-1
    J=i-H+1
  while I>=0 and J<W:
    sa = abs(A[I][J] - B[I][J])
    for point in dp[I][J]:
      if I!=H-1:
        dp[I+1][J].add(point+sa)
        dp[I+1][J].add(abs(point-sa))
      if J!=W-1:
        dp[I][J+1].add(point+sa)
        dp[I][J+1].add(abs(point-sa))
    I-=1
    J+=1

ans=dp[H-1][W-1]
sa = abs(A[H-1][W-1] - B[H-1][W-1])
res=float("inf")
for p in ans:
  res=min(res,abs(p+sa),abs(p-sa))
print(res)
# Original solution
h,w = list(map(int, input().split()))
a = [list(map(int, input().split()))+[0] for i in range(h)] + [[0]*(w+1)]
b = [list(map(int, input().split()))+[0] for i in range(h)] + [[0]*(w+1)]
diff = [[x-y for x,y in zip(i,j)] for i,j in zip(a,b)]
diff_set = [[set() for j in range(w+1)] for i in range(h+1)]
diff_set[0][0].add(diff[0][0])
for i in range(h):
  for j in range(w):
    for x in diff_set[i][j]:
      diff_set[i][j+1].add(abs(x+diff[i][j+1]))
      diff_set[i][j+1].add(abs(x-diff[i][j+1]))
      diff_set[i+1][j].add(abs(x+diff[i+1][j]))
      diff_set[i+1][j].add(abs(x-diff[i+1][j]))
ans = min(diff_set[h-1][w-1])
print(ans)

# Optimized solution
h,w = list(map(int, input().split()))
a = [list(map(int, input().split()))+[0] for i in range(h)] + [[0]*(w+1)]
b = [list(map(int, input().split()))+[0] for i in range(h)] + [[0]*(w+1)]
diff = [[abs(x-y) for x,y in zip(i,j)] for i,j in zip(a,b)]
dp = [[0 for j in range(w+1)] for i in range(h+1)]
dp[0][0] = (1 << diff[0][0])


def reverse(d, x):
  res = 0
  for i in range(x+1):
    res <<= 1
    if (1<<i) & d:
      res |= 1
  return res


for i in range(h):
  for j in range(w):
    dp[i][j+1] |= (dp[i][j] << diff[i][j+1])
    dp[i][j+1] |= (dp[i][j] >> diff[i][j+1])
    dp[i][j+1] |= reverse(dp[i][j], diff[i][j+1])
    dp[i+1][j] |= (dp[i][j] << diff[i+1][j])
    dp[i+1][j] |= (dp[i][j] >> diff[i+1][j])
    dp[i+1][j] |= reverse(dp[i][j], diff[i+1][j])
last = dp[h-1][w-1]
for i in range(6401):
  if (last >> i) & 1:
    ans = i
    break
print(ans)
# Original solution
import sys
from collections import deque
def input(): return sys.stdin.readline().strip()


def main():
    H, W = list(map(int, input().split()))
    A = []
    for _ in range(H):
        A.append(list(map(int, input().split())))
    B = []
    for _ in range(H):
        B.append(list(map(int, input().split())))
    
    """
    明らかなdp臭。
        dp[i][j] = (マス(0, 0)から(i, j)に進むときの偏りとしてありうるものの集合)
    とおく。(0<=i<=H-1, 0<=j<=W-1, 0<=d<=80)
    """

    dp = [[set([]) for _ in range(W)] for _ in range(H)]
    dp[0][0].add(abs(A[0][0] - B[0][0]))
    for i in range(1, H):
        diff = A[i][0] - B[i][0]
        for val in dp[i - 1][0]:
            dp[i][0].add(abs(val + diff))
            dp[i][0].add(abs(val - diff))
    for j in range(1, W):
        diff = A[0][j] - B[0][j]
        for val in dp[0][j - 1]:
            dp[0][j].add(abs(val + diff))
            dp[0][j].add(abs(val - diff))

    for i in range(1, H):
        for j in range(1, W):
            diff = A[i][j] - B[i][j]
            for val in dp[i - 1][j]:
                dp[i][j].add(abs(val + diff))
                dp[i][j].add(abs(val - diff))
            for val in dp[i][j - 1]:
                dp[i][j].add(abs(val + diff))
                dp[i][j].add(abs(val - diff))
    
    print((min(dp[H - 1][W - 1])))


   


if __name__ == "__main__":
    main()


# Optimized solution
import sys
from collections import deque
def input(): return sys.stdin.readline().strip()


def main():
    H, W = list(map(int, input().split()))
    A = []
    for _ in range(H):
        A.append(list(map(int, input().split())))
    B = []
    for _ in range(H):
        B.append(list(map(int, input().split())))
    
    """
    明らかなdp臭。
        dp[i][j] = (マス(0, 0)から(i, j)に進むときの偏りとしてありうるものの集合)
    とおく。(0<=i<=H-1, 0<=j<=W-1, 0<=d<=80)
    """

    dp = [[set([]) for _ in range(W)] for _ in range(H)]
    dp[0][0].add(abs(A[0][0] - B[0][0]))
    for i in range(1, H):
        diff = A[i][0] - B[i][0]
        for val in dp[i - 1][0]:
            if abs(val + diff) <= 1600: dp[i][0].add(abs(val + diff))
            if abs(val - diff) <= 1600: dp[i][0].add(abs(val - diff))
    for j in range(1, W):
        diff = A[0][j] - B[0][j]
        for val in dp[0][j - 1]:
            if abs(val + diff) <= 6400: dp[0][j].add(abs(val + diff))
            if abs(val - diff) <= 6400: dp[0][j].add(abs(val - diff))

    for i in range(1, H):
        for j in range(1, W):
            diff = A[i][j] - B[i][j]
            for val in dp[i - 1][j]:
                if abs(val + diff) <= 6400: dp[i][j].add(abs(val + diff))
                if abs(val - diff) <= 6400: dp[i][j].add(abs(val - diff))
            for val in dp[i][j - 1]:
                if abs(val + diff) <= 6400: dp[i][j].add(abs(val + diff))
                if abs(val - diff) <= 6400: dp[i][j].add(abs(val - diff))
    
    print((min(dp[H - 1][W - 1])))


if __name__ == "__main__":
    main()
# Original solution
import sys
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)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[[0] * (80 * 80 * 2 + 1) for _ in range(W)] for _ in range(H)]
dp[0][0][X[0][0]] = 1
for i in range(H):
  for j in range(W):
    for k in range(80 * 3 + 1):
      if dp[i][j][k] == 0: continue
      if i + 1 < H:
        u = k + X[i + 1][j]
        v = abs(k - X[i + 1][j])
        dp[i + 1][j][u] += dp[i][j][k]
        dp[i + 1][j][v] += dp[i][j][k]
      if j + 1 < W:
        u = k + X[i][j + 1]
        v = abs(k - X[i][j + 1])
        dp[i][j + 1][u] += dp[i][j][k]
        dp[i][j + 1][v] += dp[i][j][k]
res = 80
#print(dp[1][1])
for k in range(81):
  if dp[-1][-1][k]: res = min(k, res)
print(res)

# Optimized solution
import sys
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)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(X[0][0])
for i in range(H):
  for j in range(W):
    for k in dp[i][j]:
      if i + 1 < H:
        u = k + X[i + 1][j]
        v = abs(k - X[i + 1][j])
        dp[i + 1][j].add(u)
        dp[i + 1][j].add(v)
      if j + 1 < W:
        u = k + X[i][j + 1]
        v = abs(k - X[i][j + 1])
        dp[i][j + 1].add(u)
        dp[i][j + 1].add(v)
res = float("inf")
#print(dp[1][1])
for k in dp[-1][-1]: res = min(k, res)
print(res)
# Original solution
import sys
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)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp1 = [set() for _ in range(W)]
dp1[0].add(X[0][0])
dp2 = [set() for _ in range(W)]
for i in range(H):
  if i % 2 == 0:
    dp2 = [set() for _ in range(W)]
  else: dp1 = [set() for _ in range(W)]
  for j in range(W):
    if i % 2 == 0:
      for k in dp1[j]:
        if i + 1 < H:
          u = k + X[i + 1][j]
          v = abs(k - X[i + 1][j])
          dp2[j].add(u)
          dp2[j].add(v)
        if j + 1 < W:
          u = k + X[i][j + 1]
          v = abs(k - X[i][j + 1])
          dp1[j + 1].add(u)
          dp1[j + 1].add(v)
    else:
      for k in dp2[j]:
        if i + 1 < H:
          u = k + X[i + 1][j]
          v = abs(k - X[i + 1][j])
          dp1[j].add(u)
          dp1[j].add(v)
        if j + 1 < W:
          u = k + X[i][j + 1]
          v = abs(k - X[i][j + 1])
          dp2[j + 1].add(u)
          dp2[j + 1].add(v)
res = float("inf")
if H % 2:
  for k in dp1[-1]: res = min(k, res)
else:
  for k in dp2[-1]: res = min(k, res)
print(res)

# Optimized solution
import sys
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)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp1 = [set() for _ in range(W)]
dp1[0].add(X[0][0])
dp2 = [set() for _ in range(W)]
dplimit = [[0] * W for _ in range(H)]
dplimit[-1][-1] = 80
for i in range(H - 1, -1, -1):
  for j in range(W - 1, -1, -1):
    x = X[i][j]
    if i > 0: dplimit[i - 1][j] = max(dplimit[i - 1][j], dplimit[i][j] + x)
    if j > 0: dplimit[i][j - 1] = max(dplimit[i][j - 1], dplimit[i][j] + x)
for i in range(H):
  if i % 2 == 0:
    dp2 = [set() for _ in range(W)]
  else: dp1 = [set() for _ in range(W)]
  for j in range(W):
    if i % 2 == 0:
      for k in dp1[j]:
        if i + 1 < H:
          u = k + X[i + 1][j]
          v = abs(k - X[i + 1][j])
          if u <= dplimit[i][j]: dp2[j].add(u)
          if v <= dplimit[i][j]: dp2[j].add(v)
        if j + 1 < W:
          u = k + X[i][j + 1]
          v = abs(k - X[i][j + 1])
          if u <= dplimit[i][j]: dp1[j + 1].add(u)
          if v <= dplimit[i][j]: dp1[j + 1].add(v)
    else:
      for k in dp2[j]:
        if i + 1 < H:
          u = k + X[i + 1][j]
          v = abs(k - X[i + 1][j])
          if u <= dplimit[i][j]: dp1[j].add(u)
          if v <= dplimit[i][j]: dp1[j].add(v)
        if j + 1 < W:
          u = k + X[i][j + 1]
          v = abs(k - X[i][j + 1])
          if u <= dplimit[i][j]: dp2[j + 1].add(u)
          if v <= dplimit[i][j]: dp2[j + 1].add(v)
res = float("inf")
if H % 2:
  for k in dp1[-1]: res = min(k, res)
else:
  for k in dp2[-1]: res = min(k, res)
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 = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

dp = [[[False] * 12801 for j in range(W)] for _ in range(H)]
dp[0][0][C[0][0]] = True
for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        for n in range(12801):
            if i > 0 and dp[i-1][j][n] or j > 0 and dp[i][j-1][n]:
                dp[i][j][abs(n-C[i][j])] = True
                dp[i][j][abs(n+C[i][j])] = True

for n in range(12801):
    if dp[H-1][W-1][n]:
        print(n)
        break

# 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)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

M = (H+W)*80
dp = [[0] * W for _ in range(H)]
dp[0][0] |= 1 << (M-C[0][0])
for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        bit = 0
        if i > 0:
            bit |= dp[i-1][j]
        if j > 0:
            bit |= dp[i][j-1]
        dp[i][j] = (bit >> C[i][j]) | (bit << C[i][j])
ans = M
for i in range(M+1):
    if (dp[-1][-1] >> i) & 1:
        ans = min(ans, abs(M-i))
print(ans)
# Original solution
import sys
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)]
can_make = [[None for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        x = A[i][j] - B[i][j]
        if i == j == 0:
            can_make[i][j] = set([abs(x)])
            continue
        st = set()
        if i > 0:
            for y in can_make[i-1][j]:
                if abs(y + x) <= 6400:
                    st.add(abs(y + x))
                st.add(abs(y - x))
        if j > 0:
            for y in can_make[i][j-1]:
                if abs(y + x) <= 6400:
                    st.add(abs(y + x))
                st.add(abs(y - x))
        can_make[i][j] = st
print((min(can_make[-1][-1], key=lambda x: abs(x))))



# Optimized solution
import sys
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)]
can_make = [[None for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        x = A[i][j] - B[i][j]
        if i == j == 0:
            can_make[i][j] = set([abs(x)])
            continue
        st = set()
        ub = 80 * (W - i + H - j)
        if i > 0:
            for y in can_make[i-1][j]:
                if abs(y + x) <= ub:
                    st.add(abs(y + x))
                if abs(y - x) <= ub:
                    st.add(abs(y - x))
        if j > 0:
            for y in can_make[i][j-1]:
                if abs(y + x) <= ub:
                    st.add(abs(y + x))
                if abs(y - x) <= ub:
                    st.add(abs(y - x))
        can_make[i][j] = st
print((min(can_make[-1][-1], key=lambda x: abs(x))))
# Original solution
import sys
input = sys.stdin.readline

h,w = list(map(int,input().split()))
A = []
for i in range(h):
    A.append(list(map(int,input().split())))
B = []
for i in range(h):
    B.append(list(map(int,input().split())))
l = 160*80 + 1
dp = [[[0]*l for _ in range(w+1)] for _ in range(h+1)]

for i in range(h):
    for j in range(w):
        d = abs(A[i][j] - B[i][j])
        if i == 0 and j == 0:
            dp[0][0][d] = 1
        for k in range(l):
            if k+d < l:
                dp[i+1][j][k+d] += dp[i][j][k]
                dp[i][j+1][k+d] += dp[i][j][k]
            dp[i+1][j][abs(k-d)] += dp[i][j][k]
            dp[i][j+1][abs(k-d)] += dp[i][j][k]
for i in range(l+1):
    if dp[h][w-1][i] > 0:
        print(i)
        exit(0)


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

h,w = list(map(int,input().split()))
A = []
for i in range(h):
    A.append(list(map(int,input().split())))
B = []
for i in range(h):
    B.append(list(map(int,input().split())))
l = 80*80 + 1
dp = [[[0]*l for _ in range(w+1)] for _ in range(h+1)]

for i in range(h):
    for j in range(w):
        d = abs(A[i][j] - B[i][j])
        if i == 0 and j == 0:
            dp[0][0][d] = 1
        for k in range(l):
            if k+d < l:
                dp[i+1][j][k+d] += dp[i][j][k]
                dp[i][j+1][k+d] += dp[i][j][k]
            dp[i+1][j][abs(k-d)] += dp[i][j][k]
            dp[i][j+1][abs(k-d)] += dp[i][j][k]
for i in range(l+1):
    if dp[h][w-1][i] > 0:
        print(i)
        exit(0)
# Original solution
import sys
def input():
    return sys.stdin.readline()[:-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)]
d=[[0 for i in range(w)] for j in range(h)]
for i in range(h):
    for j in range(w):
        d[i][j] = abs(a[i][j]-b[i][j])
dp=[[[False for i in range(80*80)] for j in range(w)] for k in range(h)]
dp[0][0][d[0][0]] = True
for i in range(h):
    for j in range(w):
        for dis in range(80*80):
            if dp[i][j][dis]:
                if i+1<h:
                    dp[i+1][j][abs(dis-d[i+1][j])]=True
                    if dis+d[i+1][j]<80*80:
                        dp[i+1][j][dis+d[i+1][j]]=True
                if j+1<w:
                    dp[i][j+1][abs(dis-d[i][j+1])]=True
                    if dis+d[i][j+1]<80*80:
                        dp[i][j+1][dis+d[i][j+1]]=True
for dis in range(80*80):
    if dp[-1][-1][dis]:
        print(dis)
        break

# Optimized solution
import sys
def input():
    return sys.stdin.readline()[:-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)]
d=[[0 for i in range(w)] for j in range(h)]
maxd=1
for i in range(h):
    for j in range(w):
        d[i][j] = abs(a[i][j]-b[i][j])
        maxd=max(maxd,d[i][j])
dp=[[0 for j in range(w)] for k in range(h)]
c=20000
dp[0][0] |= 1 << c+d[0][0]
for i in range(h):
    for j in range(w):
        if i+1<h:
            dp[i+1][j] |= dp[i][j] << d[i+1][j]
            dp[i+1][j] |= dp[i][j] >> d[i+1][j]
        if j+1<w:
            dp[i][j+1] |= dp[i][j] << d[i][j+1]
            dp[i][j+1] |= dp[i][j] >> d[i][j+1]
m=1000000
cnt = 0
for x in bin(dp[h-1][w-1])[2:][::-1]:
    if x=='1':
        m=min(m,abs(cnt-c))
    cnt+=1
print(m)
# 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)]
dp = [[[False]*((H+W)*80) for _ in range(W)] for _ in range(H)]

dp[0][0][abs(A[0][0]-B[0][0])] = True
for h in range(H-1):
    for w in range(W-1):
        v1 = abs(A[h+1][w]-B[h+1][w])
        v2 = abs(A[h][w+1]-B[h][w+1])
        for v in range((H+W)*80):
            if dp[h][w][v]:
                dp[h+1][w][v1+v] = True
                dp[h+1][w][abs(v1-v)] = True
                dp[h][w+1][v2+v] = True
                dp[h][w+1][abs(v2-v)] = True

v0 = abs(A[H-1][W-1]-B[H-1][W-1])
for v in range((H+W)*80):
    if dp[H-1][W-2][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True
    if dp[H-2][W-1][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True

for v in range((H+W)*80):
    if dp[H-1][W-1][v]:
        ans = v
        break

print(ans)

# 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)]
dp1 = [[False]*((H+W)*80) for _ in range(W)]
dp2 = [[False]*((H+W)*80) for _ in range(W)]
for w in range(W):
    dp1[w][0] = True

for h in range(H):
    for w in range(W):
        va = A[h][w]
        vb = B[h][w]
        for v in range((H+W)*80):
            if dp1[w][v]:
                dp2[w][abs(v+va-vb)] = True
                dp2[w][abs(v-va+vb)] = True
        if w!=0:
            for v in range((H+W)*80):
                if dp2[w-1][v]:
                    dp2[w][abs(v+va-vb)] = True
                    dp2[w][abs(v-va+vb)] = True
    dp1 = dp2
    dp2 = [[False]*((H+W)*80) for _ in range(W)]

for v in range((H+W)*80):
    if dp1[W-1][v]:
        ans = v
        break

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

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

D = 80*160
dp = [[[0]*D for i in range(w)] for j in range(h)]
dp[0][0][C[0][0]] = 1
for i in range(h):
    for j in range(w):
        if i+1< h:
            for k in range(D):
                if dp[i][j][k] != 0:
                    dp[i+1][j][k+C[i+1][j]] = 1
                    dp[i+1][j][abs(k-C[i+1][j])] = 1
        if j+1<w:
            for k in range(D):
                if dp[i][j][k] != 0:
                    dp[i][j+1][k+C[i][j+1]] = 1
                    dp[i][j+1][abs(k-C[i][j+1])] = 1

for i in range(D):
    if dp[-1][-1][i] != 0:
        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)]

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

D = 80*80
dp = [[[0]*D for i in range(w)] for j in range(h)]
dp[0][0][C[0][0]] = 1
for i in range(h):
    for j in range(w):
        if i+1< h:
            for k in range(D):
                if dp[i][j][k] != 0:
                    if k+C[i+1][j] < D:
                        dp[i+1][j][k+C[i+1][j]] = 1
                    dp[i+1][j][abs(k-C[i+1][j])] = 1
        if j+1<w:
            for k in range(D):
                if dp[i][j][k] != 0:
                    if k+C[i][j+1] < D:
                        dp[i][j+1][k+C[i][j+1]] = 1
                    dp[i][j+1][abs(k-C[i][j+1])] = 1

for i in range(D):
    if dp[-1][-1][i] != 0:
        print(i)
        exit()
# Original solution
h,w = list(map(int,input().split()))
grid = []
for i in range(h):
    grid.append(list(map(int,input().split())))
for i in range(h):
    lst = list(map(int,input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j]-lst[j])
lst = [[set() for _ in range(w)] for _ in range(h)]
lst[0][0].add(grid[0][0])
for j in range(1,w):
    for x in lst[0][j-1]:
        lst[0][j].add(abs(grid[0][j]-x))
        if grid[0][j] + x <= 6400:
            lst[0][j].add(grid[0][j]+x)
for i in range(1,h):
    for x in lst[i-1][0]:
        lst[i][0].add(abs(grid[i][0]-x))
        if grid[i][0] + x <= 6400:
            lst[i][0].add(grid[i][0]+x)
for i in range(1,h):
    for j in range(1,w):
        for x in lst[i][j-1]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        for x in lst[i-1][j]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
print((min(lst[h-1][w-1])))

# Optimized solution
import time
t1 = time.time()
h,w = list(map(int,input().split()))
grid = []
for i in range(h):
    grid.append(list(map(int,input().split())))
for i in range(h):
    lst = list(map(int,input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j]-lst[j])
lst = [[set() for _ in range(w)] for _ in range(h)]
lst[0][0].add(grid[0][0])
for j in range(1,w):
    for x in lst[0][j-1]:
        lst[0][j].add(abs(grid[0][j]-x))
        if (grid[0][j] + x) <= 6400:
            lst[0][j].add(grid[0][j]+x)
for i in range(1,h):
    for x in lst[i-1][0]:
        lst[i][0].add(abs(grid[i][0]-x))
        if grid[i][0] + x <= 6400:
            lst[i][0].add(grid[i][0]+x)
for i in range(1,h):
    for j in range(1,w):
        for x in lst[i][j-1]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        for x in lst[i-1][j]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        t = time.time()
        if t - t1 > 1.8:
            print((0))
            exit()
print((min(lst[h-1][w-1])))