M = 6400*4

mid = M//2

INF = 10**18



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

As = []

for i in range(H):

    As.append(list(map(int, input().split())))

diffs = []

for i in range(H):

    bs = list(map(int, input().split()))

    diffs.append([abs(a-b) for a,b in zip(As[i],bs)])



# bitset高速化

# 初期値

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

dp[0][0] = 1 << (mid+diffs[0][0]) #プラスマイナス考慮して、下駄をはかせる

for i in range(H):

    for j in range(W):

        # 行方向

        if i+1 < H:

            d = diffs[i+1][j]

            dp[i+1][j] |= dp[i][j] >> d  

            dp[i+1][j] |= dp[i][j] << d  

        # 列方向

        if j+1 < W:

            d = diffs[i][j+1]

            dp[i][j+1] |= dp[i][j] >> d

            dp[i][j+1] |= dp[i][j] << d



ans = INF

for i in range(M):

    if dp[H-1][W-1] >> i & 1:

        # print(i-mid)

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

print(ans)
M = 6400

mid = M//2

INF = 10**18



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

As = []

for i in range(H):

    As.append(list(map(int, input().split())))

diffs = []

for i in range(H):

    bs = list(map(int, input().split()))

    diffs.append([abs(a-b) for a,b in zip(As[i],bs)])



# bitset高速化

# 初期値

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

dp[0][0] = 1 << (mid+diffs[0][0]) #プラスマイナス考慮して、下駄をはかせる

for i in range(H):

    for j in range(W):

        # 行方向

        if i+1 < H:

            d = diffs[i+1][j]

            dp[i+1][j] |= dp[i][j] >> d  

            dp[i+1][j] |= dp[i][j] << d  

        # 列方向

        if j+1 < W:

            d = diffs[i][j+1]

            dp[i][j+1] |= dp[i][j] >> d

            dp[i][j+1] |= dp[i][j] << d



ans = INF

for i in range(M):

    if dp[H-1][W-1] >> i & 1:

        # print(i-mid)

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

print(ans)

M = 6400 * 4
mid = M // 2
INF = 10**18
H, W = list(map(int, input().split()))
As = []
for i in range(H):
    As.append(list(map(int, input().split())))
diffs = []
for i in range(H):
    bs = list(map(int, input().split()))
    diffs.append([abs(a - b) for a, b in zip(As[i], bs)])
# bitset高速化
# 初期値
dp = [[0] * W for j in range(H)]
dp[0][0] = 1 << (mid + diffs[0][0])  # プラスマイナス考慮して、下駄をはかせる
for i in range(H):
    for j in range(W):
        # 行方向
        if i + 1 < H:
            d = diffs[i + 1][j]
            dp[i + 1][j] |= dp[i][j] >> d
            dp[i + 1][j] |= dp[i][j] << d
        # 列方向
        if j + 1 < W:
            d = diffs[i][j + 1]
            dp[i][j + 1] |= dp[i][j] >> d
            dp[i][j + 1] |= dp[i][j] << d
ans = INF
for i in range(M):
    if dp[H - 1][W - 1] >> i & 1:
        # print(i-mid)
        ans = min(ans, abs(i - mid))
print(ans)

M = 6400
mid = M // 2
INF = 10**18
H, W = list(map(int, input().split()))
As = []
for i in range(H):
    As.append(list(map(int, input().split())))
diffs = []
for i in range(H):
    bs = list(map(int, input().split()))
    diffs.append([abs(a - b) for a, b in zip(As[i], bs)])
# bitset高速化
# 初期値
dp = [[0] * W for j in range(H)]
dp[0][0] = 1 << (mid + diffs[0][0])  # プラスマイナス考慮して、下駄をはかせる
for i in range(H):
    for j in range(W):
        # 行方向
        if i + 1 < H:
            d = diffs[i + 1][j]
            dp[i + 1][j] |= dp[i][j] >> d
            dp[i + 1][j] |= dp[i][j] << d
        # 列方向
        if j + 1 < W:
            d = diffs[i][j + 1]
            dp[i][j + 1] |= dp[i][j] >> d
            dp[i][j + 1] |= dp[i][j] << d
ans = INF
for i in range(M):
    if dp[H - 1][W - 1] >> i & 1:
        # print(i-mid)
        ans = min(ans, abs(i - mid))
print(ans)

-M = 6400 * 4 +M = 6400