import sys

import numpy as np



sr = lambda: sys.stdin.readline().rstrip()

ir = lambda: int(sr())

lr = lambda: list(map(int, sr().split()))



H, W = lr()

A = np.array([lr() for _ in range(H)])

B = np.array([lr() for _ in range(H)])

diff = np.abs(A - B).tolist()



X = (H+W) * 80

L = X + X + 1 #真ん中を0とする、最後に-X

dp = [[np.zeros(L, np.bool)] * W for _ in range(H)]

dp[0][0] = np.zeros(L, np.bool)

di = diff[0][0]

dp[0][0][X+di] = 1; dp[0][0][X-di] = 1

for h in range(H):

    for w in range(W):

        if h == 0 and w == 0:

            continue

        di = diff[h][w]

        x = np.zeros(L, np.bool)

        if h > 0:

            prev = dp[h-1][w]

            x[di:] |= prev[:L-di]

            x[:L-di] |= prev[di:]

        if w > 0:

            prev = dp[h][w-1]

            x[di:] |= prev[:L-di]

            x[:L-di] |= prev[di:]

        dp[h][w] = x



dp = dp[-1][-1][X:]

possible = np.nonzero(dp)[0]

answer = possible.min()

print(answer)

import sys

import numpy as np



sr = lambda: sys.stdin.readline().rstrip()

ir = lambda: int(sr())

lr = lambda: list(map(int, sr().split()))



H, W = lr()

A = np.array([lr() for _ in range(H)])

B = np.array([lr() for _ in range(H)])

diff = np.abs(A - B).tolist()



X = (H+W) * 80

L = X + X + 1 #真ん中を0とする、最後に-X

dp = [[np.zeros(L, np.bool) for _ in range(W)] for _ in range(H)]

di = diff[0][0]

dp[0][0][X+di] = 1; dp[0][0][X-di] = 1

for i in range(H):

    for j in range(W):

        if i == 0 and j == 0:

            continue

        di = diff[i][j]

        if i > 0:

            prev = dp[i-1][j]

            dp[i][j][di:] |= prev[:L-di]

            dp[i][j][:L-di] |= prev[di:]

        if j > 0:

            prev = dp[i][j-1]

            dp[i][j][di:] |= prev[:L-di]

            dp[i][j][:L-di] |= prev[di:]



dp = dp[-1][-1][X:]

possible = np.nonzero(dp)[0]

answer = possible.min()

print(answer)

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B).tolist()
X = (H + W) * 80
L = X + X + 1  # 真ん中を0とする、最後に-X
dp = [[np.zeros(L, np.bool)] * W for _ in range(H)]
dp[0][0] = np.zeros(L, np.bool)
di = diff[0][0]
dp[0][0][X + di] = 1
dp[0][0][X - di] = 1
for h in range(H):
    for w in range(W):
        if h == 0 and w == 0:
            continue
        di = diff[h][w]
        x = np.zeros(L, np.bool)
        if h > 0:
            prev = dp[h - 1][w]
            x[di:] |= prev[: L - di]
            x[: L - di] |= prev[di:]
        if w > 0:
            prev = dp[h][w - 1]
            x[di:] |= prev[: L - di]
            x[: L - di] |= prev[di:]
        dp[h][w] = x
dp = dp[-1][-1][X:]
possible = np.nonzero(dp)[0]
answer = possible.min()
print(answer)

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B).tolist()
X = (H + W) * 80
L = X + X + 1  # 真ん中を0とする、最後に-X
dp = [[np.zeros(L, np.bool) for _ in range(W)] for _ in range(H)]
di = diff[0][0]
dp[0][0][X + di] = 1
dp[0][0][X - di] = 1
for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            continue
        di = diff[i][j]
        if i > 0:
            prev = dp[i - 1][j]
            dp[i][j][di:] |= prev[: L - di]
            dp[i][j][: L - di] |= prev[di:]
        if j > 0:
            prev = dp[i][j - 1]
            dp[i][j][di:] |= prev[: L - di]
            dp[i][j][: L - di] |= prev[di:]
dp = dp[-1][-1][X:]
possible = np.nonzero(dp)[0]
answer = possible.min()
print(answer)

-dp = [[np.zeros(L, np.bool)] * W for _ in range(H)] -dp[0][0] = np.zeros(L, np.bool) +dp = [[np.zeros(L, np.bool) for _ in range(W)] for _ in range(H)] -for h in range(H): -    for w in range(W): -        if h == 0 and w == 0: +for i in range(H): +    for j in range(W): +        if i == 0 and j == 0: -        di = diff[h][w] -        x = np.zeros(L, np.bool) -        if h > 0: -            prev = dp[h - 1][w] -            x[di:] |= prev[: L - di] -            x[: L - di] |= prev[di:] -        if w > 0: -            prev = dp[h][w - 1] -            x[di:] |= prev[: L - di] -            x[: L - di] |= prev[di:] -        dp[h][w] = x +        di = diff[i][j] +        if i > 0: +            prev = dp[i - 1][j] +            dp[i][j][di:] |= prev[: L - di] +            dp[i][j][: L - di] |= prev[di:] +        if j > 0: +            prev = dp[i][j - 1] +            dp[i][j][di:] |= prev[: L - di] +            dp[i][j][: L - di] |= prev[di:]