We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
The square (i, j) has two numbers A_{ij} and B_{ij} written on it.
First, for each square, Takahashi paints one of the written numbers red and the other blue.
Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.
Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).
Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.
Find the minimum unbalancedness possible.

-----Constraints-----
 - 2 \leq H \leq 80
 - 2 \leq W \leq 80
 - 0 \leq A_{ij} \leq 80
 - 0 \leq B_{ij} \leq 80
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
H W
A_{11} A_{12} \ldots A_{1W}
:
A_{H1} A_{H2} \ldots A_{HW}
B_{11} B_{12} \ldots B_{1W}
:
B_{H1} B_{H2} \ldots B_{HW}

-----Output-----
Print the minimum unbalancedness possible.

-----Sample Input-----
2 2
1 2
3 4
3 4
2 1

-----Sample Output-----
0

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

Difficulty: MEDIUM

Solutions:
import sys

def input():
	return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
(H, W) = map(int, input().split())
A = [[int(x) for x in input().split()] for _ in range(H)]
B = [[int(x) for x in input().split()] for _ in range(H)]
C = [[0] * W for _ in range(H)]
for i in range(H):
	for j in range(W):
		C[i][j] = abs(A[i][j] - B[i][j])
center = 80 * (H + W)
dp = [[0] * W for _ in range(H)]
dp[0][0] = 1 << center - C[0][0] | 1 << center + C[0][0]
for i in range(H):
	for j in range(W):
		if i:
			dp[i][j] |= dp[i - 1][j] << C[i][j]
			dp[i][j] |= dp[i - 1][j] >> C[i][j]
		if j:
			dp[i][j] |= dp[i][j - 1] << C[i][j]
			dp[i][j] |= dp[i][j - 1] >> C[i][j]
ans = center * 2
for i in range(center * 2):
	if dp[H - 1][W - 1] >> i & 1:
		ans = min(ans, abs(i - center))
print(ans)


def ints():
	return list(map(int, input().split()))
(H, W) = ints()
A = [ints() for i in range(H)]
B = [ints() for i in range(H)]
C = 20000
dp = [[0 for j in range(W)] for i in range(H)]
dp[0][0] |= 1 << C - A[0][0] + B[0][0]
dp[0][0] |= 1 << C + A[0][0] - B[0][0]
for i in range(H):
	for j in range(W):
		if i + 1 < H:
			dp[i + 1][j] |= dp[i][j] << abs(A[i + 1][j] - B[i + 1][j])
			dp[i + 1][j] |= dp[i][j] >> abs(A[i + 1][j] - B[i + 1][j])
		if j + 1 < W:
			dp[i][j + 1] |= dp[i][j] << abs(A[i][j + 1] - B[i][j + 1])
			dp[i][j + 1] |= dp[i][j] >> abs(A[i][j + 1] - B[i][j + 1])
ans = 10 ** 100
for k in range(2 * C):
	if dp[H - 1][W - 1] >> k & 1:
		ans = min(ans, abs(k - C))
print(ans)


def main():
	import sys
	input = sys.stdin.readline
	sys.setrecursionlimit(10 ** 7)
	from collections import Counter, deque
	from itertools import combinations, permutations, accumulate, groupby, product
	from bisect import bisect_left, bisect_right
	from heapq import heapify, heappop, heappush
	import math
	(h, w) = map(int, input().split())
	a = [list(map(int, input().split())) for _ in range(h)]
	b = [list(map(int, input().split())) for _ in range(h)]
	c = [[0] * w for _ in range(h)]
	for i in range(h):
		for j in range(w):
			c[i][j] = abs(a[i][j] - b[i][j])
	mid = 6400
	dp = [[0] * w for _ in range(h)]
	dp[0][0] = 1 << mid + c[0][0] | 1 << mid - c[0][0]
	for x in range(h):
		for y in range(w):
			for (dx, dy) in [[0, 1], [1, 0]]:
				nx = x + dx
				ny = y + dy
				if 0 <= nx < h and 0 <= ny < w:
					dp[nx][ny] = dp[nx][ny] | dp[x][y] << c[nx][ny]
					dp[nx][ny] = dp[nx][ny] | dp[x][y] >> c[nx][ny]
	for i in range(mid + 1):
		if dp[-1][-1] & 1:
			res = mid - i
		dp[-1][-1] >>= 1
	print(res)

def __starting_point():
	main()
__starting_point()


(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)]
Z = (H + W) * 80 + 10
dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[0][0] = 1 << Z
for h in range(H):
	for w in range(W):
		d = dp[h][w]
		k = abs(A[h][w] - B[h][w])
		dp[h + 1][w] |= d << k | d >> k
		dp[h][w + 1] |= d << k | d >> k
ans = 10 ** 18
for a in (dp[H][W - 1], dp[H - 1][W]):
	for d in range(Z * 2 + 10):
		if a & 1 << d != 0:
			ans = min(ans, abs(d - Z))
print(ans)


import sys
input = sys.stdin.readline

def main():
	(h, w) = map(int, input().split())
	red = [[int(x) for x in input().split()] for _ in range(h)]
	blue = [[int(x) for x in input().split()] for _ in range(h)]
	max_dp = 13000
	dp_first = 2 ** max_dp
	dp = [[0] * (w + 1) for _ in range(h + 1)]
	for i in range(h):
		for j in range(w):
			sub = dp[i][j + 1] | dp[i + 1][j]
			sub = sub | dp_first if i == j == 0 else sub
			shift = abs(red[i][j] - blue[i][j])
			dp[i + 1][j + 1] = sub << shift | sub >> shift
	judge = dp[h][w]
	for i in range(max_dp):
		if judge >> max_dp + i & 1 or judge >> max_dp - i & 1:
			print(i)
			return

def __starting_point():
	main()
__starting_point()


(n, m) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
dp = [[0 for _ in range(m)] for _ in range(n)]
dp[0][0] = 1 << a[0][0] - b[0][0] + 12880 | 1 << b[0][0] - a[0][0] + 12880
mxs = (1 << 12880 * 2 + 1) - 1
for i in range(n):
	for j in range(m):
		if not i and (not j):
			continue
		d = abs(a[i][j] - b[i][j])
		if i:
			u = dp[i - 1][j] << d & mxs
			v = dp[i - 1][j] >> d
			dp[i][j] |= u
			dp[i][j] |= v
		if j:
			u = dp[i][j - 1] << d & mxs
			v = dp[i][j - 1] >> d & mxs
			dp[i][j] |= u
			dp[i][j] |= v
c = dp[n - 1][m - 1] >> 12880
for i in range(12880):
	if c & 1:
		print(i)
		break
	c >>= 1


(h, w) = map(int, input().split())
ab = []
for i in range(h * 2):
	ab.extend(list(map(int, input().split())))
abs_table = [[0] * w for _ in range(h)]
dp = [[0] * w for _ in range(h)]
s = h * w
i = 0
j = 0
for k in range(s):
	if w == j:
		j = 0
		i += 1
	(a, b) = (ab[k], ab[k + s])
	abs_table[i][j] = abs(a - b)
	j += 1
n = abs_table[0][0]
dp[0][0] = 2 ** (n * 2) | 1
for i in range(h):
	for j in range(w):
		a = abs_table[i][j]
		u = 0
		l = 0
		if i:
			u = dp[i - 1][j]
			u = u << a * 2 | u | u
		if j:
			l = dp[i][j - 1]
			l = l << a * 2 | l | l
		if not u and (not l):
			pass
		elif not l:
			dp[i][j] = u
		elif not u:
			dp[i][j] = l
		else:
			len_u = len(bin(u)[2:])
			len_l = len(bin(l)[2:])
			delta = len_u - len_l
			if delta == 0:
				dp[i][j] = u | l
			elif delta < 0:
				dp[i][j] = u << abs(delta) // 2 | l
			else:
				dp[i][j] = u | l << delta // 2
flag = bin(dp[-1][-1])[2:]
zero = len(flag) // 2
for (i, v) in enumerate(flag[zero:]):
	if v == '1':
		print(i)
		break


import sys
input = sys.stdin.readline

def main():
	(h, w) = map(int, input().split())
	A = [list(map(int, input().split())) for _ in range(h)]
	B = [list(map(int, input().split())) for _ in range(h)]
	M = 80 * (h + w)
	dp = [[0] * w for i in range(h)]
	d = abs(A[0][0] - B[0][0])
	mask = (1 << 2 * M) - 1
	dp[0][0] |= 1 << M - d
	dp[0][0] |= 1 << M + d
	for i in range(h):
		for j in range(w):
			d = abs(A[i][j] - B[i][j])
			if i:
				dp[i][j] |= dp[i - 1][j] << d & mask
				dp[i][j] |= dp[i - 1][j] >> d
			if j:
				dp[i][j] |= dp[i][j - 1] << d & mask
				dp[i][j] |= dp[i][j - 1] >> d
	t = dp[-1][-1] >> M
	ans = (t & -t).bit_length() - 1
	print(ans)

def __starting_point():
	main()
__starting_point()


import sys
input = sys.stdin.readline
(H, W) = 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)]
SA = [[0] * W for i in range(H)]
for i in range(H):
	for j in range(W):
		SA[i][j] = abs(A[i][j] - B[i][j])
DP = [[0 for i in range(W)] for j in range(H)]
DP[0][0] = 1 << (H + W) * 80 + SA[0][0]
for i in range(H):
	for j in range(W):
		tempbit = DP[i][j]
		if i + 1 < H:
			DP[i + 1][j] |= tempbit << SA[i + 1][j]
			DP[i + 1][j] |= tempbit >> SA[i + 1][j]
		if j + 1 < W:
			DP[i][j + 1] |= tempbit << SA[i][j + 1]
			DP[i][j + 1] |= tempbit >> SA[i][j + 1]
ANS = 1 << 30
for i in range((H + W) * 160 + 1):
	if 1 << i & DP[-1][-1] != 0:
		ANS = min(ANS, abs(i - (H + W) * 80))
print(ANS)


import sys
sys.setrecursionlimit(2147483647)
INF = float('inf')
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().rstrip()

def resolve():
	(h, w) = map(int, input().split())
	A = [list(map(int, input().split())) for _ in range(h)]
	B = [list(map(int, input().split())) for _ in range(h)]
	d = abs(A[0][0] - B[0][0])
	ofs = 12800
	dp = [[0] * w for _ in range(h)]
	dp[0][0] = 1 << ofs + d | 1 << ofs - d
	from itertools import product
	for (i, j) in product(range(h), range(w)):
		if i == 0 and j == 0:
			continue
		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
	for i in range(ofs):
		if dp[-1][-1] >> i + ofs & 1:
			print(i)
			return
resolve()


(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)]
M = 6401

def honya(y, d):
	t = (1 << d + 1) - 1
	t = y & t
	k = 0
	for i in range(d):
		k += t & 1
		t >>= 1
		k <<= 1
	return k
D = [[0] * W for _ in range(H)]
for i in range(H):
	for j in range(W):
		D[i][j] = abs(B[i][j] - A[i][j])
DP = [[0] * W for _ in range(H)]

def nasu(x, y, d):
	t = (1 << d + 1) - 1 & y
	return (x | y << d | y >> d | honya(y, d)) & mask
mask = (1 << M) - 1
DP[0][0] = 1 << D[0][0]
for i in range(H):
	for j in range(W):
		if i + 1 < H:
			DP[i + 1][j] = nasu(DP[i + 1][j], DP[i][j], D[i + 1][j])
		if j + 1 < W:
			DP[i][j + 1] = nasu(DP[i][j + 1], DP[i][j], D[i][j + 1])
t = DP[-1][-1]
for i in range(M):
	if t & 1:
		print(i)
		break
	t >>= 1


tdlist = lambda i, j, value=0: [[value] * j for _ in range(i)]
(h, w) = list(map(int, input().split()))
D = 160 * 80
D2 = D * 2 + 1
diff = [list(map(int, input().split())) for _ in range(h)]
for i in range(h):
	for (j, b) in enumerate(list(map(int, input().split()))):
		diff[i][j] = abs(diff[i][j] - b)
dp = tdlist(h, w)
dp[0][0] |= 1 << D + diff[0][0]
dp[0][0] |= 1 << D - diff[0][0]
for i in range(h):
	for j in range(w):
		if j:
			dp[i][j] |= dp[i][j - 1] << diff[i][j]
			dp[i][j] |= dp[i][j - 1] >> diff[i][j]
		if i:
			dp[i][j] |= dp[i - 1][j] << diff[i][j]
			dp[i][j] |= dp[i - 1][j] >> diff[i][j]
ans = float('inf')
for i in range(D2):
	if dp[h - 1][w - 1] >> i & 1:
		ans = min(ans, abs(i - D))
print(ans)


import sys
input = sys.stdin.readline
(H, W) = map(int, input().split())
(A, B) = ([None] * H, [None] * H)
for i in range(H):
	A[i] = list(map(int, input().split()))
for i in range(H):
	B[i] = list(map(int, input().split()))
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
M = 80 * (80 + 80)
dp = [[0 for j in range(W + 1)] for i in range(H + 1)]
dp[0][1] = 1
dp[1][0] = 1
for i in range(1, H + 1):
	for j in range(1, W + 1):
		d = C[i - 1][j - 1]
		if d != 0:
			x = int(bin((dp[i - 1][j] & 2 ** d - 1) + 2 ** d)[::-1][:d], 2) << 1
			y = int(bin((dp[i][j - 1] & 2 ** d - 1) + 2 ** d)[::-1][:d], 2) << 1
		else:
			x = y = 0
		dp[i][j] = dp[i - 1][j] >> d | dp[i - 1][j] << d | dp[i][j - 1] >> d | dp[i][j - 1] << d | x | y
for (i, v) in enumerate(bin(dp[H][W])[::-1]):
	if v == '1':
		print(i)
		break


(h, w) = 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 + 1) for i in range(h + 1)]
dp[0][0] = 1 << (h + w) * 80
for i in range(h):
	for j in range(w):
		diff = abs(a[i][j] - b[i][j])
		dp[i + 1][j] |= dp[i][j] << diff | dp[i][j] >> diff
		dp[i][j + 1] |= dp[i][j] << diff | dp[i][j] >> diff
ans = float('inf')
for i in range((h + w) * 160):
	if dp[h][w - 1] & 1 << i:
		ans = min(ans, abs(i - (h + w) * 80))
print(ans)


def p_e():
	(h, w) = map(int, input().split())
	t1 = [list(map(int, input().split())) for _ in range(h)]
	t2 = [list(map(int, input().split())) for _ in range(h)]
	m = 160 * 80
	dp = [[0] * w for _ in range(h)]
	dp[0][0] = 1 << m
	for i in range(h):
		for j in range(w):
			v = abs(t1[i][j] - t2[i][j])
			if i > 0:
				dp[i][j] |= dp[i - 1][j]
			if j > 0:
				dp[i][j] |= dp[i][j - 1]
			dp[i][j] = dp[i][j] << v | dp[i][j] >> v
	ans = m
	for i in range(m * 2):
		if dp[h - 1][w - 1] & 1 << i > 0:
			ans = min(ans, abs(m - i))
	print(ans)
p_e()


import sys
input = sys.stdin.buffer.readline
(h, w) = map(int, input().split())
a = [map(int, input().split()) for _ in range(h)]
b = [map(int, input().split()) for _ in range(h)]
g = [list((abs(x - y) for (x, y) in zip(l1, l2))) for (l1, l2) in zip(a, b)]
poss = [[0] * w for _ in range(h)]
poss[0][0] |= 1 << 6400 + g[0][0]
poss[0][0] |= 1 << 6400 - g[0][0]
for i in range(h):
	for j in range(w - 1):
		poss[i][j + 1] |= poss[i][j] << g[i][j + 1]
		poss[i][j + 1] |= poss[i][j] >> g[i][j + 1]
	if i != h - 1:
		for j in range(w):
			poss[i + 1][j] |= poss[i][j] << g[i + 1][j]
			poss[i + 1][j] |= poss[i][j] >> g[i + 1][j]
res = poss[-1][-1]
for i in range(6400):
	if res & 1 << 6400 + i or res & 1 << 6400 - i:
		print(i)
		exit()


def fold(x, m):
	y = 0
	for _ in range(m):
		y |= x & 1
		y <<= 1
		x >>= 1
	return x | y
lmi = lambda : list(map(int, input().split()))
(h, w) = lmi()
a = [lmi() for _ in range(2 * h)]
g = [[abs(a[i][j] - a[i + h][j]) for j in range(w)] for i in range(h)]
mask = (1 << 12801) - 1
dp = [[0] * (w + 1) for _ in range(h + 1)]
dp[0][0] = 1
for i in range(h):
	for j in range(w):
		(cur, par) = (g[i][j], dp[i][j])
		pos = par << cur | fold(par, cur)
		pos &= mask
		dp[i + 1][j] |= pos
		dp[i][j + 1] |= pos
for i in range(12801):
	if dp[-1][-2] & 1 << i:
		print(i)
		break


maxV = 80 * (80 + 80)
(H, W) = list(map(int, input().split()))
Ass = [tuple(map(int, input().split())) for _ in range(H)]
Bss = [tuple(map(int, input().split())) for _ in range(H)]
dp = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
	for j in range(W):
		(A, B) = (Ass[i][j], Bss[i][j])
		if A > B:
			(A, B) = (B, A)
		if i == 0 and j == 0:
			dp[i][j] = 1 << maxV + B - A | 1 << maxV - B + A
		if i > 0:
			dp[i][j] |= dp[i - 1][j] << B - A | dp[i - 1][j] >> B - A
		if j > 0:
			dp[i][j] |= dp[i][j - 1] << B - A | dp[i][j - 1] >> B - A
bitset = dp[-1][-1]
mask = 1 << 2 * maxV
ans = maxV
for i in range(-maxV, maxV + 1):
	if bitset & mask:
		ans = min(ans, abs(i))
	mask >>= 1
print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
import time, random
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def main():
	(h, w) = LI()
	a = [LI() for _ in range(h)]
	b = [LI() for _ in range(h)]
	m = 1 << 80 * 80 * 2 + 3
	r = [[0] * w for _ in range(h)]
	c = abs(a[0][0] - b[0][0])
	r[0][0] |= 1 << 80 * 80 + 1 << c
	r[0][0] |= 1 << 80 * 80 + 1 >> c
	for i in range(h):
		for j in range(w):
			if i == 0 and j == 0:
				continue
			c = abs(a[i][j] - b[i][j])
			t = 0
			if i > 0:
				t |= (r[i - 1][j] << c) % m
				t |= r[i - 1][j] >> c
			if j > 0:
				t |= (r[i][j - 1] << c) % m
				t |= r[i][j - 1] >> c
			r[i][j] = t
	c = r[-1][-1]
	t = 10000
	for i in range(80 * 80 * 2 + 3):
		if 1 << i & c > 0:
			k = abs(i - (80 * 80 + 1))
			if t > k:
				t = k
	return t
print(main())


(H, W) = 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)]
C = [[] for i in range(H)]
for (i, (arow, brow)) in enumerate(zip(A, B)):
	for (a, b) in zip(arow, brow):
		d = abs(a - b)
		C[i].append(d)
ofs = (H + W) * 100
dp = [0] * W
dp[0] = 1 << ofs
for (i, crow) in enumerate(C):
	dp2 = [0] * W
	for (j, (c, d)) in enumerate(zip(C[i], dp)):
		dp2[j] |= d << c
		dp2[j] |= d >> c
	for (j, c) in enumerate(C[i]):
		if j == 0:
			continue
		dp2[j] |= dp2[j - 1] << c
		dp2[j] |= dp2[j - 1] >> c
	dp = dp2
ans = ofs
l = len(bin(dp[-1]))
for i in range(l):
	if dp[-1] & 1 << i:
		ans = min(ans, abs(i - ofs))
print(ans)


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def resolve():
	ofs = 6400
	(h, w) = 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(range(h), 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
INF = float('inf')
MOD = 10 ** 9 + 7
resolve()


import sys
input = sys.stdin.readline

def solve():
	maxV = 12800
	(H, W) = map(int, input().split())
	Ass = [tuple(map(int, input().split())) for _ in range(H)]
	Bss = [tuple(map(int, input().split())) for _ in range(H)]
	dp = [[0] * W for _ in range(H)]
	diff = abs(Ass[0][0] - Bss[0][0])
	dp0 = 1 << maxV
	dp[0][0] = dp0 << diff | dp0 >> diff
	for i in range(H):
		for j in range(W):
			diff = abs(Ass[i][j] - Bss[i][j])
			if i > 0:
				dp0 = dp[i - 1][j]
				dp[i][j] |= dp0 << diff | dp0 >> diff
			if j > 0:
				dp0 = dp[i][j - 1]
				dp[i][j] |= dp0 << diff | dp0 >> diff
	ans = maxV
	dpHW = dp[-1][-1]
	for i in range(2 * maxV + 1):
		if dpHW >> i & 1:
			j = abs(i - maxV)
			if j < ans:
				ans = j
	print(ans)
solve()


import sys

def main():
	input = sys.stdin.readline
	(H, W) = map(int, input().split())
	A = [list(map(int, input().split())) for _ in range(H)]
	B = [list(map(int, input().split())) for _ in range(H)]
	mx = 80 * (H + W)
	C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
	dp = [[0 for _ in range(W)] for __ in range(H)]
	dp[0][0] = 1 << mx + C[0][0]
	for i in range(H):
		for j in range(W):
			for (ni, nj) in ((i + 1, j), (i, j + 1)):
				if ni >= H or nj >= W:
					continue
				dp[ni][nj] |= dp[i][j] << C[ni][nj]
				dp[ni][nj] |= dp[i][j] >> C[ni][nj]
	for ans in range(mx + 1):
		if dp[H - 1][W - 1] & (1 << mx + ans | 1 << mx - ans):
			print(ans)
			break
main()


import sys
readline = sys.stdin.readline
(H, W) = 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 = (H + W) * 80
d = abs(A[0][0] - B[0][0])
dp[0][0] = 1 << X - d
for h in range(H):
	for (w, (a, b)) in enumerate(zip(A[h], B[h])):
		if h == w == 0:
			continue
		d = abs(a - b)
		x = 0
		if h != 0:
			x |= dp[h - 1][w] << d
			x |= dp[h - 1][w] >> d
		if w != 0:
			x |= dp[h][w - 1] << d
			x |= dp[h][w - 1] >> d
		dp[h][w] = x
bit = bin(dp[-1][-1])[2:]
can = [i - X for (i, b) in enumerate(bit[::-1]) if b == '1']
answer = min((x if x >= 0 else -x for x in can))
print(answer)


M = 7000
(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]
dp = [[0] * (w + 1) for _ in range(h + 1)]
dp[0][1] = dp[1][0] = 1 << M
for i in range(h):
	for j in range(w):
		dp[i + 1][j + 1] |= dp[i][j + 1] << abs(a[i][j] - b[i][j])
		dp[i + 1][j + 1] |= dp[i][j + 1] >> abs(a[i][j] - b[i][j])
		dp[i + 1][j + 1] |= dp[i + 1][j] << abs(a[i][j] - b[i][j])
		dp[i + 1][j + 1] |= dp[i + 1][j] >> abs(a[i][j] - b[i][j])
ans = M
for b in range(2 * M + 1):
	if dp[h][w] >> b & 1:
		ans = min(ans, abs(b - M))
print(ans)


(h, w) = 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)]
ab = [[0] * w for i in range(h)]
for ch in range(h):
	for cw in range(w):
		ab[ch][cw] = abs(a[ch][cw] - b[ch][cw])
ab0 = ab[0][0]
size = 80 * (h + w)
dp = [[0] * w for hh in range(h)]
dp[0][0] |= 2 ** (size + ab0)
dp[0][0] |= 2 ** (size - ab0)
for ch in range(h):
	for cw in range(w):
		if ch < h - 1:
			dp[ch + 1][cw] |= dp[ch][cw] << ab[ch + 1][cw]
			dp[ch + 1][cw] |= dp[ch][cw] >> ab[ch + 1][cw]
		if cw < w - 1:
			dp[ch][cw + 1] |= dp[ch][cw] << ab[ch][cw + 1]
			dp[ch][cw + 1] |= dp[ch][cw] >> ab[ch][cw + 1]
a = dp[h - 1][w - 1] >> size
m = a & -a
print(m.bit_length() - 1)


def fold(x, m):
	y = 0
	for _ in range(m):
		y |= x & 1
		y <<= 1
		x >>= 1
	return x | y
lmi = lambda : list(map(int, input().split()))
(h, w) = lmi()
a = [lmi() for _ in range(2 * h)]
g = [[abs(a[i][j] - a[i + h][j]) for j in range(w)] for i in range(h)]
m = 12801
mask = (1 << m) - 1
a = [0] * (w + 1)
a[0] = 1
for i in range(h):
	for j in range(w):
		(cur, par) = (g[i][j], a[j] | a[j - 1])
		a[j] = par << cur & mask | fold(par, cur)
v = a[-2]
print((v & -v).bit_length() - 1)


import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
(h, w) = 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)]
ab = [[0] * w for i in range(h)]
for ch in range(h):
	for cw in range(w):
		ab[ch][cw] = abs(a[ch][cw] - b[ch][cw])
ab0 = ab[0][0]
size = 80 * (h + w)
dp = [[0] * w for hh in range(h)]
dp[0][0] |= 2 ** (size + ab0)
dp[0][0] |= 2 ** (size - ab0)
for ch in range(h):
	for cw in range(w):
		if ch < h - 1:
			dp[ch + 1][cw] |= dp[ch][cw] << ab[ch + 1][cw]
			dp[ch + 1][cw] |= dp[ch][cw] >> ab[ch + 1][cw]
		if cw < w - 1:
			dp[ch][cw + 1] |= dp[ch][cw] << ab[ch][cw + 1]
			dp[ch][cw + 1] |= dp[ch][cw] >> ab[ch][cw + 1]
a = dp[h - 1][w - 1] >> size
m = a & -a
print(m.bit_length() - 1)


import sys
input = sys.stdin.readline
(h, w) = map(int, input().split())
a = [tuple(map(int, input().split())) for _ in range(h)]
b = [tuple(map(int, input().split())) for _ in range(h)]
c = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]
CONST = 80 * 80 * 2 + 100000
dp = [[0] * w for _ in range(h)]
dp[0][0] = 1 << c[0][0] + CONST
for i in range(h):
	for j in range(w):
		if i < h - 1:
			dp[i + 1][j] |= dp[i][j] << c[i + 1][j]
			dp[i + 1][j] |= dp[i][j] >> c[i + 1][j]
		if j < w - 1:
			dp[i][j + 1] |= dp[i][j] << c[i][j + 1]
			dp[i][j + 1] |= dp[i][j] >> c[i][j + 1]
for k in range(CONST):
	if dp[h - 1][w - 1] & 1 << CONST + k != 0 or dp[h - 1][w - 1] & 1 << CONST - k != 0:
		ans = k
		break
print(ans)


import sys
input = sys.stdin.buffer.readline
(H, W) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
SIZE = 20000
dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1 << SIZE + C[0][0] | 1 << SIZE - C[0][0]
for i in range(H):
	for j in range(W):
		if i == j == 0:
			continue
		else:
			dp[i][j] |= dp[i - 1][j] << C[i][j] | dp[i - 1][j] >> C[i][j]
			dp[i][j] |= dp[i][j - 1] << C[i][j] | dp[i][j - 1] >> C[i][j]
for i in range(SIZE):
	if dp[-1][-1] >> i + SIZE & 1:
		print(i)
		exit()


M = 6400 * 4
mid = M // 2
INF = 10 ** 18
(H, W) = 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)])
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:
		ans = min(ans, abs(i - mid))
print(ans)


def run():
	ofs = 1000
	(H, W) = map(int, input().split())
	dp = [[0 for w in range(W)] for h in range(H)]
	a = [list(map(int, input().split())) for i in range(H)]
	b = [list(map(int, input().split())) for i in range(H)]
	c = [[abs(a[i][j] - b[i][j]) for j in range(W)] for i in range(H)]
	dp[0][0] = 1 << ofs + c[0][0] | 1 << ofs - c[0][0]
	for h in range(1, H):
		dp[h][0] = dp[h - 1][0] << c[h][0] | dp[h - 1][0] >> c[h][0]
	for w in range(1, W):
		dp[0][w] = dp[0][w - 1] << c[0][w] | dp[0][w - 1] >> c[0][w]
	for h in range(1, H):
		for w in range(1, W):
			v = c[h][w]
			dp[h][w] = dp[h - 1][w] << v | dp[h - 1][w] >> v
			dp[h][w] |= dp[h][w - 1] << v | dp[h][w - 1] >> v
	s = list(bin(dp[-1][-1] >> ofs))
	for i in range(len(s)):
		if s[-1 - i] == '1':
			print(i)
			break
run()


(H, W) = 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)]
C = []
for (arow, brow) in zip(A, B):
	C.append([abs(a - b) for (a, b) in zip(arow, brow)])
ofs = 80 * (H + W)
dp = [0] * W
dp[0] = 1 << ofs + C[0][0] | 1 << ofs - C[0][0]
for j in range(W - 1):
	dp[j + 1] = dp[j] << C[0][j + 1] | dp[j] >> C[0][j + 1]
for i in range(1, H):
	dp2 = [0] * W
	for j in range(W):
		dp2[j] = dp[j] << C[i][j] | dp[j] >> C[i][j]
		if j:
			dp2[j] |= dp2[j - 1] << C[i][j] | dp2[j - 1] >> C[i][j]
	dp = dp2
ans = ofs * 2
for i in range(ofs * 2):
	if dp[-1] & 1 << i:
		ans = min(ans, abs(ofs - i))
print(ans)


I = [int(_) for _ in open(0).read().split()]
(H, W) = I[:2]
A = I[2:2 + H * W]
B = I[2 + H * W:]
D = [abs(a - b) for (a, b) in zip(A, B)]
dp = [0] + [1] + [0] * (W - 1)
for h in range(H):
	dpo = dp
	dp = [0] * (W + 1)
	for (w, d) in enumerate(D[W * h:W * (h + 1)]):
		s = dp[w] | dpo[w + 1]
		dp[w + 1] = s << d | s >> d
		if d:
			t = bin(s)[2:][::-1][:d + 1]
			t += '0' * (d + 1 - len(t))
			dp[w + 1] |= int(t, 2)
for i in range(160 * 80):
	if dp[-1] & 2 ** i:
		print(i)
		exit()


(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)]
c = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]
dp = [[0] * (w + 1) for _ in range(h + 1)]
dp[0][1] = 1 << 12800
for i in range(1, h + 1):
	for j in range(1, w + 1):
		t = c[i - 1][j - 1]
		dp[i][j] = dp[i][j - 1] << t | dp[i][j - 1] >> t | dp[i - 1][j] << t | dp[i - 1][j] >> t
ans = 10 ** 20
for i in range(25601):
	if dp[h][w] & 1 << i:
		ans = min(ans, abs(12800 - i))
print(ans)


import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
	(H, W) = map(int, input().split())
	A = [list(map(int, input().split())) for _ in range(H)]
	B = [list(map(int, input().split())) for _ in range(H)]
	dp = [[0] * (1 + W) for _ in range(H + 1)]
	x = abs(A[0][0] - B[0][0])
	dp[0][0] = 1 << 12800 + x
	dp[0][0] |= 1 << 12800 - x
	for i in range(H):
		for j in range(W):
			c = abs(A[i][j] - B[i][j])
			if i > 0:
				dp[i][j] |= dp[i - 1][j] << c
				dp[i][j] |= dp[i - 1][j] >> c
			if j > 0:
				dp[i][j] |= dp[i][j - 1] << c
				dp[i][j] |= dp[i][j - 1] >> c
	ans = INF
	x = dp[H - 1][W - 1]
	for i in range(25600):
		if x & 1:
			ans = min(ans, abs(i - 12800))
		x >>= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


(h, w) = map(int, input().split())
diffs = []
for y in range(h):
	row = map(int, input().split())
	diffs.append(list(row))
for y in range(h):
	row = map(int, input().split())
	for (x, b) in zip(range(w), row):
		diffs[y][x] = abs(diffs[y][x] - b)
m = 80 * (h + w)
dp = [[0 for j in range(w)] for k in range(h)]
d = m - diffs[0][0]
dp[0][0] = 1 << d
for y in range(h):
	for x in range(w):
		if x == y == 0:
			continue
		diff = diffs[y][x]
		if y != 0:
			dp[y][x] |= dp[y - 1][x] << diff
			dp[y][x] |= dp[y - 1][x] >> diff
		if x != 0:
			dp[y][x] |= dp[y][x - 1] << diff
			dp[y][x] |= dp[y][x - 1] >> diff
end = bin(dp[-1][-1])[2:][::-1]
candidates = [abs(i - m) for (i, b) in enumerate(end) if b == '1']
ans = min(candidates)
print(ans)


import sys

def main():
	inp = lambda : list(map(int, input().split()))
	(h, w) = inp()
	a = [inp() for _ in range(h)]
	b = [inp() for _ in range(h)]
	d = [[0] * w for _ in range(h)]
	for i in range(h):
		for j in range(w):
			d[i][j] = abs(a[i][j] - b[i][j])
	zero = 80 * (h + w) + 2
	dp = [[0] * w for _ in range(h)]
	dp[0][0] = 1 << zero + d[0][0] | 1 << zero - d[0][0]
	for i in range(h):
		for j in range(w):
			if i == 0:
				if j == 0:
					continue
				else:
					dp[i][j] = dp[i][j - 1] << d[i][j] | dp[i][j - 1] >> d[i][j]
			elif j == 0:
				dp[i][j] = dp[i - 1][j] << d[i][j] | dp[i - 1][j] >> d[i][j]
			else:
				dp[i][j] = dp[i - 1][j] << d[i][j] | dp[i - 1][j] >> d[i][j]
				dp[i][j] = dp[i][j] | (dp[i][j - 1] << d[i][j] | dp[i][j - 1] >> d[i][j])
	ans = dp[-1][-1]
	for i in range(zero):
		if ans >> zero + i & 1 or ans >> zero - i & 1:
			print(i)
			return

def __starting_point():
	main()
__starting_point()


(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]
x = (h + w) * 80
dp = [[0] * w for _ in range(h)]
d = abs(a[0][0] - b[0][0])
dp[0][0] = 1 << x - d
for i in range(h):
	for (j, (ae, be)) in enumerate(zip(a[i], b[i])):
		if i == j == 0:
			continue
		d = abs(ae - be)
		bt = 0
		if i != 0:
			bt |= dp[i - 1][j] << d
			bt |= dp[i - 1][j] >> d
		if j != 0:
			bt |= dp[i][j - 1] << d
			bt |= dp[i][j - 1] >> d
		dp[i][j] = bt
dp = dp[-1][-1]
b = bin(dp)[2:]
can_make = [i - x for (i, be) in enumerate(b[::-1]) if be == '1']
ans = min((x if x >= 0 else -x for x in can_make))
print(ans)


(H, W) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
Diff = [[0] * (W + 1) for _ in range(H + 1)]
M = 0
for i in range(H):
	for j in range(W):
		m = abs(A[i][j] - B[i][j])
		Diff[i][j] = m
		M += m
dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[0][0] |= 1 << Diff[i][j]
for i in range(0, H):
	for j in range(0, W):
		here = dp[i][j]
		s = Diff[i][j]
		ni = nj = here << s | here >> s
		ex = 0
		for k in range(1, s + 1):
			if here >> k & 1:
				ex |= 1 << s - k
		dp[i + 1][j] |= ni | ex
		dp[i][j + 1] |= nj | ex
a = dp[H - 1][W - 1]
ans = 0
for k in range(M + 1):
	if a >> k & 1:
		ans = k
		break
print(k)


import numpy as np
(H, W) = map(int, input().split())
red_map = np.array([input().split() for i in range(H)], dtype='int')
blue_map = np.array([input().split() for i in range(H)], dtype='int')
sub_map = abs(red_map - blue_map)
dp = [[2020] * W for i in range(H)]
max_deviation = 2 ** (80 * (H + W))
for i in range(H):
	for j in range(W):
		if not i and (not j):
			dp[i][j] = max_deviation << sub_map[i][j] | max_deviation >> sub_map[i][j]
		if i > 0:
			dp[i][j] |= dp[i - 1][j] << sub_map[i][j] | dp[i - 1][j] >> sub_map[i][j]
		if j > 0:
			dp[i][j] |= dp[i][j - 1] << sub_map[i][j] | dp[i][j - 1] >> sub_map[i][j]
pre_ans = dp[H - 1][W - 1] >> 80 * (H + W)
pre_ans_bit = format(pre_ans & -pre_ans, 'b')
ans = len(pre_ans_bit) - 1
print(ans)


(H, W) = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
for i in range(H):
	B = list(map(int, input().split()))
	for j in range(W):
		A[i][j] = abs(A[i][j] - B[j])
D = 80 * (H + W) + 1
D2 = D * 2
dp = [[0] * W for i in range(H)]
dp[0][0] |= 1 << D + A[0][0]
dp[0][0] |= 1 << D - A[0][0]
for i in range(H):
	for j in range(W):
		if i:
			dp[i][j] |= dp[i - 1][j] << A[i][j]
			dp[i][j] |= dp[i - 1][j] >> A[i][j]
		if j:
			dp[i][j] |= dp[i][j - 1] << A[i][j]
			dp[i][j] |= dp[i][j - 1] >> A[i][j]
res = dp[-1][-1]
ans = 10 ** 18
for i in range(D2):
	if res >> i & 1:
		ans = min(ans, abs(D - i))
print(ans)


(H, W) = map(int, input().split())
c = [list(map(int, input().split())) for i in range(H)]
b = [list(map(int, input().split())) for i in range(H)]
a = [[0 for i in range(W)] for j in range(H)]
for i in range(H):
	for j in range(W):
		a[i][j] = abs(c[i][j] - b[i][j])
dp = [[0 for i in range(W)] for j in range(H)]
X = 3200
dp[0][0] = 1 << X + a[0][0] | 1 << X - a[0][0]
for i in range(H):
	for j in range(W):
		if 0 <= i < H and 0 <= j + 1 < W:
			dp[i][j + 1] |= dp[i][j] << a[i][j + 1]
			dp[i][j + 1] |= dp[i][j] >> a[i][j + 1]
		if 0 <= i + 1 < H and 0 <= j < W:
			dp[i + 1][j] |= dp[i][j] << a[i + 1][j]
			dp[i + 1][j] |= dp[i][j] >> a[i + 1][j]
A = str(bin(dp[H - 1][W - 1]))[::-1]
L = len(A)
ans = 3200
for i in range(L):
	if A[i] == '1':
		ans = min(ans, abs(i - X))
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def bin_out(aa):
	bb = [format(a, 'b') for a in aa]
	print(bb)

def main():
	(h, w) = MI()
	aa = LLI(h)
	bb = LLI(h)
	max_val = 6400
	zero = 1 << max_val
	dp = [0] * w
	dp[0] = zero
	for (i, (ar, br)) in enumerate(zip(aa, bb)):
		for (j, (a, b)) in enumerate(zip(ar, br)):
			d = abs(a - b)
			if j > 0:
				dp[j] |= dp[j - 1]
			dp[j] = dp[j] << d | dp[j] >> d
	goal = dp[-1]
	for ans in range(max_val + 1):
		if goal & zero << ans or goal & zero >> ans:
			print(ans)
			break
main()


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) = 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) = map(int, input().split())
A = [[int(i) for i in input().split()] for j in range(H)]
B = [[int(i) for i in input().split()] for j in range(H)]
dp = [[0 for j in range(W)] for i in range(H)]
M = 161 * 161
dp[0][0] |= 1 << A[0][0] - B[0][0] + M
dp[0][0] |= 1 << -A[0][0] + B[0][0] + M
for i in range(H):
	for j in range(W):
		dij = abs(A[i][j] - B[i][j])
		if i > 0:
			dp[i][j] |= dp[i - 1][j] << dij
			dp[i][j] |= dp[i - 1][j] >> dij
		if j > 0:
			dp[i][j] |= dp[i][j - 1] >> dij
			dp[i][j] |= dp[i][j - 1] << dij
ans = []
for k in range(2 * M + 1):
	if 1 << k & dp[-1][-1] != 0:
		ans.append(abs(k - M))
print(min(ans))


(H, W) = 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)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
M = (H + W) * 80
dp = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
	for j in range(W):
		if i == 0:
			if j == 0:
				dp[i][j] = 1 << M << C[i][j]
			else:
				dp[i][j] |= dp[i][j - 1] >> C[i][j]
				dp[i][j] |= dp[i][j - 1] << C[i][j]
		else:
			if j != 0:
				dp[i][j] |= dp[i][j - 1] >> C[i][j]
				dp[i][j] |= dp[i][j - 1] << C[i][j]
			dp[i][j] |= dp[i - 1][j] >> C[i][j]
			dp[i][j] |= dp[i - 1][j] << C[i][j]
dp = bin(dp[i][j])[2:]
dp = dp[::-1]
ans = M
for (i, x) in enumerate(dp):
	if x == '1':
		ans = min(ans, abs(i - M))
print(ans)


(H, W) = 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)]
C = [[abs(a - b) for (a, b) in zip(arow, brow)] for (arow, brow) in zip(A, B)]
ofs = 80 * (H + W)
dp = [[0] * W for _ in range(H)]
dp[0][0] = 1 << ofs + C[0][0] | 1 << ofs - C[0][0]
for (i, crow) in enumerate(C):
	for (j, c) in enumerate(crow):
		if i:
			dp[i][j] = dp[i - 1][j] << c | dp[i - 1][j] >> c
		if j:
			dp[i][j] |= dp[i][j - 1] << c | dp[i][j - 1] >> c
ans = ofs * 2
for i in range(ofs * 2):
	if dp[-1][-1] & 1 << i:
		ans = min(ans, abs(i - ofs))
print(ans)


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

def reverse_binary(n, width):
	return int('{:0{width}b}'.format(n, width=width)[::-1], 2)
dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1 << abs(A[0][0] - B[0][0])
for h in range(H):
	for w in range(W):
		diff = abs(A[h][w] - B[h][w])
		if w:
			dp[h][w] |= reverse_binary(dp[h][w - 1] & (1 << diff) - 1, diff) << 1
			dp[h][w] |= dp[h][w - 1] >> diff
			dp[h][w] |= dp[h][w - 1] << diff
		if h:
			dp[h][w] |= reverse_binary(dp[h - 1][w] & (1 << diff) - 1, diff) << 1
			dp[h][w] |= dp[h - 1][w] >> diff
			dp[h][w] |= dp[h - 1][w] << diff
tmp = dp[-1][-1]
ans = 0
while tmp & 1 == 0:
	tmp >>= 1
	ans += 1
print(ans)


(H, W) = map(int, input().split())
A = []
A.append([0 for i in range(W + 1)])
for i in range(H):
	a = [0] + list(map(int, input().split()))
	A.append(a)
B = []
B.append([0 for i in range(W + 1)])
for i in range(H):
	b = [0] + list(map(int, input().split()))
	B.append(b)
C = [[0 for i in range(W + 1)] for j in range(H + 1)]
for i in range(H + 1):
	for j in range(W + 1):
		C[i][j] = abs(A[i][j] - B[i][j])
ST = 1 << 80 * 160 + 1
dp = [[0 for i in range(W + 1)] for j in range(H + 1)]
dp[0][1] = ST
for i in range(1, H + 1):
	for j in range(1, W + 1):
		dp[i][j] |= dp[i - 1][j] << C[i][j] | dp[i - 1][j] >> C[i][j]
		dp[i][j] |= dp[i][j - 1] << C[i][j] | dp[i][j - 1] >> C[i][j]
ANS = list(bin(dp[-1][-1] >> 80 * 160 + 1))[::-1]
for i in range(len(ANS)):
	if ANS[i] == '1':
		print(i)
		exit()


(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]
dif = []
for (aj, bj) in zip(a, b):
	dif.append([abs(aij - bij) for (aij, bij) in zip(aj, bj)])
dp = [0] * w
dp[0] = 1 << 6400
for i in range(h):
	dp[0] = dp[0] << dif[i][0] | dp[0] >> dif[i][0]
	for j in range(1, w):
		dp[j] = dp[j - 1] << dif[i][j] | dp[j - 1] >> dif[i][j] | dp[j] << dif[i][j] | dp[j] >> dif[i][j]
result = list(format(dp[-1] >> 6400, 'b'))[::-1]
ans = 0
for i in range(len(result)):
	if result[i] == '1':
		print(ans)
		break
	else:
		ans += 1


import sys

def input():
	return sys.stdin.readline().rstrip()

def main():
	(h, w) = 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)]
	abss = [[abs(A[i][j] - B[i][j]) for j in range(w)] for i in range(h)]
	dp = [[0] * w for i in range(h)]
	dp[0][0] = 1 << abss[0][0] + 13000
	for i in range(h):
		for j in range(w):
			if i != h - 1:
				dp[i + 1][j] = dp[i + 1][j] | dp[i][j] << abss[i + 1][j] | dp[i][j] >> abss[i + 1][j]
			if j != w - 1:
				dp[i][j + 1] = dp[i][j + 1] | dp[i][j] << abss[i][j + 1] | dp[i][j] >> abss[i][j + 1]
	a = 0
	ans = dp[-1][-1]
	while True:
		if ans & 1 << 13000 - a:
			print(a)
			break
		elif ans & 1 << 13000 + a:
			print(a)
			break
		a += 1

def __starting_point():
	main()
__starting_point()


import sys
from collections import deque
(h, w) = map(int, input().split())
graph = []
for i in range(h):
	a = list(map(int, sys.stdin.readline().split()))
	graph.append(a)
for i in range(h):
	b = list(map(int, sys.stdin.readline().split()))
	for j in range(w):
		graph[i][j] = abs(graph[i][j] - b[j])
result = [[0] * w for _ in range(h)]
result[0][0] = 1 << graph[0][0]

def ts(a, b):
	result = 0
	for i in range(b + 1):
		if a >> i & 1 > 0:
			result |= 1 << b >> i
	return result
for i in range(1, h + w - 1):
	for j in range(max(0, i - (w - 1)), min(i + 1, h)):
		ps = 0
		nt = graph[j][i - j]
		if j > 0:
			k = result[j - 1][i - j]
			result[j][i - j] |= k << nt | k >> nt | ts(k, nt)
		if i - j > 0:
			k = result[j][i - j - 1]
			result[j][i - j] |= k << nt | k >> nt | ts(k, nt)
ans = result[h - 1][w - 1]
ans = (-ans & ans).bit_length() - 1
print(ans)


import sys
sys.setrecursionlimit(2147483647)
INF = float('inf')
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().rstrip()

def resolve():
	ofs = 6400
	(h, w) = map(int, input().split())
	A = [list(map(int, input().split())) for _ in range(h)]
	B = [list(map(int, input().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(range(h), 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
resolve()


(H, W) = map(int, input().split())
A = [[0] * W for i in range(H)]
B = [[0] * W for i in range(H)]
for i in range(H):
	inf = [int(c) for c in input().split()]
	for j in range(W):
		A[i][j] = inf[j]
for i in range(H):
	inf = [int(c) for c in input().split()]
	for j in range(W):
		B[i][j] = inf[j]
X = (H + W) * 80
dp = [[0] * W for i in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0] = 1 << X + d
for i in range(H):
	for j in range(W):
		if i == 0 and j == 0:
			continue
		d = abs(A[i][j] - B[i][j])
		if i != 0:
			dp[i][j] |= dp[i - 1][j] >> d
			dp[i][j] |= dp[i - 1][j] << d
		if j != 0:
			dp[i][j] |= dp[i][j - 1] >> d
			dp[i][j] |= dp[i][j - 1] << d
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 - X)
		if m < ans:
			ans = m
print(ans)


(h, w) = map(int, input().split())
lsa = [list(map(int, input().split())) for i in range(h)]
lsb = [list(map(int, input().split())) for i in range(h)]
ls = [[abs(lsa[i][j] - lsb[i][j]) for j in range(w)] for i in range(h)]
bias = 80 * 80 * 80
dp = [[0 for _ in range(w)] for _ in range(h)]
dp[0][0] = 1 << ls[0][0] + bias
for i in range(h):
	for j in range(w):
		if j != 0:
			dp[i][j] |= dp[i][j - 1] << ls[i][j]
			dp[i][j] |= dp[i][j - 1] >> ls[i][j]
		if i != 0:
			dp[i][j] |= dp[i - 1][j] << ls[i][j]
			dp[i][j] |= dp[i - 1][j] >> ls[i][j]
ans = dp[h - 1][w - 1]
idx = 0
while 1:
	if 1 << bias + idx & ans or 1 << bias - idx & ans:
		break
	idx += 1
print(idx)


import numpy as np
(H, W) = list(map(int, input().split()))
A = np.array([list(map(int, input().split())) for _ in range(H)])
B = np.array([list(map(int, input().split())) for _ in range(H)])
C = abs(A - B)
D = 80 * (H + W + 1) + 10
D2 = D * 2
dp = [[0] * W for _ in range(H)]
dp[0][0] = 1 << D - int(C[0][0])
dp[0][0] |= 1 << D + int(C[0][0])
dp = np.array(dp)
for h in range(H):
	for w in range(W):
		tmp = int(C[h][w])
		if h:
			dp[h][w] |= dp[h - 1][w] << tmp
			dp[h][w] |= dp[h - 1][w] >> tmp
		if w:
			dp[h][w] |= dp[h][w - 1] << tmp
			dp[h][w] |= dp[h][w - 1] >> tmp
ans = D2
tmp = dp[H - 1][W - 1]
for i in range(D2):
	if tmp >> i & 1:
		ans = min(ans, abs(i - D))
print(ans)


from itertools import product

def offset(a, b):
	result = 0
	if b == 0:
		return 0
	for i in range(b + 1):
		if a >> i & 1:
			result |= 1 << b >> i
	return result
(H, W) = map(int, input().split())
diff = [list(map(int, input().split())) for _ in range(H)]
M = 80
for i in range(H):
	B = list(map(int, input().split()))
	for j in range(W):
		diff[i][j] = abs(diff[i][j] - B[j])
dp = [[0] * W for _ in range(H)]
dp[0][0] = 1 << diff[0][0]
for (i, j) in product(range(H), range(W)):
	if (i, j) == (H - 1, W - 1):
		break
	if i + 1 < H:
		k = diff[i + 1][j]
		dp[i + 1][j] |= dp[i][j] >> k | dp[i][j] << k | offset(dp[i][j], k)
	if j + 1 < W:
		k = diff[i][j + 1]
		dp[i][j + 1] |= dp[i][j] >> k | dp[i][j] << k | offset(dp[i][j], k)
ans = dp[H - 1][W - 1]
print((ans & -ans).bit_length() - 1)


def main():
	(h, w) = list(map(int, input().split()))
	L = 80 * (h + w) + 1
	a = [list(map(int, input().split())) for i in range(h)]
	b = [list(map(int, input().split())) for i in range(h)]
	x = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]
	L = 12800
	state = [[0] * w for _ in range(h)]
	delta = x[0][0]
	state[0][0] = 2 ** L >> delta | 2 ** L << delta
	for j in range(1, w):
		delta = x[0][j]
		state[0][j] = state[0][j - 1] >> delta | state[0][j - 1] << delta
	for i in range(1, h):
		delta = x[i][0]
		state[i][0] = state[i - 1][0] >> delta | state[i - 1][0] << delta
	for i in range(1, h):
		for j in range(1, w):
			delta = x[i][j]
			state[i][j] = state[i][j - 1] >> delta | state[i][j - 1] << delta
			state[i][j] |= state[i - 1][j] >> delta | state[i - 1][j] << delta
	ans = 10 ** 9
	for i in range(L * 2):
		if state[h - 1][w - 1] & 1:
			ans = min(ans, abs(i - L))
		state[h - 1][w - 1] >>= 1
	print(ans)

def __starting_point():
	main()
__starting_point()


def max2(x, y):
	return x if x > y else y
(H, W) = map(int, input().split())
A = []
B = []
ofs = 80 * H * W
for _ in range(H):
	A.append(list(map(int, input().split())))
for _ in range(H):
	B.append(list(map(int, input().split())))
d = [[0] * W for _ in range(H)]
for h in range(H):
	for w in range(W):
		d[h][w] = abs(A[h][w] - B[h][w])
dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1 << ofs
dp[0][0] = dp[0][0] << d[0][0] | dp[0][0] >> d[0][0]
for h in range(H):
	for w in range(W):
		if h < H - 1:
			dp[h + 1][w] |= dp[h][w] << d[h + 1][w] | dp[h][w] >> d[h + 1][w]
		if w < W - 1:
			dp[h][w + 1] |= dp[h][w] << d[h][w + 1] | dp[h][w] >> d[h][w + 1]
res = dp[-1][-1] >> ofs
print((-res & res).bit_length() - 1)


import sys
import numpy as np
input = sys.stdin.readline
(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)]
C = [[abs(a - b) for (a, b) in zip(la, lb)] for (la, lb) in zip(A, B)]
MAX = 160 * (80 + 80) + 10
vals = np.zeros((W, MAX)).astype(bool)
temp = np.zeros(MAX).astype(bool)
temp[C[0][0]] = True
for y in range(H):
	for x in range(W):
		c = C[y][x]
		if c == 0:
			if y > 0:
				temp |= vals[x]
			if x > 0:
				temp |= vals[x - 1]
			vals[x] &= False
			vals[x] |= temp
			temp &= False
		else:
			if y > 0:
				temp[c:] |= vals[x][:-c]
				temp[:c + 1] |= vals[x][:c + 1][::-1]
				temp[:-c] |= vals[x][c:]
			if x > 0:
				temp[c:] |= vals[x - 1][:-c]
				temp[:c + 1] |= vals[x - 1][:c + 1][::-1]
				temp[:-c] |= vals[x - 1][c:]
			vals[x] &= False
			vals[x] |= temp
			temp &= False
print(np.where(vals[W - 1])[0][0])


(H, W) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
dp = [0] * W
dp[0] = s = 1 << 6400
for (a, b) in zip(A, B):
	for (j, (aa, bb)) in enumerate(zip(a, b)):
		d = abs(aa - bb)
		if j > 0:
			dp[j] |= dp[j - 1]
		dp[j] = dp[j] << d | dp[j] >> d
g = dp[-1]
for i in range(6401):
	if g & s << i or g & s >> i:
		print(i)
		quit()


e = enumerate
(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]
dp = [0] * w
c = 13000
for (i, (a, b)) in e(zip(a, b)):
	for (j, (x, y)) in e(zip(a, b)):
		x = abs(x - y)
		if i == j == 0:
			dp[j] = 2 ** c << x
			continue
		t = 0
		if j:
			t |= dp[j - 1] << x
			t |= dp[j - 1] >> x
		if i:
			t |= dp[j] << x
			t |= dp[j] >> x
		dp[j] = t
(*a,) = map(int, bin(dp[w - 1])[2:].zfill(26001))
print(min([i for (i, f) in e(a[13000:]) if f] + [i for (i, f) in e(a[13000::-1]) if f]))


(h, w) = 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)]
grid = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
	for j in range(w):
		grid[i][j] = abs(a[i][j] - b[i][j])
dp = [[0 for j in range(w)] for k in range(h)]
buf = (h + w) * 80
for i in range(h):
	for j in range(w):
		t = grid[i][j]
		if i == j == 0:
			dp[i][j] = 1 << buf + t | 1 << buf - t
		if j > 0:
			dp[i][j] = dp[i][j] | dp[i][j - 1] << t | dp[i][j - 1] >> t
		if i > 0:
			dp[i][j] = dp[i][j] | dp[i - 1][j] << t | dp[i - 1][j] >> t
for i in range(buf):
	if dp[h - 1][w - 1] & 1 << buf + i or dp[h - 1][w - 1] & 1 << buf - i:
		print(i)
		return


import sys
input = sys.stdin.buffer.readline

def main():
	(h, w) = map(int, input().split())
	A = [list(map(int, input().split())) for _ in range(h)]
	B = [list(map(int, input().split())) for _ in range(h)]
	C = [[0] * w for _ in range(h)]
	for i in range(h):
		for j in range(w):
			C[i][j] = abs(A[i][j] - B[i][j])
	D = [[0] * w for _ in range(h)]
	D[0][0] = 1 << C[0][0] + 5000 | 1 >> C[0][0] + 5000
	for i in range(h):
		for j in range(w):
			if i > 0:
				D[i][j] = D[i][j] | D[i - 1][j] << C[i][j] | D[i - 1][j] >> C[i][j]
			if j > 0:
				D[i][j] = D[i][j] | D[i][j - 1] << C[i][j] | D[i][j - 1] >> C[i][j]
	val = D[-1][-1]
	ans = 5000
	for i in range(10000):
		if val >> i & 1:
			ans = min(ans, abs(i - 5000))
	print(ans)
main()


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 5)
(H, W) = map(int, input().split())
A = [[int(i) for i in input().split()] for _ in range(H)]
B = [[int(i) for i in input().split()] for _ in range(H)]
C = [[abs(A[y][x] - B[y][x]) for x in range(W)] for y in range(H)]
N = 13000
half = N // 2
dp = [[0] * W for _ in range(H)]
dp[0][0] = 1 << half - C[0][0] | 1 << half + C[0][0]
for y in range(H):
	for x in range(W):
		if y != 0:
			dp[y][x] |= dp[y - 1][x] >> C[y][x] | dp[y - 1][x] << C[y][x]
		if x != 0:
			dp[y][x] |= dp[y][x - 1] >> C[y][x] | dp[y][x - 1] << C[y][x]
b = bin(dp[-1][-1])[2:][::-1]
ret = float('inf')
for (k, v) in enumerate(b):
	if v == '1':
		ret = min(ret, abs(k - half))
print(ret)


(H, W) = map(int, input().split())
A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
D = 80 * (H + W) + 1
D2 = D * 2
dp = [[0] * W for _ in range(H)]
dp[0][0] |= 1 << D + C[0][0]
dp[0][0] |= 1 << D - C[0][0]
for h in range(H):
	for w in range(W):
		c = C[h][w]
		if h >= 1:
			dp[h][w] |= dp[h - 1][w] << c
			dp[h][w] |= dp[h - 1][w] >> c
		if w >= 1:
			dp[h][w] |= dp[h][w - 1] << c
			dp[h][w] |= dp[h][w - 1] >> c
ans = D2
tmp = dp[H - 1][W - 1]
for i in range(D2):
	if tmp >> i & 1:
		ans = min(ans, abs(i - D))
print(ans)


import sys, math, itertools, heapq, collections, bisect, copy
input = lambda : sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10 ** 7)
inf = float('inf')
mod = 10 ** 9 + 7
ans = inf
count = 0
pro = 1
(h, w) = map(int, input().split())
A = [tuple(map(int, input().split())) for i in range(h)]
B = [tuple(map(int, input().split())) for i in range(h)]
K = 160 * 80
dp = [[0] * w for i in range(h)]
dp[0][0] = 1 << K
for i in range(h):
	for j in range(w):
		d = abs(A[i][j] - B[i][j])
		if i - 1 >= 0:
			dp[i][j] |= dp[i - 1][j]
		if j - 1 >= 0:
			dp[i][j] |= dp[i][j - 1]
		dp[i][j] = dp[i][j] << d | dp[i][j] >> d
ans = K
for i in range(2 * K):
	if dp[h - 1][w - 1] & 1 << i > 0:
		ans = min(ans, abs(K - i))
print(ans)


(R, C) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(R)]
B = [list(map(int, input().split())) for _ in range(R)]
dp = [0] * C
dp[0] = s = 1 << 6400
for (row_a, row_b) in zip(A, B):
	for (i, (a, b)) in enumerate(zip(row_a, row_b)):
		d = abs(a - b)
		if i > 0:
			dp[i] |= dp[i - 1]
		dp[i] = dp[i] << d | dp[i] >> d
g = dp[-1]
for i in range(6401):
	if g & s << i or g & s >> i:
		print(i)
		quit()


(h, w) = list(map(int, input().split()))
a = []
for i in range(h):
	ai = list(map(int, input().split()))
	a.append(ai)
for i in range(h):
	bi = list(map(int, input().split()))
	for j in range(w):
		a[i][j] -= bi[j]
		a[i][j] = abs(a[i][j])
dp = [[0 for j in range(w)] for i in range(h)]
a2 = [a[i][j] for i in range(h) for j in range(w)]
mxa = max(a2)
tmp = 1 << mxa * (h + w) + 10
dp[0][0] |= tmp << a[0][0]
dp[0][0] |= tmp >> a[0][0]
for j in range(1, w):
	dp[0][j] = dp[0][j - 1] << a[0][j]
	dp[0][j] |= dp[0][j - 1] >> a[0][j]
for i in range(1, h):
	for j in range(w):
		dp[i][j] = dp[i - 1][j] << a[i][j]
		dp[i][j] |= dp[i - 1][j] >> a[i][j]
		if j:
			dp[i][j] |= dp[i][j - 1] << a[i][j]
			dp[i][j] |= dp[i][j - 1] >> a[i][j]
tmp1 = dp[h - 1][w - 1]
tmp1 >>= mxa * (h + w) + 10
for k in range(mxa * (h + w) + 10):
	if tmp1 & 1:
		print(k)
		return
	tmp1 >>= 1


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write

def solve():
	ofs = 700
	(h, w) = 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 + 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
	for (i, s) in enumerate(bin(dp[w - 1])[-ofs - 1:]):
		if int(s):
			write('%d\n' % i)
			return
solve()


(H, W) = map(int, input().split())
A = []
B = []
abs_diff = [[0] * (W + 1) for h in range(H + 1)]
for h in range(H):
	A.append(list(map(int, input().split())))
for h in range(H):
	B.append(list(map(int, input().split())))
max_diff = 0
for h in range(H):
	for w in range(W):
		d = abs(A[h][w] - B[h][w])
		max_diff = max(max_diff, d)
		abs_diff[h + 1][w + 1] = d
base = max_diff * (H + W)
dp = [[0 for w in range(W + 1)] for h in range(H + 1)]
dp[0][1] = dp[1][0] = 1 << base
for h in range(1, H + 1):
	for w in range(1, W + 1):
		dp[h][w] = dp[h - 1][w] << abs_diff[h][w] | dp[h - 1][w] >> abs_diff[h][w] | dp[h][w - 1] << abs_diff[h][w] | dp[h][w - 1] >> abs_diff[h][w]
positive = bin(dp[H][W] >> base)
print(positive[::-1].find('1'))


def main():
	(H, W) = map(int, input().split())
	MAX_BIAS = 80 * (H + W - 1)
	A = []
	for _ in range(H):
		A += map(int, input().split())
	B = []
	for _ in range(H):
		B += map(int, input().split())
	dp = [0] * (H * W)
	dp[0] |= 1 << MAX_BIAS
	for r in range(H):
		for c in range(W):
			p = r * W + c
			d = abs(A[p] - B[p])
			x = dp[p]
			if r > 0:
				x |= dp[p - W]
			if c > 0:
				x |= dp[p - 1]
			dp[p] = x << d | x >> d
	ans = MAX_BIAS
	for j in range(2 * MAX_BIAS + 1):
		if dp[H * W - 1] >> j & 1:
			t = j - MAX_BIAS
			if ans > abs(t):
				ans = abs(t)
	print(ans)
main()


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write

def resolve():
	ofs = 3200
	(h, w) = 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
INF = float('inf')
MOD = 10 ** 9 + 7
resolve()


import sys

def main():
	input = sys.stdin.readline
	(h, w) = map(int, input().split())
	a = [list(map(int, input().split())) for _ in range(h)]
	b = [list(map(int, input().split())) for _ in range(h)]
	m = 80 * 162
	flg = (1 << m * 2) - 1
	dp = [[0] * w for j in range(h)]
	dp[0][0] |= 1 << a[0][0] - b[0][0] + m
	dp[0][0] |= 1 << b[0][0] - a[0][0] + m
	for i in range(h):
		for j in range(w):
			if i < h - 1:
				ne = abs(a[i + 1][j] - b[i + 1][j])
				dp[i + 1][j] |= dp[i][j] << ne & flg
				dp[i + 1][j] |= dp[i][j] >> ne & flg
			if j < w - 1:
				ne = abs(a[i][j + 1] - b[i][j + 1])
				dp[i][j + 1] |= dp[i][j] << ne & flg
				dp[i][j + 1] |= dp[i][j] >> ne & flg
	ans = 10 ** 10
	for i in range(160 * 162):
		if dp[h - 1][w - 1] >> i & 1:
			ans = min(ans, abs(i - m))
	return print(ans)
main()


def resolve():
	(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)]
	Sub = [[0] * W for i in range(H)]
	for i in range(H):
		for j in range(W):
			Sub[i][j] = abs(A[i][j] - B[i][j])
	max_k = 80 * (H + W)
	dp = [[0] * W for _ in range(H)]
	dp[0][0] = 1 << max_k + Sub[0][0] | 1 << max_k - Sub[0][0]
	for i in range(H):
		for j in range(W):
			d = Sub[i][j]
			if i:
				dp[i][j] |= dp[i - 1][j] << d | dp[i - 1][j] >> d
			if j:
				dp[i][j] |= dp[i][j - 1] << d | dp[i][j - 1] >> d
	ans = 10 ** 18
	for i in range(max_k * 2 + 1):
		if dp[H - 1][W - 1] & 1 << i:
			ans = min(ans, abs(max_k - i))
	print(ans)

def __starting_point():
	resolve()
__starting_point()


from collections import deque
(H, W) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
C = [[0] * W for _ in range(H)]
for i in range(H):
	for j in range(W):
		C[i][j] = abs(A[i][j] - B[i][j])
M = 80 * (H + W)
dp = [[0 for i in range(W)] for j in range(H)]
c = C[0][0]
dp[0][0] += 1 << c + M | 1 << M - c
for s in range(1, H + W):
	for b in range(max(s - H + 1, 0), min(s + 1, W)):
		a = s - b
		c = C[a][b]
		if a >= 1:
			d = dp[a - 1][b]
			dp[a][b] |= d << c | d >> c
		if b >= 1:
			d = dp[a][b - 1]
			dp[a][b] |= d << c | d >> c
ans = 10 ** 10
d = dp[-1][-1]
for i in range(0, M * 2 + 1):
	if 1 << i & d:
		ans = min(ans, abs(i - M))
print(ans)


(h, w) = 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)]
pos = 160 * 80
dp = [[0] * w for i in range(h)]
sub = abs(a[0][0] - b[0][0])
dp[0][0] = 1 << pos - sub | 1 << pos + sub
for c in range(h):
	for l in range(w):
		sub = abs(a[c][l] - b[c][l])
		if l != 0 and c != 0:
			dp[c][l] |= dp[c][l - 1] << sub | dp[c][l - 1] >> sub | dp[c - 1][l] << sub | dp[c - 1][l] >> sub
		elif c != 0:
			dp[c][l] |= dp[c - 1][l] << sub | dp[c - 1][l] >> sub
		elif l != 0:
			dp[c][l] |= dp[c][l - 1] << sub | dp[c][l - 1] >> sub
bit = dp[-1][-1]
for i in range(pos):
	if bit & 1 << pos + i or bit & 1 << pos - i:
		print(i)
		break


(H, W) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
dp = [[0] * (W + 1) for _ in range(H + 1)]
F = 80 * 200 + 100
dp[0][0] = 1 << F
for h in range(H):
	for w in range(W):
		a = A[h][w]
		b = B[h][w]
		d = dp[h][w]
		dp[h + 1][w] |= d << abs(a - b) | d >> abs(a - b)
		dp[h][w + 1] |= d << abs(a - b) | d >> abs(a - b)
res = dp[H][W - 1] | dp[H - 1][W]
ans = 10 ** 18
for d in range(F * 2):
	if res & 1 << d != 0:
		ans = min(ans, abs(d - F))
print(ans)


import numpy as np
(H, W) = list(map(int, input().split()))
As = [list(map(int, input().split())) for _ in range(H)]
Bs = [list(map(int, input().split())) for _ in range(H)]
ds = [[abs(A - B) for (A, B) in zip(row_A, row_B)] for (row_A, row_B) in zip(As, Bs)]
size = 10005
dp = np.full((H, W, size), False)
dp[0][0][ds[0][0]] = True
for i in range(H):
	for j in range(W):
		if i + 1 < H:
			d = ds[i + 1][j]
			dp[i + 1][j][d:] = np.logical_or(dp[i][j][:size - d], dp[i + 1][j][d:])
			dp[i + 1][j][:size - d] = np.logical_or(dp[i][j][d:], dp[i + 1][j][:size - d])
			dp[i + 1][j][:d + 1] = np.logical_or(dp[i][j][d::-1], dp[i + 1][j][:d + 1])
		if j + 1 < W:
			d = ds[i][j + 1]
			dp[i][j + 1][d:] = np.logical_or(dp[i][j][:size - d], dp[i][j + 1][d:])
			dp[i][j + 1][:size - d] = np.logical_or(dp[i][j][d:], dp[i][j + 1][:size - d])
			dp[i][j + 1][:d + 1] = np.logical_or(dp[i][j][d::-1], dp[i][j + 1][:d + 1])
print(np.min(np.where(dp[-1][-1])))


(H, W) = 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)


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)


def inpl():
	return list(map(int, input().split()))
(H, W) = inpl()
A = [inpl() for _ in range(H)]
B = [inpl() for _ in range(H)]
DP = [[0 for _ in range(W + 1)] for _ in range(H + 1)]
DP[0][0] = 1 << 12800
for h in range(H):
	for w in range(W):
		d = abs(A[h][w] - B[h][w])
		DP[h + 1][w] |= DP[h][w] >> d
		DP[h + 1][w] |= DP[h][w] << d
		DP[h][w + 1] |= DP[h][w] >> d
		DP[h][w + 1] |= DP[h][w] << d
l = 1 << 12800
r = 1 << 12800
for i in range(12801):
	if DP[-1][-2] & l | DP[-1][-2] & r:
		print(i)
		break
	l = l << 1
	r = r >> 1


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


(h, w) = 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)]
ma = 80 * 2 * 80
DP = [[0] * w for i in range(h)]
for i in range(h):
	for j in range(w):
		k = abs(a[i][j] - b[i][j])
		if i | j:
			m = DP[i - 1][j] | DP[i][j - 1]
		else:
			m = 1 << ma
		DP[i][j] = m << k | m >> k
k = 1 << ma
for i in range(ma):
	if DP[-1][-1] & k >> i or DP[-1][-1] & k << i:
		print(i)
		exit()


(h, w) = map(int, input().split())
a = [list(map(int, input().split())) + [0] for i in range(h)] + [[0] * (w + 1)]
b = [list(map(int, input().split())) + [0] for i in range(h)] + [[0] * (w + 1)]
diff = [[abs(x - y) for (x, y) in zip(i, j)] for (i, j) in zip(a, b)]
dp = [[0 for j in range(w + 1)] for i in range(h + 1)]
dp[0][0] = 1 << diff[0][0]

def reverse(d, x):
	res = 0
	for i in range(x + 1):
		res <<= 1
		if 1 << i & d:
			res |= 1
	return res
for i in range(h):
	for j in range(w):
		dp[i][j + 1] |= dp[i][j] << diff[i][j + 1]
		dp[i][j + 1] |= dp[i][j] >> diff[i][j + 1]
		dp[i][j + 1] |= reverse(dp[i][j], diff[i][j + 1])
		dp[i + 1][j] |= dp[i][j] << diff[i + 1][j]
		dp[i + 1][j] |= dp[i][j] >> diff[i + 1][j]
		dp[i + 1][j] |= reverse(dp[i][j], diff[i + 1][j])
last = dp[h - 1][w - 1]
for i in range(6401):
	if last >> i & 1:
		ans = i
		break
print(ans)


import sys
INF = float('inf')

def solve(H: int, W: int, A: 'List[List[int]]', B: 'List[List[int]]'):
	base = [[abs(A[h][w] - B[h][w]) for w in range(W)] for h in range(H)]
	mx = (1 << 12800) - 1
	table = [[0] * W for _ in range(H)]
	table[0][0] = 1 << 6400 + base[0][0] | 1 << 6400 - base[0][0]
	for h in range(H):
		for w in range(W):
			if h > 0:
				table[h][w] |= table[h - 1][w] << base[h][w]
				table[h][w] |= table[h - 1][w] >> base[h][w]
			if w > 0:
				table[h][w] |= table[h][w - 1] << base[h][w]
				table[h][w] |= table[h][w - 1] >> base[h][w]
			table[h][w] &= mx
	m = INF
	for j in range(12800):
		i = table[H - 1][W - 1] >> j & 1
		if i == 1:
			m = min(m, abs(j - 6400))
	print(m)
	return

def main():

	def iterate_tokens():
		for line in sys.stdin:
			for word in line.split():
				yield word
	tokens = iterate_tokens()
	H = int(next(tokens))
	W = int(next(tokens))
	A = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]
	B = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]
	solve(H, W, A, B)

def __starting_point():
	main()
__starting_point()


(h, w) = 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)]
s = [0] * w
s[0] = p = 1 << 6400
for i in range(h):
	for j in range(w):
		d = abs(a[i][j] - b[i][j])
		if j > 0:
			s[j] |= s[j - 1]
		s[j] = s[j] << d | s[j] >> d
t = s[-1]
for i in range(6401):
	if t & p << i or t & p >> i:
		print(i)
		break


import sys
input = sys.stdin.readline
(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)]
D = [[0] * W for i in range(H)]
for i in range(H):
	for j in range(W):
		D[i][j] = abs(A[i][j] - B[i][j])
buf = 80 * (H + W)
dp = [[0] * W for i in range(H)]
dp[0][0] = 1 << buf + D[0][0] | 1 << buf - D[0][0]
for i in range(H):
	for j in range(W):
		d = D[i][j]
		if i:
			dp[i][j] |= dp[i - 1][j] << d | dp[i - 1][j] >> d
		if j:
			dp[i][j] |= dp[i][j - 1] << d | dp[i][j - 1] >> d
ans = 10 ** 18
for i in range(buf * 2 + 1):
	if dp[H - 1][W - 1] & 1 << i:
		ans = min(ans, abs(buf - i))
print(ans)


import numpy as np
(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]
d = []
for i in range(h):
	d.append([abs(a[i][j] - b[i][j]) for j in range(w)])
dp = np.zeros((h, w, 12800))
dp[0, 0, d[0][0] + 6400] = 1
for i in range(w - 1):
	if d[0][i + 1] == 0:
		dp[0, i + 1] += dp[0, i]
		continue
	dp[0, i + 1, d[0][i + 1]:] += dp[0, i, :-d[0][i + 1]]
	dp[0, i + 1, :-d[0][i + 1]] += dp[0, i, d[0][i + 1]:]
for i in range(h - 1):
	if d[i + 1][0] == 0:
		dp[i + 1, 0] += dp[i, 0]
		continue
	dp[i + 1, 0, d[i + 1][0]:] += dp[i, 0, :-d[i + 1][0]]
	dp[i + 1, 0, :-d[i + 1][0]] += dp[i, 0, d[i + 1][0]:]
for r in range(1, h):
	for c in range(1, w):
		if d[r][c] == 0:
			dp[r, c] += dp[r - 1, c]
			dp[r, c] += dp[r, c - 1]
			continue
		dp[r, c, d[r][c]:] += dp[r - 1, c, :-d[r][c]]
		dp[r, c, :-d[r][c]] += dp[r - 1, c, d[r][c]:]
		dp[r, c, d[r][c]:] += dp[r, c - 1, :-d[r][c]]
		dp[r, c, :-d[r][c]] += dp[r, c - 1, d[r][c]:]
print(min([abs(e - 6400) for e in np.where(dp[h - 1, w - 1] > 0)[0]]))


from sys import stdin
input = stdin.buffer.readline
(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]
dp = [0] * -~w
dp[0] = 1 << 6400
for i in range(h):
	for j in range(w):
		t = dp[j]
		if j:
			t |= dp[j - 1]
		x = abs(a[i][j] - b[i][j])
		dp[j] = t << x | t >> x
for (i, x) in enumerate(bin(dp[w - 1])[-6401:]):
	if int(x):
		print(i)
		exit(0)


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


import numpy as np
(H, W) = map(int, input().split())
A = np.zeros((H, W))
for c in [1, -1]:
	for i in range(H):
		num = list(map(int, input().split()))
		A[i, :] += np.array(num) * c
M = 160 * 80
dp = [[0] * W for _ 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] << int(a) | dp[i][j] >> int(a)
i = M
while True:
	if dp[H - 1][W - 1] >> i & 1:
		print(i - M)
		break
	i += 1


import sys
input = sys.stdin.readline
(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]
ab = [[0] * w for _ in range(h)]
for i in range(h):
	for j in range(w):
		ab[i][j] = abs(a[i][j] - b[i][j])
m = 12800
dp = [[0] * w for _ in range(h)]
dp[0][0] = 1 << m + ab[0][0] | 1 << m - ab[0][0]
for i in range(h):
	for j in range(w):
		for (dx, dy) in [(1, 0), (0, 1)]:
			(nx, ny) = (i + dx, j + dy)
			if 0 <= nx < h and 0 <= ny < w:
				dp[nx][ny] = dp[nx][ny] | dp[i][j] << ab[nx][ny]
				dp[nx][ny] = dp[nx][ny] | dp[i][j] >> ab[nx][ny]
for i in range(m + 1):
	if dp[h - 1][w - 1] >> i & 1:
		ans = m - i
print(ans)


(H, W) = 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)]
center = 80 * (H + W)
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[0] * W for i in range(H)]
dp[0][0] = 1 << center + C[0][0] | 1 >> center - C[0][0]
for i in range(H):
	for j in range(W):
		if i:
			dp[i][j] |= dp[i - 1][j] << C[i][j]
			dp[i][j] |= dp[i - 1][j] >> C[i][j]
		if j:
			dp[i][j] |= dp[i][j - 1] << C[i][j]
			dp[i][j] |= dp[i][j - 1] >> C[i][j]
ans = center * 2
for i in range(center * 2):
	if dp[H - 1][W - 1] >> i & 1:
		ans = min(ans, abs(i - center))
print(ans)


import numpy as np
(H, W, *L) = map(int, open(0).read().split())
A = L[:H * W]
B = L[H * W:]
C = [abs(b - a) for (a, b) in zip(A, B)]
U = max(C) * (H + W)
N = 2 * U + 1
INF = 10 ** 10
dp = np.zeros((H, W, N), dtype=bool)
dp[0, 0, C[0] + U] = True
for i in range(1, W):
	c = C[i]
	dp[0, i, c:] |= dp[0, i - 1, :N - c]
	dp[0, i, :N - c] |= dp[0, i - 1, c:]
for i in range(1, H):
	c = C[i * W]
	dp[i, 0, c:] |= dp[i - 1, 0, :N - c]
	dp[i, 0, :N - c] |= dp[i - 1, 0, c:]
for i in range(1, H):
	for j in range(1, W):
		c = C[i * W + j]
		dp[i, j, c:] |= dp[i, j - 1, :N - c]
		dp[i, j, :N - c] |= dp[i, j - 1, c:]
		dp[i, j, c:] |= dp[i - 1, j, :N - c]
		dp[i, j, :N - c] |= dp[i - 1, j, c:]
ans = np.min(np.abs(np.where(dp[H - 1][W - 1])[0] - U))
print(ans)


(H, W) = map(int, input().split())
A = tuple((tuple(map(int, input().split())) for _ in range(H)))
B = tuple((tuple(map(int, input().split())) for _ in range(H)))
table = [0] * W
table[0] = 1 << 6400
for i in range(H):
	for j in range(W):
		shamt = abs(A[i][j] - B[i][j])
		if j > 0:
			table[j] |= table[j - 1]
		table[j] = table[j] << shamt | table[j] >> shamt
dl = dr = 1 << 6400
for i in range(6401):
	if table[W - 1] & (dl | dr):
		print(i)
		break
	dl <<= 1
	dr >>= 1


(H, W) = list(map(int, input().split()))
amap = [list(map(int, input().split())) for i in range(H)]
bmap = [list(map(int, input().split())) for i in range(H)]
dist_map = [[amap[y][x] - bmap[y][x] for x in range(W)] for y in range(H)]
mid = 80 * 80 * 5 // 2
dp = [[1 << mid] * W for y in range(H)]
dp[0][0] = 1 << dist_map[0][0] + mid | 1 << mid - dist_map[0][0]
for x in range(1, W):
	y = 0
	d = abs(dist_map[y][x])
	dp[y][x] = dp[y][x - 1] << d | dp[y][x - 1] >> d
for y in range(1, H):
	x = 0
	d = abs(dist_map[y][x])
	dp[y][x] = dp[y - 1][x] << d | dp[y - 1][x] >> d
for y in range(1, H):
	for x in range(1, W):
		d = abs(dist_map[y][x])
		dp[y][x] = dp[y][x - 1] << d | dp[y][x - 1] >> d | (dp[y - 1][x] << d | dp[y - 1][x] >> d)
ans = dp[-1][-1]
for i in range(mid + 1):
	if ans & 1 << mid - i or ans & 1 << mid + i:
		print(i)
		return


import sys
input = sys.stdin.readline
(h, w) = map(int, input().split())
a = [tuple(map(int, input().split())) for _ in [0] * h]
b = [tuple(map(int, input().split())) for _ in [0] * h]
mid = 1 << 80 * 160
dp = [[0] * w for _ in [0] * h]
for i in range(h):
	for j in range(w):
		diff = abs(a[i][j] - b[i][j])
		new = 0
		if i > 0:
			new |= dp[i - 1][j]
		if j > 0:
			new |= dp[i][j - 1]
		if (i, j) == (0, 0):
			new = mid
		dp[i][j] = new << diff | new >> diff
res = 10 ** 9
p = dp[-1][-1]
for i in range(80 * 321):
	if p & 1 == 1:
		res = min(res, abs(80 * 160 - i))
	p = p >> 1
print(res)


(h, w) = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]
C = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]
dp = [[0] * w for i in range(h)]
ofs = 80 * (h + w)
dp[0][0] = 1 << ofs + C[0][0] | 1 << ofs - C[0][0]
for r in range(h):
	for c in range(w):
		if c > 0:
			dp[r][c] |= dp[r][c - 1] << C[r][c] | dp[r][c - 1] >> C[r][c]
		if r > 0:
			dp[r][c] |= dp[r - 1][c] << C[r][c] | dp[r - 1][c] >> C[r][c]
dp[h - 1][w - 1] >>= ofs
for i in range(ofs):
	if dp[h - 1][w - 1] >> i & 1:
		print(i)
		break


import sys
readline = sys.stdin.readline

def reverse(d, x):
	res = 0
	for i in range(x + 1):
		res <<= 1
		if 1 << i & d:
			res |= 1
	return res
(H, W) = map(int, readline().split())
A = [list(map(int, readline().split())) for _ in range(H)]
B = [list(map(int, readline().split())) for _ in range(H)]
G = [[0] * W for _ in range(H)]
for i in range(H):
	for j in range(W):
		G[i][j] = abs(A[i][j] - B[i][j])
dp = [[0] * W for _ in range(H)]
dp[0][0] = 1 << G[0][0]
for s in range(H + W - 1):
	for h in range(max(0, s - (W - 1)), 1 + min(s, H - 1)):
		w = s - h
		d = dp[h][w]
		if w != W - 1:
			g = G[h][w + 1]
			dp[h][w + 1] |= d << g | d >> g | reverse(d, g)
		if h != H - 1:
			g = G[h + 1][w]
			dp[h + 1][w] |= d << g | d >> g | reverse(d, g)
ans = dp[H - 1][W - 1]
ans = (-ans & ans).bit_length() - 1
print(ans)


(H, W) = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(H)]
B = [[int(a) for a in input().split()] for _ in range(H)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
X = [[0] * W for _ in range(H)]
ofs = 80 * (H + W)
X[0][0] = 1 << ofs + C[0][0] | 1 << ofs - C[0][0]
for i in range(H):
	for j in range(W):
		if i > 0:
			X[i][j] |= X[i - 1][j] << C[i][j] | X[i - 1][j] >> C[i][j]
		if j > 0:
			X[i][j] |= X[i][j - 1] << C[i][j] | X[i][j - 1] >> C[i][j]
for k in range(ofs):
	if X[i][j] >> k + ofs & 1:
		break
print(k)