h, w = list(map(int, input().split()))

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

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

grid = [[0] * w for _ in range(h)]

for i in range(h):

    for j in range(w):

        m = abs(A[i][j] - B[i][j])

        grid[i][j] = m



bias_inf = 2000

dp = [[[0 for i in range(-bias_inf,bias_inf)] for j in range(w)] for k in range(h)]

dp[0][0][0 - grid[0][0]] = 1

dp[0][0][0 + grid[0][0]] = 1

for i in range(h):

    for j in range(w):

        for k in range(-bias_inf,bias_inf):

            if i == h-1 and j == w-1:

                break

            if dp[i][j][k]:

                if i != h-1 and j != w-1:

                    dp[i][j+1][k + grid[i][j+1]] = 1

                    dp[i+1][j][k + grid[i+1][j]] = 1

                    dp[i][j+1][k - grid[i][j+1]] = 1

                    dp[i+1][j][k - grid[i+1][j]] = 1

                elif j == w-1:

                    dp[i+1][j][k - grid[i+1][j]] = 1

                    dp[i+1][j][k + grid[i+1][j]] = 1

                else:

                    dp[i][j+1][k - grid[i][j+1]] = 1

                    dp[i][j+1][k + grid[i][j+1]] = 1

ans = bias_inf

for i in range(-bias_inf,bias_inf):

    if dp[-1][-1][i]:

        ans = min(ans, abs(i))

print(ans)
def solve(h, w, A, B):

    center = 80 * (h + w - 1)

    C = [[abs(A[r][c] - B[r][c]) for c in range(w)] for r in range(h)]

    dp = [[0] * w for r in range(h)]

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

    for r in range(h):

        for c in range(w):

            if r > 0:

                dp[r][c] |= (dp[r-1][c] << C[r][c]) | (dp[r-1][c] >> C[r][c])

            if c > 0:

                dp[r][c] |= (dp[r][c-1] << C[r][c]) | (dp[r][c-1] >> C[r][c])

    mask = bin(dp[-1][-1] >> center)

    return mask[::-1].find("1")

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)]

print((solve(h, w, A, B)))

h, w = list(map(int, input().split()))
A = [list(map(int, input().split())) for _ in range(h)]
B = [list(map(int, input().split())) for _ in range(h)]
grid = [[0] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        m = abs(A[i][j] - B[i][j])
        grid[i][j] = m
bias_inf = 2000
dp = [[[0 for i in range(-bias_inf, bias_inf)] for j in range(w)] for k in range(h)]
dp[0][0][0 - grid[0][0]] = 1
dp[0][0][0 + grid[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k in range(-bias_inf, bias_inf):
            if i == h - 1 and j == w - 1:
                break
            if dp[i][j][k]:
                if i != h - 1 and j != w - 1:
                    dp[i][j + 1][k + grid[i][j + 1]] = 1
                    dp[i + 1][j][k + grid[i + 1][j]] = 1
                    dp[i][j + 1][k - grid[i][j + 1]] = 1
                    dp[i + 1][j][k - grid[i + 1][j]] = 1
                elif j == w - 1:
                    dp[i + 1][j][k - grid[i + 1][j]] = 1
                    dp[i + 1][j][k + grid[i + 1][j]] = 1
                else:
                    dp[i][j + 1][k - grid[i][j + 1]] = 1
                    dp[i][j + 1][k + grid[i][j + 1]] = 1
ans = bias_inf
for i in range(-bias_inf, bias_inf):
    if dp[-1][-1][i]:
        ans = min(ans, abs(i))
print(ans)

def solve(h, w, A, B):
    center = 80 * (h + w - 1)
    C = [[abs(A[r][c] - B[r][c]) for c in range(w)] for r in range(h)]
    dp = [[0] * w for r in range(h)]
    dp[0][0] = (1 << (center + C[0][0])) | (1 << (center - C[0][0]))
    for r in range(h):
        for c in range(w):
            if r > 0:
                dp[r][c] |= (dp[r - 1][c] << C[r][c]) | (dp[r - 1][c] >> C[r][c])
            if c > 0:
                dp[r][c] |= (dp[r][c - 1] << C[r][c]) | (dp[r][c - 1] >> C[r][c])
    mask = bin(dp[-1][-1] >> center)
    return mask[::-1].find("1")


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)]
print((solve(h, w, A, B)))

+def solve(h, w, A, B): +    center = 80 * (h + w - 1) +    C = [[abs(A[r][c] - B[r][c]) for c in range(w)] for r in range(h)] +    dp = [[0] * w for r in range(h)] +    dp[0][0] = (1 << (center + C[0][0])) | (1 << (center - C[0][0])) +    for r in range(h): +        for c in range(w): +            if r > 0: +                dp[r][c] |= (dp[r - 1][c] << C[r][c]) | (dp[r - 1][c] >> C[r][c]) +            if c > 0: +                dp[r][c] |= (dp[r][c - 1] << C[r][c]) | (dp[r][c - 1] >> C[r][c]) +    mask = bin(dp[-1][-1] >> center) +    return mask[::-1].find("1") + + -A = [list(map(int, input().split())) for _ in range(h)] -B = [list(map(int, input().split())) for _ in range(h)] -grid = [[0] * w for _ in range(h)] -for i in range(h): -    for j in range(w): -        m = abs(A[i][j] - B[i][j]) -        grid[i][j] = m -bias_inf = 2000 -dp = [[[0 for i in range(-bias_inf, bias_inf)] for j in range(w)] for k in range(h)] -dp[0][0][0 - grid[0][0]] = 1 -dp[0][0][0 + grid[0][0]] = 1 -for i in range(h): -    for j in range(w): -        for k in range(-bias_inf, bias_inf): -            if i == h - 1 and j == w - 1: -                break -            if dp[i][j][k]: -                if i != h - 1 and j != w - 1: -                    dp[i][j + 1][k + grid[i][j + 1]] = 1 -                    dp[i + 1][j][k + grid[i + 1][j]] = 1 -                    dp[i][j + 1][k - grid[i][j + 1]] = 1 -                    dp[i + 1][j][k - grid[i + 1][j]] = 1 -                elif j == w - 1: -                    dp[i + 1][j][k - grid[i + 1][j]] = 1 -                    dp[i + 1][j][k + grid[i + 1][j]] = 1 -                else: -                    dp[i][j + 1][k - grid[i][j + 1]] = 1 -                    dp[i][j + 1][k + grid[i][j + 1]] = 1 -ans = bias_inf -for i in range(-bias_inf, bias_inf): -    if dp[-1][-1][i]: -        ans = min(ans, abs(i)) -print(ans) +A = [list(map(int, input().split())) for i in range(h)] +B = [list(map(int, input().split())) for i in range(h)] +print((solve(h, w, A, B)))