import itertools

import os

import sys



if os.getenv("LOCAL"):

    sys.stdin = open("_in.txt", "r")



sys.setrecursionlimit(10 ** 9)

INF = float("inf")

IINF = 10 ** 18

MOD = 10 ** 9 + 7

# MOD = 998244353





H, W = list(map(int, sys.stdin.buffer.readline().split()))

A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]



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

for h, w in itertools.product(list(range(H)), list(range(W))):

    grid[h][w] = A[h][w] - B[h][w]



# 符号をどっちにするか選べる

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

dp[0][0] = {grid[0][0] % 800, -(grid[0][0]) % 800}

for h, w in itertools.product(list(range(H)), list(range(W))):

    g = grid[h][w]

    s = dp[h][w]

    if h - 1 >= 0:

        for c in dp[h - 1][w]:

            s.add((c + g) % 800)

            s.add((c - g) % 800)

    if w - 1 >= 0:

        for c in dp[h][w - 1]:

            s.add((c + g) % 800)

            s.add((c - g) % 800)

# print(grid)

# print(dp)

ans = INF

for s in dp[-1][-1]:

    ans = min(ans, s)

    ans = min(ans, (-s) % 800)

print(ans)

import itertools

import os

import sys



import numpy as np



if os.getenv("LOCAL"):

    sys.stdin = open("_in.txt", "r")



sys.setrecursionlimit(10 ** 9)

INF = float("inf")

IINF = 10 ** 18

MOD = 10 ** 9 + 7

# MOD = 998244353





# int で bitset をします

# https://twitter.com/armeria_betrue/status/1203681298826452993



H, W = list(map(int, sys.stdin.buffer.readline().split()))

A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]

B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]



A = np.array(A, dtype=int)

B = np.array(B, dtype=int)

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



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

# 下から n ビットめが 1 ならそこまでで絶対値 n を作れる

dp[0][0] = 1 << (grid[0][0] + (80 * 40 * 40))

for h, w in itertools.product(list(range(H)), list(range(W))):

    g = grid[h][w]

    if h - 1 >= 0:

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

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

    if w - 1 >= 0:

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

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



i = 0

n = dp[-1][-1] >> (80 * 40 * 40)

while True:

    if n & 1:

        print(i)

        break

    n >>= 1

    i += 1

import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353
H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
grid = [[0] * W for _ in range(H)]
for h, w in itertools.product(list(range(H)), list(range(W))):
    grid[h][w] = A[h][w] - B[h][w]
# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0] % 800, -(grid[0][0]) % 800}
for h, w in itertools.product(list(range(H)), list(range(W))):
    g = grid[h][w]
    s = dp[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            s.add((c + g) % 800)
            s.add((c - g) % 800)
# print(grid)
# print(dp)
ans = INF
for s in dp[-1][-1]:
    ans = min(ans, s)
    ans = min(ans, (-s) % 800)
print(ans)

import itertools
import os
import sys
import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")
sys.setrecursionlimit(10**9)
INF = float("inf")
IINF = 10**18
MOD = 10**9 + 7
# MOD = 998244353
# int で bitset をします
# https://twitter.com/armeria_betrue/status/1203681298826452993
H, W = list(map(int, sys.stdin.buffer.readline().split()))
A = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
B = [list(map(int, sys.stdin.buffer.readline().split())) for _ in range(H)]
A = np.array(A, dtype=int)
B = np.array(B, dtype=int)
grid = np.abs(A - B).tolist()
dp = [[0] * W for _ in range(H)]
# 下から n ビットめが 1 ならそこまでで絶対値 n を作れる
dp[0][0] = 1 << (grid[0][0] + (80 * 40 * 40))
for h, w in itertools.product(list(range(H)), list(range(W))):
    g = grid[h][w]
    if h - 1 >= 0:
        dp[h][w] |= dp[h - 1][w] << grid[h][w]
        dp[h][w] |= dp[h - 1][w] >> grid[h][w]
    if w - 1 >= 0:
        dp[h][w] |= dp[h][w - 1] << grid[h][w]
        dp[h][w] |= dp[h][w - 1] >> grid[h][w]
i = 0
n = dp[-1][-1] >> (80 * 40 * 40)
while True:
    if n & 1:
        print(i)
        break
    n >>= 1
    i += 1

+import numpy as np +# int で bitset をします +# https://twitter.com/armeria_betrue/status/1203681298826452993 -grid = [[0] * W for _ in range(H)] -for h, w in itertools.product(list(range(H)), list(range(W))): -    grid[h][w] = A[h][w] - B[h][w] -# 符号をどっちにするか選べる -dp = [[set() for _ in range(W)] for _ in range(H)] -dp[0][0] = {grid[0][0] % 800, -(grid[0][0]) % 800} +A = np.array(A, dtype=int) +B = np.array(B, dtype=int) +grid = np.abs(A - B).tolist() +dp = [[0] * W for _ in range(H)] +# 下から n ビットめが 1 ならそこまでで絶対値 n を作れる +dp[0][0] = 1 << (grid[0][0] + (80 * 40 * 40)) -    s = dp[h][w] -        for c in dp[h - 1][w]: -            s.add((c + g) % 800) -            s.add((c - g) % 800) +        dp[h][w] |= dp[h - 1][w] << grid[h][w] +        dp[h][w] |= dp[h - 1][w] >> grid[h][w] -        for c in dp[h][w - 1]: -            s.add((c + g) % 800) -            s.add((c - g) % 800) -# print(grid) -# print(dp) -ans = INF -for s in dp[-1][-1]: -    ans = min(ans, s) -    ans = min(ans, (-s) % 800) -print(ans) +        dp[h][w] |= dp[h][w - 1] << grid[h][w] +        dp[h][w] |= dp[h][w - 1] >> grid[h][w] +i = 0 +n = dp[-1][-1] >> (80 * 40 * 40) +while True: +    if n & 1: +        print(i) +        break +    n >>= 1 +    i += 1