CodeEff/ECCO huggingface dataset:
# Original solution
H, W = list(map(int, input().split()))
AB = []
for i in range(H):
  AB.append(list(map(int, input().split())))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

K = 80*H*W
dp = [[[False]*K for w in range(W)] for h in range(H)]
dp[0][0][abs(AB[0][0])] = True


for h in range(H):
  for w in range(W):
    if h == 0:
      if w == 0:
        continue
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2]
    else:
      if w == 0:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h-1][w][k2]
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2] or dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2] or dp[h-1][w][k2]

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

# Optimized solution
H, W = list(map(int, input().split()))
AB = []
for i in range(H):
  AB.append(list(map(int, input().split())))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

K = 80*(H + W)
dp = [[[False]*K for w in range(W)] for h in range(H)]
dp[0][0][abs(AB[0][0])] = True


for h in range(H):
  for w in range(W):
    if h == 0:
      if w == 0:
        continue
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2]
    else:
      if w == 0:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h-1][w][k2]
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2] or dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2] or dp[h-1][w][k2]

for k in range(K):
  if dp[H-1][W-1][k]:
    print(k)
    break
# Original solution
H, W = list(map(int, input().split()))
AB = []
max_ab = 0
for i in range(H):
  AB.append(list(map(int, input().split())))
  max_ab = max(max_ab, max(AB[-1]))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

K = max_ab*(H + W) + 1
dp = [[[False]*K for w in range(W+1)] for h in range(H+1)]
dp[0][0][0] = True
dp[0][1][0] = True
dp[1][0][0] = True

for h in range(H):
  for w in range(W):
    ab = AB[h][w]
    for i, tp in enumerate(dp[h][w+1]):
      if not tp:
        continue
        
      dp[h+1][w+1][abs(i+ab)] = True
      dp[h+1][w+1][abs(i-ab)] = True
      
    for i, tp in enumerate(dp[h+1][w]):
      if not tp:
        continue
      
      dp[h+1][w+1][abs(i+ab)] = True
      dp[h+1][w+1][abs(i-ab)] = True

for k in range(K):
  if dp[H][W][k]:
    print(k)
    break

# Optimized solution
H, W = list(map(int, input().split()))
AB = []
max_ab = 0
for i in range(H):
  AB.append(list(map(int, input().split())))
  max_ab = max(max_ab, max(AB[-1]))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

base = max_ab*(H + W)
dp = [[1 << base for w in range(W)] for h in range(H)]
dp[0][0] = (1 << base + AB[0][0]) | (1 << base - AB[0][0])
for h in range(1, H):
  dp[h][0] = (dp[h-1][0] << AB[h][0]) | (dp[h-1][0] >> AB[h][0])

for w in range(1, W):
  dp[0][w] = (dp[0][w-1] << AB[0][w]) | (dp[0][w-1] >> AB[0][w])

for h in range(1, H):
  for w in range(1, W):
    dp[h][w] = (
      (dp[h-1][w] << AB[h][w]) | (dp[h-1][w] >> AB[h][w]) 
      | (dp[h][w-1] << AB[h][w]) | (dp[h][w-1] >> AB[h][w]))

positive = bin(dp[H-1][W-1] >> base)
print((positive[::-1].find('1')))
# 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]*W for i in range(H)]
mx = 1
for i in range(H):
    for j in range(W):
        D[i][j] = abs(A[i][j]-B[i][j])
        mx = max(mx, D[i][j])
MX = mx*(H+W)
dp = [[[0]*(MX+1) for _ in range(W+1)] for _ in range(H+1)]
dp[1][1][D[0][0]] = 1
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in range(MX+1):
            if dp[i][j][k]:
                if j+1 <= W:
                    d = D[i-1][j]
                    if k+d <= MX:
                        dp[i][j+1][k+d] = 1
                    dp[i][j+1][abs(k-d)] = 1
                if i+1 <= H:
                    d = D[i][j-1]
                    if k+d <= MX:
                        dp[i+1][j][k+d] = 1
                    dp[i+1][j][abs(k-d)] = 1
#for i in range(H+1):
#    for j in range(W+1):
#        print(i, j)
#        print(dp[i][j])
for i in range(MX+1):
    if dp[H][W][i]:
        print(i)
        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]*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])
buf = 80*(H+W)
dp = [[0]*W for i in range(H)]
dp[0][0] = 1<<(buf+D[0][0]) | 1<<(buf-D[0][0])
for i in range(H):
    for j in range(W):
        d = D[i][j]
        if i:
            dp[i][j] |= dp[i-1][j]<<d | dp[i-1][j]>>d
        if j:
            dp[i][j] |= dp[i][j-1]<<d | dp[i][j-1]>>d
ans = 10**18
for i in range(buf*2+1):
    if dp[H-1][W-1]&(1<<i):
        ans = min(ans, abs(buf-i))
print(ans)
# Original solution
import sys
from collections import defaultdict
input = sys.stdin.readline
H, W = list(map(int, input().split()))
A, B = [None] * H, [None] * H
for i in range(H):
    A[i] = list(map(int, input().split()))
for i in range(H):
    B[i] = list(map(int, input().split()))
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
M = 80*(80+80)
dp = [[defaultdict(lambda :False) for j in range(W+1)] for i in range(H+1)]
dp[0][1][0] = True
dp[1][0][0] = True
for i in range(1, H+1):
    for j in range(1, W+1):
        d = C[i-1][j-1]
        for k in range((i+j-1)*80):
            if dp[i-1][j][abs(k-d)] or dp[i-1][j][k+d] or dp[i][j-1][abs(k-d)] or dp[i][j-1][k+d]:
                dp[i][j][k] = True

for i in range(M):
    if i in dp[H][W]:
        print(i)
        break


# Optimized solution
import sys
input = sys.stdin.readline
H, W = list(map(int, input().split()))
A, B = [None] * H, [None] * H
for i in range(H):
    A[i] = list(map(int, input().split()))
for i in range(H):
    B[i] = list(map(int, input().split()))
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
M = 80*(80+80)
dp = [[0 for j in range(W+1)] for i in range(H+1)]
dp[0][1] = 1
dp[1][0] = 1
for i in range(1, H+1):
    for j in range(1, W+1):
        d = C[i-1][j-1]
        if d != 0:
            x = int(bin((dp[i-1][j]&(2**d-1))+2**d)[::-1][:d], 2)<<1
            y = int(bin((dp[i][j-1]&(2**d-1))+2**d)[::-1][:d], 2)<<1
        else:
            x=y=0
        #print(bin(dp[i-1][j]), bin(dp[i-1][j]+(2**10)))
        dp[i][j] = (dp[i-1][j] >> d) | (dp[i-1][j] << d) | (dp[i][j-1] >> d) | (dp[i][j-1] << d) | x | y
        #print(i, j, d, bin(dp[i][j]))

for i, v in enumerate(bin(dp[H][W])[::-1]):
    if v == '1':
        print(i)
        break
# Original solution
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):
    base = [[None]*W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            base[h][w] = abs(A[h][w]-B[h][w])

    table = [[0 for _ in range(W)] for _ in range(H)]
    table[0][0] |= (1 << base[0][0])
    for h in range(H):
        for w in range(W):
            if h > 0:
                for i in range(6401):
                    j = (table[h-1][w] >> i) & 1
                    if j == 0:
                        continue
                    if base[h][w]+i < 6401:
                        table[h][w] |= 1 << abs(i+base[h][w])
                    table[h][w] |= 1 << abs(i-base[h][w])
            if w > 0:
                for i in range(6401):
                    j = (table[h][w-1] >> i) & 1
                    if j == 0:
                        continue
                    if base[h][w]+i < 6401:
                        table[h][w] |= 1 << abs(i+base[h][w])
                    table[h][w] |= 1 << abs(i-base[h][w])
    for j in range(6401):
        i = (table[H-1][W-1] >> j) & 1
        if i == 1:
            print(j)
            return

    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
INF = float("inf")


def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):
    base = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
    mx = (1 << 12800) - 1
    table = [[0]*W for _ in range(H)]
    table[0][0] = (1 << (6400 + base[0][0])) | (1 << (6400-base[0][0]))
    for h in range(H):
        for w in range(W):
            if h > 0:
                table[h][w] |= (table[h-1][w] << base[h][w])
                table[h][w] |= (table[h-1][w] >> base[h][w])
            if w > 0:
                table[h][w] |= (table[h][w-1] << base[h][w])
                table[h][w] |= (table[h][w-1] >> base[h][w])
            table[h][w] &= mx

    m = INF
    for j in range(12800):
        i = (table[H-1][W-1] >> j) & 1
        if i == 1:
            m = min(m, abs(j-6400))
    print(m)
    # print(table[H-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()
# Original solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    G = [[0] * W for _ in range(H)]
    M = 0
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    L = (H + W) * M
    dp = [[[False] * (L + 1) for j in range(W + 1)] for i in range(H + 1)]
    dp[0][1][0] = dp[1][0][0] = True

    for i in range(H):
        for j in range(W):
            b = G[i][j]
            for k in range(L + 1):
                if abs(k + b) <= L:
                    dp[i + 1][j + 1][k] = (
                        dp[i][j + 1][abs(k - b)]
                        or dp[i][j + 1][abs(k + b)]
                        or dp[i + 1][j][abs(k - b)]
                        or dp[i + 1][j][abs(k + b)]
                    )
                else:
                    dp[i + 1][j + 1][k] = dp[i][j + 1][abs(k - b)] or dp[i + 1][j][abs(k - b)]

    ans = INF
    for k in range(L + 1):
        if dp[H][W][k]:
            ans = k
            break

    print(ans)
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    G = [[0] * W for _ in range(H)]
    M = 0
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

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

    for i in range(H):
        for j in range(W):
            b = G[i][j]
            for k in dp[i + 1][j]:
                dp[i + 1][j + 1].add(k + b)
                dp[i + 1][j + 1].add(abs(k - b))
            for k in dp[i][j + 1]:
                dp[i + 1][j + 1].add(k + b)
                dp[i + 1][j + 1].add(abs(k - b))

    print((min(dp[H][W])))
    return


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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    G = [[0] * W for _ in range(H)]
    M = 0
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

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

    for i in range(H):
        for j in range(W):
            b = G[i][j]
            for k in dp[i + 1][j]:
                dp[i + 1][j + 1].add(k + b)
                dp[i + 1][j + 1].add(abs(k - b))
            for k in dp[i][j + 1]:
                dp[i + 1][j + 1].add(k + b)
                dp[i + 1][j + 1].add(abs(k - b))

    print((min(dp[H][W])))
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    G = [[0] * W for _ in range(H)]
    M = 0
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

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

    for i in range(H):
        for j in range(W):
            s = dp[i + 1][j + 1]
            b = G[i][j]
            for k in dp[i + 1][j]:
                s.add(k + b)
                s.add(abs(k - b))
            for k in dp[i][j + 1]:
                s.add(k + b)
                s.add(abs(k - b))

    print((min(dp[H][W])))
    return


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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    M = 0
    G = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    M = 20000
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

    for i in range(H):
        for j in range(W):
            if i + 1 < H:
                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j])
            if j + 1 < W:
                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1])

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    M = 0
    G = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    M *= max(H, W)
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

    for i in range(H):
        for j in range(W):
            if i + 1 < H:
                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j])
            if j + 1 < W:
                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1])

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    M = 0
    G = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    M *= max(H, W)
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

    for i in range(H):
        for j in range(W):
            if i + 1 < H:
                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j])
            if j + 1 < W:
                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1])

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()


# Optimized solution
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    M = 0
    G = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    M *= max(H, W)
    dp = [0] * W
    dp[0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

    for i in range(H):
        for j in range(W):
            if j + 1 < W:
                dp[j + 1] |= (dp[j] >> G[i][j + 1]) | (dp[j] << G[i][j + 1])
            if i + 1 < H:
                dp[j] = (dp[j] >> G[i + 1][j]) | (dp[j] << G[i + 1][j])

    ans = INF
    for i in range(2 * M + 2):
        if dp[W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


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 i in range(H)]
B = [list(map(int,input().split())) for i in range(H)]

C = [[0]*W for i in range(H)]
M = 0

for h in range(H):
    for w in range(W):
        C[h][w] = abs(A[h][w] - B[h][w])
        M = max(M,C[h][w])
        
#print(C)

ZZZ = M*(H+W-1)+1

DP = [[[0]*ZZZ for i in range(W)] for j in range(H)]

for h in range(H):
    for w in range(W):
        if h==0 and w==0:
            DP[h][w][C[h][w]] = 1
        elif h==0:
            for i in range(ZZZ):
                DP[h][w][i] = DP[h][w-1][abs(i-C[h][w])]| (DP[h][w-1][abs(i+C[h][w])] if abs(i+C[h][w])<ZZZ else 0)
        elif w==0:
            for i in range(ZZZ):
                DP[h][w][i] = DP[h-1][w][abs(i-C[h][w])]|(DP[h-1][w][abs(i+C[h][w])] if abs(i+C[h][w])<ZZZ else 0)
        else:
            for i in range(ZZZ):
                DP[h][w][i] = DP[h-1][w][abs(i-C[h][w])]|(DP[h-1][w][abs(i+C[h][w])] if abs(i+C[h][w])<ZZZ else 0)|DP[h][w-1][abs(i-C[h][w])]|(DP[h][w-1][abs(i+C[h][w])] if abs(i+C[h][w])<ZZZ else 0)
                
for i in range(ZZZ):
    if DP[H-1][W-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)]
M = 0

for h in range(H):
    for w in range(W):
        C[h][w] = abs(A[h][w] - B[h][w])
        
#print(C)

c = 80*(H+W-1)+1

DP = [[0]*W for j in range(H)]

for h in range(H):
    for w in range(W):
        if h==0 and w==0:
            DP[h][w] = 1 << (C[h][w]+c)
        elif h==0:
            DP[h][w] = (DP[h][w-1] << C[h][w]) | (DP[h][w-1] >> C[h][w])
        elif w==0:
            DP[h][w] = (DP[h-1][w] << C[h][w]) | (DP[h-1][w] >> C[h][w])
        else:
            DP[h][w] = (DP[h][w-1] << C[h][w]) | (DP[h][w-1] >> C[h][w]) | (DP[h-1][w] << C[h][w]) | (DP[h-1][w] >> C[h][w])

ans = float('inf')
now = DP[H-1][W-1]
tmpans = -c
while now != 0:
    if now%2==1:
        ans = min(ans,abs(tmpans))
    tmpans += 1
    now = now >> 1

print(ans)