CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.readline
h,w = list(map(int,input().split()))
la = [list(map(int,input().split())) for i in range(h)]
lb = [list(map(int,input().split())) for i in range(h)]
k = (h+w)*80+2

dp = [[[-1 for l in range(k)] for i in range(w)] for j in range(h)]

dp[0][0][abs(la[0][0]-lb[0][0])] = 1


for i in range(h):
    for j in range(w):
        for l in range(k):
            if dp[i][j][l] == 1:
                if j + 1 < w:
                    a = abs(la[i][j+1]-lb[i][j+1])
                
                    dp[i][j+1][l+a] = 1
                    dp[i][j+1][abs(l-a)] = 1
                if i + 1 < h:
                    a = abs(la[i+1][j]-lb[i+1][j])
                
                    dp[i+1][j][l+a] = 1
                    dp[i+1][j][abs(l-a)] = 1
print((dp[-1][-1].index(1)))

# Optimized solution
import sys
input = sys.stdin.readline
h,w = list(map(int,input().split()))
la = [list(map(int,input().split())) for i in range(h)]
for i in range(h):
    l = list(map(int,input().split()))
    for j,num in enumerate(l):
        la[i][j] = abs(la[i][j]-num)
dp = [[0 for i in range(w)] for j in range(h)]
k = (h+w)*80+2
dp[0][0] = 1 << (k+la[0][0]) | 1 << (k-la[0][0])

for i in range(h):
    for j in range(w):
        if i:
            dp[i][j] |= (dp[i-1][j] << la[i][j]) | dp[i-1][j] >> la[i][j]
        if j:
            dp[i][j] |= (dp[i][j-1] << la[i][j]) | dp[i][j-1] >> la[i][j]
        
s = bin(dp[-1][-1])[2:]
l = len(s)-1
ans = float('inf')
for i in range(l,-1,-1):
  if s[i]=='1':
    m = abs(l-i-k)
    if m<ans:
      ans = m
print(ans)
# Original solution
import sys
input = sys.stdin.readline
h,w = list(map(int,input().split()))
la = [list(map(int,input().split())) for i in range(h)]
for i in range(h):
    l = list(map(int,input().split()))
    for j,num in enumerate(l):
        la[i][j] = abs(la[i][j]-num)
dp = [[0 for i in range(w)] for j in range(h)]
k = (h+w)*80+2
dp[0][0] = 1 << (k+la[0][0]) | 1 << (k-la[0][0])

for i in range(h):
    for j in range(w):
        if i:
            dp[i][j] |= (dp[i-1][j] << la[i][j]) | dp[i-1][j] >> la[i][j]
        if j:
            dp[i][j] |= (dp[i][j-1] << la[i][j]) | dp[i][j-1] >> la[i][j]
        
s = bin(dp[-1][-1])[2:]
l = len(s)-1
ans = float('inf')
for i in range(l,-1,-1):
  if s[i]=='1':
    m = abs(l-i-k)
    if m<ans:
      ans = m
print(ans)


# Optimized solution
import sys
input = sys.stdin.readline
h,w = list(map(int,input().split()))
la = [list(map(int,input().split())) for i in range(h)]
for i in range(h):
    l = list(map(int,input().split()))
    for j,num in enumerate(l):
        la[i][j] = abs(la[i][j]-num)
dp = [[0 for i in range(w)] for j in range(h)]
k = (h+w)*80+2
dp[0][0] = 1 << (k+la[0][0]) | 1 << (k-la[0][0])

for i in range(h):
    for j in range(w):
        if i:
            dp[i][j] |= (dp[i-1][j] << la[i][j]) | dp[i-1][j] >> la[i][j]
        if j:
            dp[i][j] |= (dp[i][j-1] << la[i][j]) | dp[i][j-1] >> la[i][j]

check = dp[-1][-1]       
for i in range(k):
    if (check >> k+i) & 1 or (check >> k-i) & 1:
        print(i)
        exit()
# Original solution
from collections import defaultdict

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

dp = defaultdict(set)
dp[(0, 0)].add(abs(A[0][0] - B[0][0]))
for i in range(H):
    for j in range(W):
        d = abs(A[i][j] - B[i][j])
        if i > 0:
            for k in dp[(i - 1, j)]:
                dp[(i, j)].add(abs(k + d))
                dp[(i, j)].add(abs(k - d))
        if j > 0:
            for k in dp[(i, j - 1)]:
                dp[(i, j)].add(abs(k + d))
                dp[(i, j)].add(abs(k - d))
print((min(dp[(H - 1, W - 1)])))


# Optimized solution
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)]

dp = [[0] * W for i in range(H)]
dp[0][0] = 1 << (abs(A[0][0] - B[0][0]) + 6400)
for i in range(H):
    for j in range(W):
        d = abs(A[i][j] - B[i][j])
        if i > 0:
            dp[i][j] |= dp[i - 1][j] << d | dp[i - 1][j] >> d
        if j > 0:
            dp[i][j] |= dp[i][j - 1] << d | dp[i][j - 1] >> d
ans = 0
res = dp[H - 1][W - 1] >> 6400
while res & 1 == 0:
    res >>= 1
    ans += 1
print(ans)
# Original solution
H, W = [int(x) for x in input().split()]

A = [[0]*W for i in range(H)]
for c in [1, -1]:
    for i in range(H):
        arr = [int(x) for x in input().split()]
        for j in range(W):
            A[i][j] += arr[j] * c

M = 160*80
dp = [[0]*W for i in range(H)]
dp[0][0] = 1 << M
for i in range(H):
    for j in range(W):
        if i > 0:
            dp[i][j] |= dp[i-1][j]
        if j > 0:
            dp[i][j] |= dp[i][j-1]
        a = abs(A[i][j])
        dp[i][j] = (dp[i][j] << a) | (dp[i][j] >> a)

ans = M
for i in range(2*M):
    if dp[H-1][W-1] & (1 << i) > 0:
        ans = min(ans, abs(M-i))
print(ans)


# Optimized solution
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# template


def resolve():
    ofs = 6400
    h, w = list(map(int, readline().split()))
    A = [list(map(int, readline().split())) for _ in range(h)]
    B = [list(map(int, readline().split())) for _ in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    from itertools import product
    for i, j in product(list(range(h)), list(range(w))):
        x = abs(A[i][j]-B[i][j])
        t = 0
        if(i-1 >= 0):
            t |= dp[i-1][j]
        if(j-1 >= 0):
            t |= dp[i][j-1]
        dp[i][j] |= ((t << x) | (t >> x))

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            print(i)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()
# Original solution
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 3200
    h, w = list(map(int, readline().split()))
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = 0
            if(i-1 >= 0):
                t |= dp[i-1][j]
            if(j-1 >= 0):
                t |= dp[i][j-1]
            dp[i][j] |= ((t << x) | (t >> x))

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            print(i)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()


# Optimized solution
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 1000
    h, w = list(map(int, readline().split()))
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = 0
            if(i-1 >= 0):
                t |= dp[i-1][j]
            if(j-1 >= 0):
                t |= dp[i][j-1]
            dp[i][j] |= ((t << x) | (t >> x))

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            print(i)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()
# Original solution
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 6400
    h, w = list(map(int, readline().split()))
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = (dp[j] | dp[j-1])
            dp[j] = ((t << x) | (t >> x))

    b = list([s == '1' for s in list(bin(dp[w-1])[-ofs-1:-ofs+100])])
    for i, s in enumerate(b):
        if(s):
            print(i)
            return


if __name__ == "__main__":
    solve()


# Optimized solution
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 1000
    h, w = list(map(int, readline().split()))
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = (dp[j] | dp[j-1])
            dp[j] = ((t << x) | (t >> x))

    b = list([s == '1' for s in list(bin(dp[w-1])[-ofs-1:-ofs+100])])
    for i, s in enumerate(b):
        if(s):
            print(i)
            return


if __name__ == "__main__":
    solve()
# Original solution
import sys
input = sys.stdin.readline


h, w = list(map(int, input().split()))
A = []
B = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    B.append(line)


def slide_down(y, x, possible):
    new = set()
    delta = abs(A[y+1][x] - B[y+1][x])
    limit = (h+w) * 80
    for num in possible:
        if num + delta <= limit - (y+x)*80:
            new.add(num+delta)
        if abs(num-delta) <= limit - (y+x)*80:
            new.add(abs(num-delta))
    return new


def slide_right(y, x, possible):
    new = set()
    delta = abs(A[y][x+1] - B[y][x+1])
    limit = (h+w) * 80
    for num in possible:
        if num + delta <= limit - (y+x)*80:
            new.add(num+delta)
        if abs(num-delta) <= limit - (y+x)*80:
            new.add(abs(num-delta))
    return new


DP = [[set() for j in range(w)] for i in range(h)]
DP[0][0] = {abs(A[0][0] - B[0][0])}
for row in range(h):
    for col in range(w):
        possible = DP[row][col]
        if col < w-1:
            new = slide_right(row, col, possible)
            DP[row][col+1] |= new
        if row < h-1:
            new = slide_down(row, col, possible)
            DP[row+1][col] |= new

ans = min(DP[h-1][w-1])
print(ans)

# Optimized solution
import sys
input = sys.stdin.readline


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

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

const = 6400
bitset = 1 << const
DP = [[0]*w for _ in range(h)]
DP[0][0] = bitset >> A[0][0]


def slide_down(y, x):
    bit = DP[y][x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y+1][x] |= new1 | new2


def slide_right(y, x):
    bit = DP[y][x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y][x+1] |= new1 | new2


for y in range(h):
    for x in range(w):
        if y < h-1:
            slide_down(y, x)
        if x < w-1:
            slide_right(y, x)

ans = DP[h-1][w-1]
for i in range(81):
    if (ans >> const+i) & 1 or (ans >> const-i) & 1:
        print(i)
        exit()
# Original solution
import sys
input = sys.stdin.readline


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

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

const = 6400
bitset = 1 << const
DP = [[0]*w for _ in range(h)]
DP[0][0] = bitset >> A[0][0]


def slide_down(y, x):
    bit = DP[y][x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y+1][x] |= new1 | new2


def slide_right(y, x):
    bit = DP[y][x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y][x+1] |= new1 | new2


for y in range(h):
    for x in range(w):
        if y < h-1:
            slide_down(y, x)
        if x < w-1:
            slide_right(y, x)

ans = DP[h-1][w-1]
for i in range(81):
    if (ans >> const+i) & 1 or (ans >> const-i) & 1:
        print(i)
        exit()


# Optimized solution
import sys
input = sys.stdin.readline


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

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

const = 6400
bitset = 1 << const
DP = [0]*w
DP[0] = bitset >> A[0][0]


def slide_down(y, x):
    bit = DP[x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[x] = new1 | new2


def slide_right(y, x):
    bit = DP[x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[x+1] |= new1 | new2


for y in range(h):
    for x in range(w):
        if x < w-1:
            slide_right(y, x)
        if y < h-1:
            slide_down(y, x)

ans = DP[w-1]
for i in range(81):
    if (ans >> const+i) & 1 or (ans >> const-i) & 1:
        print(i)
        exit()
# Original solution
import sys
input = sys.stdin.readline


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

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

const = 640000
bitset = 1 << const
DP = [0]*w
DP[0] = bitset >> A[0][0]


def slide_down(y, x):
    bit = DP[x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[x] = new1 | new2


def slide_right(y, x):
    bit = DP[x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[x+1] |= new1 | new2


for y in range(h):
    for x in range(w):
        if x < w-1:
            slide_right(y, x)
        if y < h-1:
            slide_down(y, x)

ans = DP[w-1]
for i in range(81):
    if (ans >> const+i) & 1 or (ans >> const-i) & 1:
        print(i)
        exit()


# Optimized solution
h, w = list(map(int, input().split()))
A = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

const = 6400
mask = 1 << const
DP = [0]*w
DP[0] = mask >> A[0][0]


def slide_down(y, x):
    bit = DP[x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[x] = new1 | new2


def slide_right(y, x):
    bit = DP[x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[x+1] |= new1 | new2


for y in range(h):
    for x in range(w):
        if x < w-1:
            slide_right(y, x)
        if y < h-1:
            slide_down(y, x)

pattern = DP[w-1]
for i in range(81):
    if (pattern >> const+i) & 1 or (pattern >> const-i) & 1:
        print(i)
        exit()
# Original solution
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]

LIMIT = 80 * 40 * 40
# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0], -grid[0][0]}
for h, w in itertools.product(list(range(H)), list(range(W))):
    g = grid[h][w]
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            if -LIMIT <= c + g <= LIMIT:
                dp[h][w].add(c + g)
            if -LIMIT <= c - g <= LIMIT:
                dp[h][w].add(c - g)
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            if -LIMIT <= c + g <= LIMIT:
                dp[h][w].add(c + g)
            if -LIMIT <= c - g <= LIMIT:
                dp[h][w].add(c - g)

ans = INF
for s in dp[-1][-1]:
    ans = min(ans, abs(s))

print(ans)


# Optimized solution
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]

LIMIT = 80 * 80
# 符号をどっちにするか選べる
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0] = {grid[0][0], -grid[0][0]}
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]:
            if abs(c + g) <= LIMIT:
                s.add(abs(c + g))
            if abs(c - g) <= LIMIT:
                s.add(abs(c - g))
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            if abs(c + g) <= LIMIT:
                s.add(abs(c + g))
            if abs(c - g) <= LIMIT:
                s.add(abs(c - g))

ans = INF
for s in dp[-1][-1]:
    ans = min(ans, abs(s))

print(ans)
# Original solution
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], -grid[0][0]}
for h, w in itertools.product(list(range(H)), list(range(W))):
    g = grid[h][w]
    s = dp[h][w]
    limit = (H - h + 1) * (W - w + 1) * 80
    if h - 1 >= 0:
        for c in dp[h - 1][w]:
            if abs(c + g) <= limit:
                s.add(abs(c + g))
            if abs(c - g) <= limit:
                s.add(abs(c - g))
    if w - 1 >= 0:
        for c in dp[h][w - 1]:
            if abs(c + g) <= limit:
                s.add(abs(c + g))
            if abs(c - g) <= limit:
                s.add(abs(c - g))

ans = INF
for s in dp[-1][-1]:
    ans = min(ans, abs(s))

print(ans)


# Optimized solution
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)
# Original solution
H,W = list(map(int,input().split()))
A = [list(map(int,input().split())) for _ in [0]*H]
B = [list(map(int,input().split())) for _ in [0]*H]


C = [[abs(b-a) for a,b in zip(a_row,b_row)] for a_row,b_row in zip(A,B)]

d = [[set() for _ in [0]*W] for _ in [0]*H]
d[0][0] = {C[0][0]}

for i in range(H):
    for j in range(W):
        for n in d[i][j]:
            if i+1<H:
                c = C[i+1][j]
                d[i+1][j].add(n+c)
                d[i+1][j].add(abs(n-c))
            if j+1<W:
                c = C[i][j+1]
                d[i][j+1].add(n+c)
                d[i][j+1].add(abs(n-c))
ans = min(abs(o) for o in d[-1][-1])
print(ans)

# Optimized solution
H,W = list(map(int,input().split()))
A = [list(map(int,input().split())) for _ in [0]*H]
B = [list(map(int,input().split())) for _ in [0]*H]


C = [[abs(b-a) for a,b in zip(a_row,b_row)] for a_row,b_row in zip(A,B)]

dm = [[0]*W for _ in [0]*H]
dm[-1][-1] = 80
for i in range(H-1,-1,-1):
    for j in range(W-1,-1,-1):
        c = C[i][j]
        if i-1>=0:
            dm[i-1][j] = max(dm[i-1][j],dm[i][j] + c)
        if j-1>=0:
            dm[i][j-1] = max(dm[i][j-1],dm[i][j] + c)

d = [[set() for _ in [0]*W] for _ in [0]*H]
d[0][0] = {C[0][0]}

for i in range(H):
    for j in range(W):
        for n in d[i][j]:
            if i+1<H:
                c = C[i+1][j]
                if n+c <= dm[i][j]:
                    d[i+1][j].add(n+c)
                if n-c <= dm[i][j]:
                    d[i+1][j].add(abs(n-c))
            if j+1<W:
                c = C[i][j+1]
                if n+c <= dm[i][j]:
                    d[i][j+1].add(n+c)
                if n-c <= dm[i][j]:
                    d[i][j+1].add(abs(n-c))
ans = min(abs(o) for o in d[-1][-1])
print(ans)
# Original solution
import sys
from collections import defaultdict
input = sys.stdin.readline

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)]
dp = [[defaultdict(lambda : False) for _ in range(W)] for _ in range(H)]
dp[0][0][0] = True

for h in range(H):
    for w in range(W):
        C = A[h][w] - B[h][w]
        for v, f in list(dp[h][w].items()):
            if f:
                if h < H - 1:
                    dp[h + 1][w][v + C] = True
                    dp[h + 1][w][v - C] = True
                if w < W - 1:
                    dp[h][w + 1][v + C] = True
                    dp[h][w + 1][v - C] = True

ans = 10**10
C = A[H - 1][W - 1] - B[H - 1][W - 1]
for v, f in list(dp[H - 1][W - 1].items()):
    if f:
        ans = min(ans, abs(v - C))
        ans = min(ans, abs(v + C))
print(ans)

# Optimized solution
H, W = list(map(int, input().split()))
M = (H + W) * 80

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

dp = [[0] * W for _ in range(H)]
dp[0][0] = (1 << (M - abs(A[0][0] - B[0][0])))

for h in range(H):
    for w, (a, b) in enumerate(zip(A[h], B[h])):
        d = abs(a - b)
        mask = 0
        if h > 0:
            mask |= (dp[h - 1][w] << d)
            mask |= (dp[h - 1][w] >> d)
        if w > 0:
            mask |= (dp[h][w - 1] << d)
            mask |= (dp[h][w - 1] >> d)
        dp[h][w] |= mask

state = dp[-1][-1]
nums = []
for digit, b in enumerate(bin(state)[2:][:: -1]):
    if b == '1':
        nums.append(digit - M)
ans = min(abs(n) for n in nums)
print(ans)