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=[[[0]*(80*(h+w+1)+1) for i in range(w)] for j in range(h)]
  dp[0][0][abs(a[0][0]-b[0][0])]=1
  for i in range(h):
    for j in range(w):
      c=abs(a[i][j]-b[i][j])
      if i!=0 or j!=0:
        for k in range(80*(i+j+1)+1):
          dp[i][j][k]=dp[i-1][j][abs(k-c)]|dp[i-1][j][k+c]|dp[i][j-1][abs(k-c)]|dp[i][j-1][k+c]
  for i in range(80*(h+w+1)+1):
    if dp[h-1][w-1][i]==1:
      print(i)
      break

if __name__=='__main__':
  main()

# Optimized solution
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=[[[0]*(80*(h+w+1)+1) for i in range(w)] for j in range(h)]
  dp[0][0][abs(a[0][0]-b[0][0])]=1
  for i in range(h):
    for j in range(w):
      c=abs(a[i][j]-b[i][j])
      if i!=0 or j!=0:
        for k in range(80*(i+j+1)+1):
          dp[i][j][k]=dp[i-1][j][abs(k-c)]|dp[i-1][j][k+c]|dp[i][j-1][abs(k-c)]|dp[i][j-1][k+c]
  for i in range(80*(h+w+1)+1):
    if dp[h-1][w-1][i]==1:
      print(i)
      break

if __name__=='__main__':
  main()
# Original solution
#!/usr/bin/env python3
import sys


def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):
    ab = [[0]*W for _ in range(H)]

    for h in range(H):
        for w in range(W):
            ab[h][w] = abs(A[h][w]-B[h][w])
    
    # i,jにいるときに作れる値
    setlist = [[set() for _ in range(W)] for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                setlist[i][j].add(ab[i][j])
                continue

            if  i == 0:
                for num in setlist[i][j-1]:
                    setlist[i][j].add(abs(num+ab[i][j]))
                    setlist[i][j].add(abs(num-ab[i][j]))
                continue

            if j == 0:
                for num in setlist[i-1][j]:
                    setlist[i][j].add(abs(num+ab[i][j]))
                    setlist[i][j].add(abs(num-ab[i][j]))
                continue                
            
            for num in setlist[i-1][j]:
                setlist[i][j].add(abs(num+ab[i][j]))
                setlist[i][j].add(abs(num-ab[i][j]))
            for num in setlist[i][j-1]:
                setlist[i][j].add(abs(num+ab[i][j]))
                setlist[i][j].add(abs(num-ab[i][j]))

    print((min(setlist[H-1][W-1])))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    B = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    solve(H, W, A, B)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys

def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):
    SA=[[0]*W for i in range(H)]
    MAX = (H+W)*80
    for i in range(H):
        for j in range(W):
            SA[i][j]=abs(A[i][j]-B[i][j])

    DP=[[0 for i in range(W)] for j in range(H)]
    DP[0][0]=1<<MAX+SA[0][0]

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

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

    ANS=1<<MAX*2
    for i in range(MAX*2+1):
        if 1<<i & DP[-1][-1]!=0:
            ANS=min(ANS,abs(i-MAX))

    print(ANS)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    B = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    solve(H, W, A, B)

if __name__ == '__main__':
    main()
# Original 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)]

c = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]

sets = [[set() for j in range(w)] for i in range(h)]
sets[0][0].add(c[0][0])
for i in range(1, h):
    for e in sets[i - 1][0]:
        sets[i][0].add(e + c[i][0])
        sets[i][0].add(abs(e - c[i][0]))
for i in range(1, w):
    for e in sets[0][i - 1]:
        sets[0][i].add(e+c[0][i])
        sets[0][i].add(abs(e-c[0][i]))
for i in range(1, h):
    for j in range(1, w):
        for e in sets[i-1][j]:
            sets[i][j].add(e+c[i][j])
            sets[i][j].add(abs(e - c[i][j]))
        for e in sets[i][j-1]:
            sets[i][j].add(e+c[i][j])
            sets[i][j].add(abs(e-c[i][j]))

print((min(sets[h - 1][w - 1])))


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

c = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]
mask = (1 << (h+w) * 80) - 1
sets = [[0 for j in range(w)] for i in range(h)]
sets[0][0] = (1 << 80+c[0][0]) | (1 << 80-c[0][0])
for i in range(1, h):
    sets[i][0] |= (sets[i - 1][0] << 80 + c[i][0]
                   ) | (sets[i - 1][0] << 80 - c[i][0])
for i in range(1, w):
    sets[0][i] |= (sets[0][i-1] << 80 + c[0][i]
                   ) | (sets[0][i-1] << 80 - c[0][i])
for i in range(1, h):
    for j in range(1, w):
        sets[i][j] |= (sets[i - 1][j] << 80 + c[i][j]
                       ) | (sets[i - 1][j] << 80 - c[i][j])
        sets[i][j] |= (sets[i][j-1] << 80 + c[i][j]
                       ) | (sets[i][j - 1] << 80 - c[i][j])
    sets[i][j] &= mask

s = bin(sets[h - 1][w - 1] + (1 << (h + w) * 80))
min_v = 1 << 80
for i in range(len(s)):
    if s[-1-i] == '1':
        min_v = min(min_v, abs(i - (h + w) * 80 + 80))
print(min_v)
# Original solution
def main():
    h, w = list(map(int, input().split()))
    grid = [[0] * (w + 1)]
    for _ in range(h):
        grid.append([0] + list(map(int, input().split())))
    for i in range(1, h + 1):
        l = [0] + list(map(int, input().split()))
        for j in range(w + 1):
            grid[i][j] = abs(grid[i][j] - l[j])

    biggest = max([max(i) for i in grid])
    m = (h + w) * biggest

    if m == 0:
        print((0))
        exit()

    unba = [[[False] * m for _ in range(w + 1)] for _ in range(h + 1)]
    # trueplace = [[[] for _ in range(w + 1)] for _ in range(h + 1)]

    for i in range(1, w + 1):
        unba[0][i][0] = True
    for i in range(1, h + 1):
        unba[i][0][0] = True

    for i in range(1, h + 1):
        for j in range(1, w + 1):
            for k in range(m):
                if unba[i-1][j][k] or unba[i][j-1][k]:
                    unba[i][j][grid[i][j] + k] = True
                    unba[i][j][abs(grid[i][j] - k)] = True

    l = [i for i in range(m) if unba[h][w][i]]
    print((min(l)))

main()

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

    h, w = list(map(int, input().split()))
    g = []
    for _ in range(h):
        g.append(list(map(int, input().split())))
    for i in range(h):
        k = list(map(int, input().split()))
        for j in range(w):
            g[i][j] = abs(g[i][j] - k[j])

    m = (h + w) * 80

    if m == 0:
        print((0))
        exit()

    dp = [[False] * m for _ in range(w)]
    dp[0][g[0][0]] = True
    for j in range(1, w):
        for k in range(m):
            if dp[j-1][k]:
                dp[j][abs(k - g[0][j])] = True
                dp[j][k + g[0][j]] = True
    
    for i in range(1, h):
        new_dp = [[False] * m for _ in range(w)]
        for k in range(m):
            if dp[0][k]:
                new_dp[0][abs(k - g[i][0])] = True
                new_dp[0][k + g[i][0]] = True
        
        for j in range(1, w):
            for k in range(m):
                if new_dp[j-1][k] or dp[j][k]:
                    new_dp[j][abs(k - g[i][j])] = True
                    new_dp[j][k + g[i][j]] = True
        dp = new_dp

    for j in range(m):
        if dp[w-1][j]:
            print(j)
            break

main()
# Original solution
def main():
    from sys import stdin
    def input():
        return stdin.readline().strip()

    h, w = list(map(int, input().split()))
    g = []
    for _ in range(h):
        g.append(list(map(int, input().split())))
    for i in range(h):
        k = list(map(int, input().split()))
        for j in range(w):
            g[i][j] = abs(g[i][j] - k[j])

    m = (h + w) * 80

    if m == 0:
        print((0))
        exit()

    dp = [[False] * m for _ in range(w)]
    dp[0][g[0][0]] = True
    for j in range(1, w):
        for k in range(m):
            if dp[j-1][k]:
                dp[j][abs(k - g[0][j])] = True
                dp[j][k + g[0][j]] = True
    
    for i in range(1, h):
        new_dp = [[False] * m for _ in range(w)]
        for k in range(m):
            if dp[0][k]:
                new_dp[0][abs(k - g[i][0])] = True
                new_dp[0][k + g[i][0]] = True
        
        for j in range(1, w):
            for k in range(m):
                if new_dp[j-1][k] or dp[j][k]:
                    new_dp[j][abs(k - g[i][j])] = True
                    new_dp[j][k + g[i][j]] = True
        dp = new_dp

    for j in range(m):
        if dp[w-1][j]:
            print(j)
            break

main()

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

    h, w = list(map(int, input().split()))
    g = []
    for _ in range(h):
        g.append(list(map(int, input().split())))
    for i in range(h):
        k = list(map(int, input().split()))
        for j in range(w):
            g[i][j] = abs(g[i][j] - k[j])

    m = (h + w) * 50

    if m == 0:
        print((0))
        exit()

    dp = [[False] * m for _ in range(w)]
    dp[0][g[0][0]] = True
    for j in range(1, w):
        for k in range(m):
            if dp[j-1][k]:
                dp[j][abs(k - g[0][j])] = True
                try:
                    dp[j][k + g[0][j]] = True
                except:
                    pass
    
    for i in range(1, h):
        new_dp = [[False] * m for _ in range(w)]
        for k in range(m):
            if dp[0][k]:
                new_dp[0][abs(k - g[i][0])] = True
                try:
                    new_dp[0][k + g[i][0]] = True
                except:
                    pass
        
        for j in range(1, w):
            for k in range(m):
                if new_dp[j-1][k] or dp[j][k]:
                    new_dp[j][abs(k - g[i][j])] = True
                    try:
                        new_dp[j][k + g[i][j]] = True
                    except:
                        pass
        dp = new_dp

    for j in range(m):
        if dp[w-1][j]:
            print(j)
            break

main()
# Original solution
import copy
def main():
    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)]
    n = 2*80*160 + 10
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            if 0 < i and 0 < h:
                for k in range(x, n-x):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(x, n-x):
                    if dp[i-1][j][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(x, n-x):
                    if dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(n):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

if __name__ == "__main__":
    main()


# Optimized solution
def main():
    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)]
    n = 2000
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            if 0 < i and 0 < h:
                for k in range(x, n-x):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(x, n-x):
                    if dp[i-1][j][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(x, n-x):
                    if dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(n):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    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)]

C = [[0 for j in range(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])

dp = [[[0 for k in range(6400)] for j in range(W)] for i in range(H)]
dp[0][0][C[0][0]] = 1

for i in range(H):
    for j in range(W):
        for k in range(6400):
            if not dp[i][j][k]:
                continue
            ni,nj = i+1,j+1
            if ni < H:
                if k+C[ni][j]<6400:
                    dp[ni][j][k+C[ni][j]] = 1
                dp[ni][j][abs(k-C[ni][j])] = 1
            if nj < W:
                if k+C[i][nj]<6400:
                    dp[i][nj][k+C[i][nj]] = 1
                dp[i][nj][abs(k-C[i][nj])] = 1

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

# Optimized solution
def main():

    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 for j in range(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])

    dp = [[0 for k in range(6400)] for g in range(H*W)]
    dp[0][C[0][0]] = 1

    for g in range(H*W):
        i,j = divmod(g,W)
        for k in range(6400):
            if not dp[i*W+j][k]:
                continue
            ni,nj = i+1,j+1
            if ni < H:
                if k+C[ni][j]<6400:
                    dp[ni*W+j][k+C[ni][j]] = 1
                dp[ni*W+j][abs(k-C[ni][j])] = 1
            if nj < W:
                if k+C[i][nj]<6400:
                    dp[i*W+nj][k+C[i][nj]] = 1
                dp[i*W+nj][abs(k-C[i][nj])] = 1

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

main()
# Original solution
def main():
    import sys

    H, W = list(map(int, sys.stdin.readline().split()))
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = {}
    dp[0] = set()
    dp[0].add(X[0][0])
    dp[0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            threshold = ((H - h) + (W - w)) * 79
            if W * h + w > W - 1:
                for d in dp[W * (h - 1) + w]:

                    if d + x <= threshold:
                        s.add(d + x)
                    if d - x >= -threshold:
                        s.add(d - x)

            if (W * h + w) % W != 0:
                for d in dp[W * h + w - 1]:
                    if d + x <= threshold:
                        s.add(d + x)
                    if d - x >= -threshold:
                        s.add(d - x)

            dp[W * h + w] = s

    ans = min([abs(s) for s in dp[W * H - 1]])
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    import sys

    H, W = list(map(int, sys.stdin.readline().split()))
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

    ans = min([abs(s) for s in dp[H - 1][W - 1]])
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
def main():  
  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 = []
  for i in range(h):
    s = list(map(int,readline().split()))
    a.append(s)
  b = []
  for i in range(h):
    t = list(map(int,readline().split()))
    b.append(t)
  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])

  dp = [[[0]*6321 for i in range(w)] for _ in range(h)]
  dp[0][0][C[0][0]] = 1

  for i in range(h):
    for j in range(w):
      for k in range(6321):
        if dp[i][j][k]:
          if i < h-1:
            c = C[i+1][j]
            if k+c < 6321:
              dp[i+1][j][k+c] = 1
            dp[i+1][j][abs(k-c)] = 1
          if j < w-1:
            d = C[i][j+1]
            if k+d < 6321:
              dp[i][j+1][k+d] = 1
            dp[i][j+1][abs(k-d)] = 1

  for i in range(6321):
    if dp[h-1][w-1][i]:
      print(i)
      break
      
if __name__ == "__main__":
    main()

# Optimized solution
import sys
input = sys.stdin.readline
h,w = list(map(int,input().split()))
a = []
for i in range(h):
  s = list(map(int,input().split()))
  a.append(s)
  
b = []
for i in range(h):
  s = list(map(int,input().split()))
  b.append(s)
  
dp = [[0]*w for i in range(h)]
t = 80*(h+w)
dp[0][0] = 2**(t+abs(a[0][0]-b[0][0]))

for i in range(h):
  for j in range(w):
    r = abs(a[i][j]-b[i][j])
    if j > 0:
      dp[i][j] |= (dp[i][j-1]<<r | dp[i][j-1]>>r)
    if i > 0:
      dp[i][j] |= (dp[i-1][j]<<r | dp[i-1][j]>>r)
      
a = dp[h-1][w-1]>>t
m = a&(-a)
print((m.bit_length()-1))
# Original solution
H, W = list(map(int, input().split()))
A = [None]*H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [None]*H
for i in range(H):
    B[i] = list(map(int, input().split()))

# C: A-Bの絶対値を持つ
C = [[None]*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])

INF = 10**12
K = 2*80*(H+W-1)
dp = [[[0]*(K+1) for _ in range(W)] for _ in range(H)]# 答えから80*(H+W-1)を引く
dp[0][0][80*(H+W-1)+C[0][0]] = 1
dp[0][0][80*(H+W-1)-C[0][0]] = 1

for i in range(H):
    for j in range(W):
        for k in range(K+1):
            if i > 0 and dp[i-1][j][k] > 0:
                dp[i][j][k+C[i][j]] += dp[i-1][j][k]
                dp[i][j][k-C[i][j]] += dp[i-1][j][k]
            if j > 0 and dp[i][j-1][k] > 0:
                dp[i][j][k+C[i][j]] += dp[i][j-1][k]
                dp[i][j][k-C[i][j]] += dp[i][j-1][k]
ans = INF
for k in range(K+1):
    res = dp[H-1][W-1][k]
    if res > 0:
        ans = min(ans, abs(k-80*(H+W-1)))
print(ans)

# Optimized solution
H, W = list(map(int, input().split()))
A = [None]*H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [None]*H
for i in range(H):
    B[i] = list(map(int, input().split()))

# C: A-Bの絶対値を持つ
C = [[None]*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])

K = 80*(H+W-1)
dp = [[[0]*(K+1) for _ in range(W)] for _ in range(H)]
dp[0][0][C[0][0]] = 1

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

for k in range(K+1):
    res = dp[H-1][W-1][k]
    if res:
        print(k)
        exit()
# Original solution
H, W = list(map(int, input().split()))
A = [None]*H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [None]*H
for i in range(H):
    B[i] = list(map(int, input().split()))

# C: A-Bの絶対値を持つ
C = [[None]*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])

K = 80*(H+W-1)
dp = [[[0]*(K+1) for _ in range(W)] for _ in range(H)]
dp[0][0][C[0][0]] = 1

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

for k in range(K+1):
    res = dp[H-1][W-1][k]
    if res:
        print(k)
        exit()

# Optimized solution
H, W = list(map(int, input().split()))
A = [None]*H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [None]*H
for i in range(H):
    B[i] = list(map(int, input().split()))

# C: A-Bの絶対値を持つ
C = [[None]*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])

K = 80*(H+W-1)//2 + 1# 80*(H+W-1)の半分を超えるのは無駄
dp = [[[0]*(K+1) for _ in range(W)] for _ in range(H)]# 答えから80*(H+W-1)を引く
dp[0][0][C[0][0]] = 1

for i in range(H):
    for j in range(W):
        for k in range(K+1):
            if i and dp[i-1][j][k]:
                if k+C[i][j] < K:
                    dp[i][j][k+C[i][j]] = 1
                dp[i][j][abs(k-C[i][j])] = 1
            if j and dp[i][j-1][k]:
                if k+C[i][j] < K:
                    dp[i][j][k+C[i][j]] = 1
                dp[i][j][abs(k-C[i][j])] = 1

for k in range(K+1):
    res = dp[H-1][W-1][k]
    if res:
        print(k)
        break