import numpy as np

H, W = list(map(int, input().split()))

A = np.array([list(map(int, input().split())) for _ in range(H)])

B = np.array([list(map(int, input().split())) for _ in range(H)])



C = abs(A - B)

# print (C)



D = 80 * (H + W + 1) + 10

D2 = D * 2



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



dp[0][0] = 1 << (D - int(C[0][0]))

# print (dp[0][0])

dp[0][0] |= 1 << (D + int(C[0][0]))

# print ((dp[0][0]))







# print (dp[0][0])

dp = np.array(dp)



for h in range(H):

    for w in range(W):

        tmp = int(C[h][w])

        if h:

            dp[h][w] |= dp[h - 1][w] << tmp

            dp[h][w] |= dp[h - 1][w] >> tmp

        if w:

            dp[h][w] |= dp[h][w - 1] << tmp

            dp[h][w] |= dp[h][w - 1] >> tmp



        

ans = D2

# print (ans)

tmp = dp[H - 1][W - 1]

for i in range(D2):

    if (tmp >> i) & 1:

        ans = min(ans, abs(i - D))



print (ans)



H, W = list(map(int, input().split()))



A = [tuple(map(int, input().split())) for _ in range(H)]

B = [tuple(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)]



D = 80 * (H + W) + 1

D2 = D * 2



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



dp[0][0] |= 1 << (D + C[0][0])

dp[0][0] != 1 << (D - C[0][0])



for h in range(H):

    for w in range(W):

        tmp = C[h][w]

        if h:

            dp[h][w] |= dp[h - 1][w] << tmp

            dp[h][w] |= dp[h - 1][w] >> tmp

        if w:

            dp[h][w] |= dp[h][w - 1] << tmp

            dp[h][w] |= dp[h][w - 1] >> tmp



ans = D2



tmp = dp[H - 1][W - 1]



for i in range(D2):

    if (tmp >> i) & 1:

        ans = min(ans, abs(i - D))



print (ans)
import numpy as np

H, W = list(map(int, input().split()))
A = np.array([list(map(int, input().split())) for _ in range(H)])
B = np.array([list(map(int, input().split())) for _ in range(H)])
C = abs(A - B)
# print (C)
D = 80 * (H + W + 1) + 10
D2 = D * 2
dp = [[0] * (W) for _ in range(H)]
dp[0][0] = 1 << (D - int(C[0][0]))
# print (dp[0][0])
dp[0][0] |= 1 << (D + int(C[0][0]))
# print ((dp[0][0]))
# print (dp[0][0])
dp = np.array(dp)
for h in range(H):
    for w in range(W):
        tmp = int(C[h][w])
        if h:
            dp[h][w] |= dp[h - 1][w] << tmp
            dp[h][w] |= dp[h - 1][w] >> tmp
        if w:
            dp[h][w] |= dp[h][w - 1] << tmp
            dp[h][w] |= dp[h][w - 1] >> tmp
ans = D2
# print (ans)
tmp = dp[H - 1][W - 1]
for i in range(D2):
    if (tmp >> i) & 1:
        ans = min(ans, abs(i - D))
print(ans)

H, W = list(map(int, input().split()))
A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(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)]
D = 80 * (H + W) + 1
D2 = D * 2
dp = [[0] * W for _ in range(H)]
dp[0][0] |= 1 << (D + C[0][0])
dp[0][0] != 1 << (D - C[0][0])
for h in range(H):
    for w in range(W):
        tmp = C[h][w]
        if h:
            dp[h][w] |= dp[h - 1][w] << tmp
            dp[h][w] |= dp[h - 1][w] >> tmp
        if w:
            dp[h][w] |= dp[h][w - 1] << tmp
            dp[h][w] |= dp[h][w - 1] >> tmp
ans = D2
tmp = dp[H - 1][W - 1]
for i in range(D2):
    if (tmp >> i) & 1:
        ans = min(ans, abs(i - D))
print(ans)

-import numpy as np - -A = np.array([list(map(int, input().split())) for _ in range(H)]) -B = np.array([list(map(int, input().split())) for _ in range(H)]) -C = abs(A - B) -# print (C) -D = 80 * (H + W + 1) + 10 +A = [tuple(map(int, input().split())) for _ in range(H)] +B = [tuple(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)] +D = 80 * (H + W) + 1 -dp = [[0] * (W) for _ in range(H)] -dp[0][0] = 1 << (D - int(C[0][0])) -# print (dp[0][0]) -dp[0][0] |= 1 << (D + int(C[0][0])) -# print ((dp[0][0])) -# print (dp[0][0]) -dp = np.array(dp) +dp = [[0] * W for _ in range(H)] +dp[0][0] |= 1 << (D + C[0][0]) +dp[0][0] != 1 << (D - C[0][0]) -        tmp = int(C[h][w]) +        tmp = C[h][w] -# print (ans)