AtCoder Beginner Contest 147 - Balanced Path

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.

Source: atcoder | Difficulty: MEDIUM


Solutions:
import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop
import itertools
import math
import copy
(H, W) = list(map(int, input().split()))
A = []
B = []
for _ in range(H):
	a = list(map(int, input().split()))
	A.append(a)
for _ in range(H):
	b = list(map(int, input().split()))
	B.append(b)
C = []
for i in range(H):
	cs = []
	for j in range(W):
		c = abs(A[i][j] - B[i][j])
		cs.append(c)
	C.append(cs)
X = (H + W) * 80
L = X + X + 1
dp = [[0] * W for _ in range(H)]
dp[0][0] = np.zeros(L, np.bool)
d = C[0][0]
dp[0][0][X + d] = 1
dp[0][0][X - d] = 1
for r in range(H):
	for c in range(W):
		if r == 0 and c == 0:
			continue
		l = np.zeros(L, np.bool)
		d = C[r][c]
		if r != 0:
			l[d:] |= dp[r - 1][c][:L - d]
			l[:L - d] |= dp[r - 1][c][d:]
		if c != 0:
			l[d:] |= dp[r][c - 1][:L - d]
			l[:L - d] |= dp[r][c - 1][d:]
		dp[r][c] = l
dp = dp[-1][-1]
d = np.where(dp)[0] - X
ans = np.abs(d).min()
print(ans)


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 main():
	e = enumerate
	((h, w), *a) = [list(map(int, t.split())) for t in open(0)]
	dp = [0] * w
	c = 12800
	for (i, a) in e(zip(a, a[h:])):
		for (j, (x, y)) in e(zip(*a)):
			x = abs(x - y)
			t = 0
			if j:
				t |= dp[j - 1] << x | dp[j - 1] >> x
			if i:
				t |= dp[j] << x | dp[j] >> x
			dp[j] = t if i + j else 2 ** c << x
	(*a,) = map(int, bin(dp[w - 1])[2:].zfill(c - ~c))
	print([i for (i, (f, g)) in e(zip(a[c:], a[c::-1])) if f + g][0])
main()


import sys
import numpy as np
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
(H, W) = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B).tolist()
X = (H + W) * 80
L = X + X + 1
dp = [[None] * W for _ in range(H)]
dp[0][0] = np.zeros(L, np.bool)
dp[0][0][X + diff[0][0]] = 1
dp[0][0][X - diff[0][0]] = 1
for h in range(H):
	for w in range(W):
		if h == 0 and w == 0:
			continue
		di = diff[h][w]
		x = np.zeros(L, np.bool)
		if h > 0:
			x[di:] |= dp[h - 1][w][:L - di]
			x[:L - di] |= dp[h - 1][w][di:]
		if w > 0:
			x[di:] |= dp[h][w - 1][:L - di]
			x[:L - di] |= dp[h][w - 1][di:]
		dp[h][w] = x
dp = dp[-1][-1][X:]
possible = np.where(dp == 1)[0]
answer = possible.min()
print(answer)


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)]
diff = [[0] * w for i in range(h)]
for i in range(h):
	for j in range(w):
		diff[i][j] = abs(a[i][j] - b[i][j])
dp = [0] * (h * w)
dp[0] |= 1 << 13000 + diff[0][0]
dp[0] |= 1 << 13000 - diff[0][0]
for k in range(h * w):
	i = k // w
	j = k % w
	if i + 1 < h:
		tmp1 = diff[i + 1][j]
		dp[k + w] |= dp[k] << tmp1
		dp[k + w] |= dp[k] >> tmp1
	if j + 1 < w:
		tmp2 = diff[i][j + 1]
		dp[k + 1] |= dp[k] << tmp2
		dp[k + 1] |= dp[k] >> tmp2
ans = 10 ** 9
for i in range(81):
	if dp[w * h - 1] >> 13000 + i & 1:
		ans = min(ans, i)
for i in range(81):
	if dp[w * h - 1] >> 13000 - i & 1:
		ans = min(ans, i)
print(ans)


(h, w) = 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()


(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 = [[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])
import numpy as np
dp = [[0] * (w + 1) for _ in range(h + 1)]
import numpy as np
d = 80 * (h + w - 1) + 1
D = 2 * d + 1
dp[0][0] = np.zeros(D, np.bool)
dp[0][0][C[0][0] + d] = 1
for i in range(h):
	for j in range(w):
		c = C[i][j]
		if i == 0 and j == 0:
			continue
		elif i == 0:
			b = np.zeros(D, np.bool)
			b[c:] |= dp[i][j - 1][:D - c]
			b[:D - c] |= dp[i][j - 1][c:]
		elif j == 0:
			b = np.zeros(D, np.bool)
			b[c:] |= dp[i - 1][j][:D - c]
			b[:D - c] |= dp[i - 1][j][c:]
		else:
			b = np.zeros(D, np.bool)
			b[c:] |= dp[i][j - 1][:D - c]
			b[:D - c] |= dp[i][j - 1][c:]
			b[c:] |= dp[i - 1][j][:D - c]
			b[:D - c] |= dp[i - 1][j][c:]
		dp[i][j] = b
ans = float('inf')
for i in range(D):
	if dp[h - 1][w - 1][i]:
		ans = min(ans, abs(i - d))
print(ans)


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


import sys
input = sys.stdin.readline
(h, w) = map(int, input().split())
red = [list(map(int, input().split())) + [0] for i in range(h)] + [[0 for i in range(w + 1)]]
blue = [list(map(int, input().split())) + [0] for i in range(h)] + [[0 for i in range(w + 1)]]
field = [[[0 for h in range(6500)] for i in range(w + 1)] for j in range(h + 1)]
field[0][0][abs(red[0][0] - blue[0][0])] = 1
for i in range(h):
	for j in range(w):
		for f in range(6401):
			if field[i][j][f] == 1:
				field[i][j + 1][f + abs(red[i][j + 1] - blue[i][j + 1])] = 1
				field[i][j + 1][abs(f - abs(red[i][j + 1] - blue[i][j + 1]))] = 1
				field[i + 1][j][f + abs(red[i + 1][j] - blue[i + 1][j])] = 1
				field[i + 1][j][abs(f - abs(red[i + 1][j] - blue[i + 1][j]))] = 1
for i in range(6500):
	if field[h - 1][w - 1][i] == 1:
		print(i)
		exit()


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)


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 = [set([])] + [set([0])] + [set([])] * (W - 1)
for h in range(H):
	dpo = dp
	dp = [set([])] + [None] * W
	for (w, d) in enumerate(D[W * h:W * (h + 1)]):
		s = dp[w] | dpo[w + 1]
		dp[w + 1] = {v + d for v in s} | {abs(v - d) for v in s}
print(min(dp[-1]))


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


import sys
input = sys.stdin.readline
(H, W) = map(int, input().split())
diff = [list(map(int, input().split())) for _ in range(H)]
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 = [[False] * W for _ in range(H)]
ofs = 80 * (H + W)
dp[0][0] = 1 << ofs + diff[0][0] | 1 >> ofs - diff[0][0]
for i in range(H):
	for j in range(W):
		if i > 0:
			dp[i][j] |= dp[i - 1][j] << diff[i][j] | dp[i - 1][j] >> diff[i][j]
		if j > 0:
			dp[i][j] |= dp[i][j - 1] << diff[i][j] | dp[i][j - 1] >> diff[i][j]
for k in range(ofs):
	if dp[-1][-1] >> k + ofs & 1:
		print(k)
		break


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


(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 = [[a[i][j] - b[i][j] for j in range(w)] for i in range(h)]
dp = [w * [0] for _ in range(h)]
dp[0][0] = 1 << 80 + d[0][0] | 1 << 80 - d[0][0]
for i in range(h):
	for j in range(w):
		if i != 0:
			dp[i][j] |= dp[i - 1][j] << 80 + d[i][j]
			dp[i][j] |= dp[i - 1][j] << 80 - d[i][j]
		if j != 0:
			dp[i][j] |= dp[i][j - 1] << 80 + d[i][j]
			dp[i][j] |= dp[i][j - 1] << 80 - d[i][j]
n = dp[-1][-1] | 1 << 15000
ans = 1 << 1000
for i in range(15000):
	if n & 1 << i:
		ans = min(ans, abs(i - (h + w - 1) * 80))
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


import math
(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)]
diff = [[abs(x - y) for (x, y) in zip(ar, br)] for (ar, br) in zip(a, b)]
M = 80 * (h + w)
dp = [[0 for j in range(w)] for i in range(h)]
dp[0][0] = 1 << diff[0][0] + M
for i in range(h):
	for j in range(w):
		if i > 0:
			dp[i][j] |= dp[i - 1][j] << diff[i][j] | dp[i - 1][j] >> diff[i][j]
		if j > 0:
			dp[i][j] |= dp[i][j - 1] << diff[i][j] | dp[i][j - 1] >> diff[i][j]
		dp[i][j] &= (1 << M * 2) - 1
for k in range(M):
	if dp[h - 1][w - 1] >> k + M & 1 == 1:
		print(k)
		break


(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) = map(int, input().split())
A = [[0] * w for i in range(h)]
for i in range(h):
	A[i] = list(map(int, input().split()))
B = [[0] * w for i in range(h)]
for i in range(h):
	B[i] = list(map(int, input().split()))
C = [[0] * w for i in range(h)]
for i in range(h):
	for j in range(w):
		C[i][j] = abs(A[i][j] - B[i][j])
dp = [[[0] * 6401 for j in range(h + 1)] for i in range(h + w + 1)]
dp[0][0][C[0][0]] = 1
for i in range(h + w):
	for j in range(max(0, i - w), min(i, h) + 1):
		for k in range(6401):
			if dp[i][j][k] == 1:
				if i - j < w - 1:
					if k + C[j][i - j + 1] < 6400:
						dp[i + 1][j][k + C[j][i - j + 1]] = 1
					dp[i + 1][j][abs(k - C[j][i - j + 1])] = 1
				if j < h - 1:
					if k + C[j + 1][i - j] < 6400:
						dp[i + 1][j + 1][k + C[j + 1][i - j]] = 1
					dp[i + 1][j + 1][abs(k - C[j + 1][i - j])] = 1
for k in range(6400):
	if dp[h + w - 2][h - 1][k] == 1:
		print(k)
		exit()


(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 = [[0] * w for i in range(h)]
for i in range(h):
	for j in range(w):
		C[i][j] = abs(A[i][j] - B[i][j])
dp = [[0] * w for i in range(h)]
m = 80
dp[0][0] = 1 << m + C[0][0] | 1 << m - C[0][0]
for i in range(h):
	for j in range(w):
		if i >= 1:
			dp[i][j] |= dp[i - 1][j] << m + C[i][j] | dp[i - 1][j] << m - C[i][j]
		if j >= 1:
			dp[i][j] |= dp[i][j - 1] << m + C[i][j] | dp[i][j - 1] << m - C[i][j]
b = list(bin(dp[h - 1][w - 1]))[::-1]
ans = float('inf')
for i in range(len(b)):
	if b[i] == '1':
		ans = min(ans, abs(m * (h + w - 1) - i))
print(ans)


(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


(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)]
d = [[0 for j in range(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])
dp = [[0 for _ in range(6400)] for _ in range(h * w)]
dp[0][d[0][0]] = 1
for i in range(h * w):
	(nowh, noww) = divmod(i, w)
	for k in range(6400):
		if not dp[nowh * w + noww][k] == 1:
			continue
		(nxth, nxtw) = (nowh + 1, noww + 1)
		if nxtw < w:
			if k + d[nowh][nxtw] < 6400:
				dp[nowh * w + nxtw][k + d[nowh][nxtw]] = 1
			dp[nowh * w + nxtw][abs(k - d[nowh][nxtw])] = 1
		if nxth < h:
			if k + d[nxth][noww] < 6400:
				dp[nxth * w + noww][k + d[nxth][noww]] = 1
			dp[nxth * w + noww][abs(k - d[nxth][noww])] = 1
for k in range(6400):
	if dp[-1][k] == 1:
		print(k)
		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)]
C = [[0 for j in range(W)] for i in range(H)]
for i in range(H):
	for j in range(W):
		C[i][j] = abs(A[i][j] - B[i][j])
dp = [[0 for k in range(6400)] for g in range(H * W)]
dp[0][C[0][0]] = 1
for g in range(H * W):
	(i, j) = divmod(g, W)
	for k in range(6400):
		if not dp[i * W + j][k]:
			continue
		(ni, nj) = (i + 1, j + 1)
		if ni < H:
			if k + C[ni][j] < 6400:
				dp[ni * W + j][k + C[ni][j]] = 1
			dp[ni * W + j][abs(k - C[ni][j])] = 1
		if nj < W:
			if k + C[i][nj] < 6400:
				dp[i * W + nj][k + C[i][nj]] = 1
			dp[i * W + nj][abs(k - C[i][nj])] = 1
for k in range(6400):
	if dp[-1][k]:
		print(k)
		break


(h, w) = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(2 * h)]
board = [[0] * w for _ in range(h)]
for i in range(h):
	for j in range(w):
		board[i][j] += arr[i][j]
for i in range(h):
	for j in range(w):
		board[i][j] = abs(board[i][j] - arr[h + i][j])
dp = [[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add(board[0][0])
for i in range(h):
	for j in range(w):
		if i != 0:
			for k in dp[i - 1][j]:
				dp[i][j].add((k + board[i][j]) % 640)
				dp[i][j].add(abs(k - board[i][j]) % 640)
		if j != 0:
			for k in dp[i][j - 1]:
				dp[i][j].add((k + board[i][j]) % 640)
				dp[i][j].add(abs(k - board[i][j]) % 640)
print(min(dp[h - 1][w - 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


import sys
input = sys.stdin.readline
(h, w) = 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 = 64000
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()


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


(H, W) = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(H)]
B = [[int(b) for b in input().split()] for _ in range(H)]
C = [[0] * W for _ in range(H)]
num = 80
for i in range(H):
	for j in range(W):
		C[i][j] = abs(A[i][j] - B[i][j])
dp = [[0] * W for _ in range(H)]
dp[0][0] |= 1 << num + C[0][0] | 1 << num - C[0][0]
for i in range(H):
	for j in range(W):
		if i > 0:
			dp[i][j] |= dp[i - 1][j] << num + C[i][j] | dp[i - 1][j] << num - C[i][j]
		if j > 0:
			dp[i][j] |= dp[i][j - 1] << num + C[i][j] | dp[i][j - 1] << num - C[i][j]
b = list(bin(dp[-1][-1]))[::-1]
ans = 10 ** 9
for i in range(len(b)):
	if b[i] == '1':
		ans = min(ans, abs(num * (H + W - 1) - i))
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())


import numpy as np
(h, w) = map(int, input().split())
A = [0] * h
B = [0] * h
for _ in range(h):
	A[_] = list(map(int, input().split()))
for _ in range(h):
	B[_] = list(map(int, input().split()))
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])
dp = [[0] * w for _ in range(h)]
D = 80 * (h + w)
L = 2 * D + 1
dp[0][0] = np.zeros(L, np.bool)
dp[0][0][D + C[0][0]] = 1
for i in range(h):
	for j in range(w):
		if i == 0 and j == 0:
			continue
		x = np.zeros(L, np.bool)
		if i != 0:
			x[C[i][j]:] |= dp[i - 1][j][:L - C[i][j]]
			x[:L - C[i][j]] |= dp[i - 1][j][C[i][j]:]
		if j != 0:
			x[C[i][j]:] |= dp[i][j - 1][:L - C[i][j]]
			x[:L - C[i][j]] |= dp[i][j - 1][C[i][j]:]
		dp[i][j] = x
temp = np.where(dp[-1][-1] > 0)[0] - D
temp = np.abs(temp)
print(np.min(temp))


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


def ints():
	return [int(x) for x in input().split()]

def readi():
	return int(input())
(H, W) = ints()
A = [ints() for _ in range(H)]
B = [ints() for _ in range(H)]
D = [[abs(A[h][w] - B[h][w]) for w in range(W)] for h in range(H)]
memo = {}
memo = {}
N = 80 * 60
memo[0, 0] = 1 << D[0][0] + N
for h in range(H):
	for w in range(W):
		if h == 0 and w == 0:
			continue
		d = D[h][w]
		f = 0
		if h > 0:
			s = memo[h - 1, w]
			f |= s << d
			f |= s >> d
		if w > 0:
			s = memo[h, w - 1]
			f |= s << d
			f |= s >> d
		memo[h, w] = f

def min_keta(n):
	b = bin(n >> N)
	for i in range(len(b)):
		if b[-i - 1] == '1':
			return i
print(min_keta(memo[H - 1, W - 1]))


(h, w) = map(int, input().split())
a = []
b = []
for _ in range(h):
	a.append(list(map(int, input().split())))
for _ in range(h):
	b.append(list(map(int, input().split())))
grid = [[0 for _ in range(w)] for _ 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 _ in range(80 * (h + w) + 1)] for _ in range(h + w)]
dp[0][0] = 1
for ij in range(h + w - 1):
	for i in range(h):
		j = ij - i
		if j < 0 or j >= w:
			continue
		c = grid[i][j]
		for k in range(80 * (h + w - 1) + 1):
			if dp[ij][k] == 1:
				dp[ij + 1][k + c] = 1
				dp[ij + 1][abs(k - c)] = 1
for i in range(100):
	if dp[-1][i] == 1:
		print(i)
		exit()


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 numpy as np
(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)]
X = (H + W) * 80
L = X + X + 1
dp = [[0] * W for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0] = np.zeros(L, np.bool)
dp[0][0][X + d] = 1
for h in range(H):
	for w in range(W):
		a = A[h][w]
		b = B[h][w]
		if h == w == 0:
			continue
		d = abs(a - b)
		x = np.zeros(L, np.bool)
		if h != 0:
			x[d:] |= dp[h - 1][w][:L - d]
			x[:L - d] |= dp[h - 1][w][d:]
		if w != 0:
			x[d:] |= dp[h][w - 1][:L - d]
			x[:L - d] |= dp[h][w - 1][d:]
		dp[h][w] = x
dp = dp[-1][-1]
can_make = np.where(dp)[0] - X
answer = np.abs(can_make).min()
print(answer)


(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] * (13000 * (w + 1)) for _ in range(h + 1)]
dp[1][0] = 1
dp[0][13000] = 1
for i in range(1, h + 1):
	for j in range(1, w + 1):
		x = c[i - 1][j - 1]
		for k in range(13000):
			y = 0
			if k + x <= 12999:
				dp[i][13000 * j + k] |= dp[i - 1][13000 * j + k + x] | dp[i][13000 * (j - 1) + k + x]
			dp[i][13000 * j + k] |= dp[i - 1][13000 * j + abs(k - x)] | dp[i][13000 * (j - 1) + abs(k - x)]
for i in range(13000):
	if dp[h][13000 * w + i]:
		ans = i
		print(ans)
		exit()


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)


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)]
	dp = [[[0] * (80 * (h + w + 1) + 1) for i in range(w)] for j in range(h)]
	dp[0][0][abs(a[0][0] - b[0][0])] = 1
	for i in range(h):
		for j in range(w):
			c = abs(a[i][j] - b[i][j])
			if i != 0 or j != 0:
				for k in range(80 * (i + j + 1) + 1):
					dp[i][j][k] = dp[i - 1][j][abs(k - c)] | dp[i - 1][j][k + c] | dp[i][j - 1][abs(k - c)] | dp[i][j - 1][k + c]
	for i in range(80 * (h + w + 1) + 1):
		if dp[h - 1][w - 1][i] == 1:
			print(i)
			break
main()


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 = [None] * H
for i in range(H):
	A[i] = list(map(int, input().split()))
B = [None] * H
for i in range(H):
	B[i] = list(map(int, input().split()))
C = [[None] * 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])
K = 80 * (H + W - 1) // 2 + 1
dp = [[[0] * (K + 1) for _ in range(W)] for _ in range(H)]
dp[0][0][C[0][0]] = 1
for i in range(H):
	for j in range(W):
		for k in range(K + 1):
			if i and dp[i - 1][j][k]:
				if k + C[i][j] < K:
					dp[i][j][k + C[i][j]] = 1
				dp[i][j][abs(k - C[i][j])] = 1
			if j and dp[i][j - 1][k]:
				if k + C[i][j] < K:
					dp[i][j][k + C[i][j]] = 1
				dp[i][j][abs(k - C[i][j])] = 1
for k in range(K + 1):
	res = dp[H - 1][W - 1][k]
	if res:
		print(k)
		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)]
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
input = sys.stdin.readline
(h, w) = map(int, input().split())
A = []
for i in range(h):
	a_list = list(map(int, input().split()))
	A.append(a_list)
B = []
for i in range(h):
	b_list = list(map(int, input().split()))
	B.append(b_list)
dp = []
for i in range(w):
	sub_list = [False] * 12802
	dp.append(sub_list)
dp[0][0] = True
for i in range(h):
	for j in range(w):
		c = A[i][j] - B[i][j]
		l = [False] * 12802
		for x in range(12802):
			if dp[j][x]:
				l[abs(x + c)] = True
				l[abs(x - c)] = True
				if j < w - 1:
					dp[j + 1][abs(x + c)] = True
					dp[j + 1][abs(x - c)] = True
		dp[j] = l
ans = 20000
for x in range(12802):
	if dp[w - 1][x]:
		ans = min(ans, x)
print(ans)


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
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
(h, w) = [int(i) for i in readline().split()]
a = [[int(i) for i in readline().split()] for _ in range(h)]
b = [[int(i) for i in readline().split()] for _ in range(h)]
dp = [[0] for _ in range(w)]
dp[0][0] = 1
N = 6401
for i in range(h):
	ndp = [[0] * N for _ in range(w)]
	for j in range(w):
		v = abs(a[i][j] - b[i][j])
		for (ii, c) in enumerate(dp[j]):
			if ii + v < N:
				ndp[j][abs(ii + v)] |= c
			ndp[j][abs(ii - v)] |= c
		if j:
			for (ii, c) in enumerate(ndp[j - 1]):
				if ii + v < N:
					ndp[j][abs(ii + v)] |= c
				ndp[j][abs(ii - v)] |= c
	dp = ndp
c = [i for (i, c) in enumerate(dp[j]) if c]
print(min(c))


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 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(map(lambda s: s == '1', list(bin(dp[w - 1])[-ofs - 1:-ofs + 100])))
	for (i, s) in enumerate(b):
		if s:
			print(i)
			return
solve()


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


import sys
input = sys.stdin.readline

def main():
	(H, W) = map(int, input().split())
	P = W + 1
	A = [list(map(int, input().split())) for _ in range(H)]
	B = [list(map(int, input().split())) for _ in range(H)]
	L = 80 * 80
	dp = [[False] * (L + 1) for _ in range((W + 1) * (H + 1))]
	dp[P + 1][abs(A[0][0] - B[0][0])] = True
	for h in range(H):
		for w in range(W):
			if h == 0 and w == 0:
				continue
			c = abs(A[h][w] - B[h][w])
			for l in range(L - c + 1):
				b1 = abs(l - c)
				b2 = abs(l + c)
				dp[(h + 1) * P + w + 1][l] = dp[h * P + w + 1][b1] or dp[(h + 1) * P + w][b1] or dp[h * P + w + 1][b2] or dp[(h + 1) * P + w][b2]
	for l in range(L + 1):
		if dp[H * P + W][l]:
			print(l)
			break
main()


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


(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)]
arr = [set() for _ in range(W)]
arr[0].add(A[0][0] - B[0][0])
for x in range(1, W):
	s = arr[x]
	d = A[0][x] - B[0][x]
	for n in arr[x - 1]:
		s.add(abs(n + d))
		s.add(abs(n - d))
for y in range(1, H):
	nextarr = [set() for _ in range(W)]
	Ay = A[y]
	By = B[y]
	dh = H - y - 1
	for x in range(W):
		nextset = nextarr[x]
		d = Ay[x] - By[x]
		dw = W - x - 1
		lim = (dh + dw + 1) * 160
		if x >= 1:
			arr[x] |= nextarr[x - 1]
		for n in arr[x]:
			if abs(n + d) <= lim:
				nextset.add(abs(n + d))
			if abs(n - d) <= lim:
				nextset.add(abs(n - d))
	arr = nextarr
print(min([abs(d) for d in arr[-1]]))


f = lambda : map(int, input().split())
(H, W) = f()
A = [[*f()] for _ in [0] * H]
B = [[*f()] for _ in [0] * H]
G = [[set() for i in [0] * W] for j in [0] * H]
G[0][0].add(0)
for h in range(H):
	for w in range(W):
		d = A[h][w] - B[h][w]
		for a in G[h][w]:
			if h != H - 1:
				G[h + 1][w].add(abs(a + d))
				G[h + 1][w].add(abs(a - d))
			if w != W - 1:
				G[h][w + 1].add(abs(a + d))
				G[h][w + 1].add(abs(a - d))
res = set()
d = A[H - 1][W - 1] - B[H - 1][W - 1]
for a in G[H - 1][W - 1]:
	res.add(abs(a + d))
	res.add(abs(a - d))
print(min(res))


(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)]
d = [[0] * W for j in range(H)]
m = 80 * (H + W)
d[0][0] = 1 << m
for h in range(H):
	for w in range(W):
		x = abs(A[h][w] - B[h][w])
		if h > 0:
			d[h][w] |= d[h - 1][w]
		if w > 0:
			d[h][w] |= d[h][w - 1]
		d[h][w] = d[h][w] << x | d[h][w] >> x
(x, y) = (1 << m, 1 << m)
for i in range(99):
	if d[H - 1][W - 1] & x or d[H - 1][W - 1] & y:
		print(i)
		exit()
	x <<= 1
	y >>= 1


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)


import sys, queue, math, copy, itertools, bisect, collections, heapq

def main():
	LI = lambda : [int(x) for x in sys.stdin.readline().split()]
	(H, W) = LI()
	A = [LI() for _ in range(H)]
	B = [LI() for _ in range(H)]
	C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
	D = [[set() for _ in range(W + 1)] for _ in range(H + 1)]
	D[0][1] = {0}
	for i in range(1, H + 1):
		for j in range(1, W + 1):
			for k in D[i - 1][j] | D[i][j - 1]:
				D[i][j].add(abs(k + C[i - 1][j - 1]))
				D[i][j].add(abs(k - C[i - 1][j - 1]))
	print(min(D[-1][-1]))
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 = [[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()


(H, W) = map(int, input().split())
maxK = 80 * 80 + 1
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 for _ in range(maxK + 1)] for _ in range(W + 1)] for _ in range(H + 1)]
dp[1][1][C[0][0]] = 1
for i in range(1, H + 1):
	for j in range(1, W + 1):
		for k in range(maxK):
			if dp[i - 1][j][k] == 1 or dp[i][j - 1][k] == 1:
				(k_candidate1, k_candidate2) = (abs(k - C[i - 1][j - 1]), abs(k + C[i - 1][j - 1]))
				if k_candidate1 < maxK:
					dp[i][j][k_candidate1] = 1
				if k_candidate2 < maxK:
					dp[i][j][k_candidate2] = 1
for k in range(maxK):
	if dp[H][W][k] == 1:
		print(k)
		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)]
AB = [list(map(lambda x, y: abs(x - y), a, b)) for (a, b) in zip(A, B)]
dp = [[[0] * 12801 for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][0] = 1
for h in range(H):
	for w in range(W):
		ab = AB[h][w]
		for i in range(10000):
			if dp[h][w][i] == 1:
				dp[h + 1][w][i + ab] = 1
				dp[h + 1][w][abs(i - ab)] = 1
				dp[h][w + 1][i + ab] = 1
				dp[h][w + 1][abs(i - ab)] = 1
for i in range(1000):
	if dp[H][W - 1][i] == 1:
		print(i)
		break


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())
AB = []
max_ab = 0
for i in range(H):
	AB.append(list(map(int, input().split())))
	max_ab = max(max_ab, max(AB[-1]))
for i in range(H):
	AB[i] = [abs(AB[i][j] - int(b)) for (j, b) in enumerate(input().split())]
base = max_ab * (H + W)
dp = [[1 << base for w in range(W)] for h in range(H)]
dp[0][0] = 1 << base + AB[0][0] | 1 << base - AB[0][0]
for h in range(1, H):
	dp[h][0] = dp[h - 1][0] << AB[h][0] | dp[h - 1][0] >> AB[h][0]
for w in range(1, W):
	dp[0][w] = dp[0][w - 1] << AB[0][w] | dp[0][w - 1] >> AB[0][w]
for h in range(1, H):
	for w in range(1, W):
		dp[h][w] = dp[h - 1][w] << AB[h][w] | dp[h - 1][w] >> AB[h][w] | dp[h][w - 1] << AB[h][w] | dp[h][w - 1] >> AB[h][w]
positive = bin(dp[H - 1][W - 1] >> base)
print(positive[::-1].find('1'))


import sys
import numpy as np
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
(H, W) = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B).tolist()
X = (H + W) * 80
L = X + X + 1
dp = [[np.zeros(L, np.bool) for _ in range(W)] for _ in range(H)]
di = diff[0][0]
dp[0][0][X + di] = 1
dp[0][0][X - di] = 1
for i in range(H):
	for j in range(W):
		if i == 0 and j == 0:
			continue
		di = diff[i][j]
		if i > 0:
			prev = dp[i - 1][j]
			dp[i][j][di:] |= prev[:L - di]
			dp[i][j][:L - di] |= prev[di:]
		if j > 0:
			prev = dp[i][j - 1]
			dp[i][j][di:] |= prev[:L - di]
			dp[i][j][:L - di] |= prev[di:]
dp = dp[-1][-1][X:]
possible = np.nonzero(dp)[0]
answer = possible.min()
print(answer)


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


(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 = [[A[i][j] - B[i][j] for j in range(W)] for i in range(H)]
X = [[0] * W for _ in range(H)]
X[0][0] = 1 << 80 + C[0][0] | 1 << 80 - C[0][0]
for i in range(H):
	for j in range(W):
		if i:
			X[i][j] |= X[i - 1][j] << 80 + C[i][j] | X[i - 1][j] << 80 - C[i][j]
		if j:
			X[i][j] |= X[i][j - 1] << 80 + C[i][j] | X[i][j - 1] << 80 - C[i][j]
s = bin(X[-1][-1] + (1 << 13000))
mi = 1 << 100
for i in range(len(s)):
	if s[-i - 1] == '1':
		mi = min(mi, abs(i - (H + W - 1) * 80))
print(mi)


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


import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

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)]
	dp = [[set() for _ in range(W)] for _ in range(H)]
	dp[0][0] = {abs(A[0][0] - B[0][0])}
	for h in range(H):
		for w in range(W):
			(a, b) = (A[h][w], B[h][w])
			if w - 1 >= 0:
				for num in dp[h][w - 1]:
					dp[h][w].add(abs(num + a - b))
					dp[h][w].add(abs(-num + a - b))
			if h - 1 >= 0:
				for num in dp[h - 1][w]:
					dp[h][w].add(abs(num + a - b))
					dp[h][w].add(abs(-num + a - b))
	res = f_inf
	for num in dp[-1][-1]:
		res = min(res, abs(num))
	print(res)
resolve()


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)]
assert all((len(row) == W for row in A))
assert all((len(row) == W for row in B))
table = [[set() for j in range(W)] for i in range(H)]
table[0][0].add(abs(A[0][0] - B[0][0]))
for i in range(H):
	if i >= 2:
		table[i - 2] = None
	for j in range(W):
		prev = table[i - 1][j] if i > 0 else set()
		prev |= table[i][j - 1] if j > 0 else set()
		for pdiff in prev:
			diff = A[i][j] - B[i][j]
			table[i][j].add(abs(pdiff + diff))
			table[i][j].add(abs(pdiff - diff))
m = min((diff for diff in table[H - 1][W - 1]))
print(m)


(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 numpy as np
(H, W) = list(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
L = X + X + 1
dp = [[0] * W for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0] = np.zeros(L, np.bool)
dp[0][0][X + d] = 1
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 = np.zeros(L, np.bool)
		if h != 0:
			x[d:] |= dp[h - 1][w][:L - d]
			x[:L - d] |= dp[h - 1][w][d:]
		if w != 0:
			x[d:] |= dp[h][w - 1][:L - d]
			x[:L - d] |= dp[h][w - 1][d:]
		dp[h][w] = x
dp2 = dp[-1][-1]
can_make = np.where(dp2)[0] - X
answer = np.abs(can_make).min()
print(answer)


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


(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)]
M = (H + W) * 80
dp = [[0] * W for _ in range(H)]
dp[0][0] |= 1 << M - C[0][0]
for i in range(H):
	for j in range(W):
		if i == j == 0:
			continue
		bit = 0
		if i > 0:
			bit |= dp[i - 1][j]
		if j > 0:
			bit |= dp[i][j - 1]
		dp[i][j] = bit >> C[i][j] | bit << C[i][j]
ans = M
for i in range(M + 1):
	if dp[-1][-1] >> i & 1:
		ans = min(ans, abs(M - i))
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)]
for i in range(h):
	for j in range(w):
		a[i][j] = abs(a[i][j] - b[i][j])
base = 2 ** 12800 - 1
dp = [[0] * (w + 1) for _ in range(h + 1)]
dp[1][1] = 2 ** 6400 >> a[0][0] | 2 ** 6400 << a[0][0] & base
for i in range(1, h + 1):
	for j in range(1, w + 1):
		if (i == 1) & (j == 1):
			dp[1][1] = 2 ** 6400 >> a[0][0] | 2 ** 6400 << a[0][0] & base
			continue
		dp[i][j] = (dp[i - 1][j] >> a[i - 1][j - 1] | dp[i - 1][j] << a[i - 1][j - 1] | dp[i][j - 1] >> a[i - 1][j - 1] | dp[i][j - 1] << a[i - 1][j - 1]) & base
ans = 6400
ans_bit = dp[-1][-1]
for i in range(6300, 6500):
	if ans_bit >> i & 1 == 1:
		ans = min(ans, abs(6400 - i))
print(ans)


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)


def main():
	(H, W) = map(int, input().split())
	N = 80 * (H + W) // 2
	A = [list(map(int, input().split())) for _ in range(H)]
	B = [list(map(int, input().split())) for _ in range(H)]
	dp = [[set() for _ in range(W + 1)] for _ in range(2)]
	dp[0][1].add(0)

	def ltN(x):
		return x <= N
	for i in range(H):
		dp[i + 1 & 1] = [set() for _ in range(W + 1)]
		for j in range(W):
			diff = abs(A[i][j] - B[i][j])
			tmp = set()
			for x in dp[i & 1][j + 1]:
				tmp.add(abs(x + diff))
				tmp.add(abs(x - diff))
			for x in dp[i + 1 & 1][j]:
				tmp.add(abs(x + diff))
				tmp.add(abs(x - diff))
			dp[i + 1 & 1][j + 1] = tmp
	print(min(dp[H & 1][W]))
main()


(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 time
start = time.time()
(H, W) = [int(s) for s in input().split()]
inls = [[int(s) for s in input().split()] for _ in range(H)]
for i in range(H):
	x = [int(s) for s in input().split()]
	for j in range(W):
		inls[i][j] = abs(inls[i][j] - x[j])
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(inls[0][0])
for i in range(H):
	for j in range(W):
		x = 80 * (H + W - i - j)
		for e in dp[i - 1][j]:
			if e + inls[i][j] < x:
				dp[i][j].add(e + inls[i][j])
			dp[i][j].add(abs(e - inls[i][j]))
		for e in dp[i][j - 1]:
			if e + inls[i][j] < x:
				dp[i][j].add(e + inls[i][j])
			dp[i][j].add(abs(e - inls[i][j]))
	if time.time() - start > 1.7:
		dp[H - 1][W - 1].add(0)
		break
print(min(dp[H - 1][W - 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()


import sys
input = sys.stdin.readline
MI = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
(H, W) = MI()
A = [LI() for _ in [0] * H]
B = [LI() for _ in [0] * H]
G = [[set() for i in range(W)] for j in range(H)]
G[0][0].add(0)
for h in range(H):
	for w in range(W):
		d = A[h][w] - B[h][w]
		for a in G[h][w]:
			if h != H - 1:
				G[h + 1][w].add(abs(a + d))
				G[h + 1][w].add(abs(a - d))
			if w != W - 1:
				G[h][w + 1].add(abs(a + d))
				G[h][w + 1].add(abs(a - d))
res = set()
d = A[H - 1][W - 1] - B[H - 1][W - 1]
for a in G[H - 1][W - 1]:
	res.add(abs(a + d))
	res.add(abs(a - d))
res = sorted(list(res))
print(min(res))


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)


import numpy as np
import itertools
(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)]
D = [[abs(A[h][w] - B[h][w]) for w in range(W)] for h in range(H)]
max_num = 80 * (H + W)
table = np.zeros((H, W, max_num), dtype='bool')
table[0][0][D[0][0]] = True
it = itertools.product(list(range(H)), list(range(W)))
next(it)
for (h, w) in it:
	d = D[h][w]
	table[h][w][d:] += table[h - 1][w][:max_num - d] + table[h][w - 1][:max_num - d]
	table[h][w][:max_num - d] += table[h - 1][w][d:] + table[h][w - 1][d:]
	table[h][w][d:0:-1] += table[h - 1][w][:d] + table[h][w - 1][:d]
print(np.amin(*np.where(table[H - 1][W - 1])))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
(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 * 80 + 2
DP = [[[0] * ma for i in range(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 == 0 and j == 0:
			DP[i][j][k] = 1
			continue
		else:
			up = min((i + j + 1) * 80 + 1, ma - k)
			for l in range(up):
				DP[i][j][l] = DP[i][j - 1][abs(l - k)] | DP[i][j - 1][l + k] | DP[i - 1][j][abs(l - k)] | DP[i - 1][j][l + k]
for i in range(ma):
	if DP[-1][-1][i]:
		print(i)
		exit()


(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 = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(0)
for i in range(W + H - 1):
	(I, J) = (0, 0)
	if i < H:
		I = i
		J = 0
	else:
		I = H - 1
		J = i - H + 1
	while I >= 0 and J < W:
		sa = abs(A[I][J] - B[I][J])
		for point in dp[I][J]:
			if I != H - 1:
				dp[I + 1][J].add(point + sa)
				dp[I + 1][J].add(abs(point - sa))
			if J != W - 1:
				dp[I][J + 1].add(point + sa)
				dp[I][J + 1].add(abs(point - sa))
		I -= 1
		J += 1
ans = dp[H - 1][W - 1]
sa = abs(A[H - 1][W - 1] - B[H - 1][W - 1])
res = float('inf')
for p in ans:
	res = min(res, abs(p + sa), abs(p - sa))
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)]
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)


(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[h][w] - B[h][w]) for w in range(W)] for h in range(H)]

def cal():
	dp1 = [set() for _ in range(W)]
	dp2 = [set() for _ in range(W)]
	for h in range(H):
		for w in range(W):
			d = D[h][w]
			if h == 0 and w == 0:
				dp2[0].add(d)
			for v in dp1[w]:
				v1 = v + d
				v2 = v - d if v >= d else -v + d
				dp2[w].add(v1)
				dp2[w].add(v2)
			if 0 < w:
				for v in dp2[w - 1]:
					v1 = v + d
					v2 = v - d if v >= d else -v + d
					dp2[w].add(v1)
					dp2[w].add(v2)
		dp1 = dp2
		dp2 = [set() for _ in range(W)]
	return min(dp1[W - 1])
ans = cal()
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])))


import numpy as np

def main():
	(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)]
	n = 80 * (80 + 80) + 10
	m = n // 2
	dp = [[None] * w for i in range(h)]
	for i in range(h):
		for j in range(w):
			dp[i][j] = np.zeros(n, np.bool)
	dp[0][0][m + abs(a[0][0] - b[0][0])] = True
	dp[0][0][m - abs(a[0][0] - b[0][0])] = True
	for i in range(h):
		for j in range(w):
			x = abs(a[i][j] - b[i][j])
			if 0 < i and 0 < h:
				dp[i][j][x:] |= dp[i - 1][j][:n - x]
				dp[i][j][:n - x] |= dp[i - 1][j][x:]
				dp[i][j][x:] |= dp[i][j - 1][:n - x]
				dp[i][j][:n - x] |= dp[i][j - 1][x:]
			elif 0 < i:
				dp[i][j][x:] |= dp[i - 1][j][:n - x]
				dp[i][j][:n - x] |= dp[i - 1][j][x:]
			elif 0 < j:
				dp[i][j][x:] |= dp[i][j - 1][:n - x]
				dp[i][j][:n - x] |= dp[i][j - 1][x:]
	ans = float('inf')
	for i in range(m - 80 - 1, m + 80 + 2):
		if dp[h - 1][w - 1][i]:
			ans = min(ans, abs(m - i))
	print(ans)

def __starting_point():
	main()
__starting_point()


(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 x():
	return map(int, input().split())
v = list
c = abs
r = range
(h, w) = x()
p = v(r(h))
a = [v(x()) for i in p]
b = [v(x()) for i in p]
m = 13120
D = [[[0] * m for i in r(w)] for i in p]
for i in p:
	for j in r(w):
		k = c(a[i][j] - b[i][j])
		if i | j:
			for l in r((i + j + 1) * 80 + 1):
				e = c(l - k)
				D[i][j][l] = D[i][j - 1][e] | D[i][j - 1][l + k] | D[i - 1][j][e] | D[i - 1][j][l + k]
		else:
			D[i][j][k] = 1
print(D[-1][-1].index(1))


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


import sys
import math
import bisect
import heapq

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

def main():
	(H, W) = map(int, input().split())
	A = [[0] * W for _ in range(H)]
	for i in range(H):
		a = list(map(int, input().split()))
		A[i] = a
	for i in range(H):
		b = list(map(int, input().split()))
		for j in range(W):
			A[i][j] -= b[j]
	dp = [[set() for i in range(W)] for i in range(H)]
	dp[0][0].add(A[0][0])
	for i in range(H):
		for j in range(W):
			if i != 0:
				for k in dp[i - 1][j]:
					dp[i][j].add(abs(k - A[i][j]))
					dp[i][j].add(abs(k + A[i][j]))
			if j != 0:
				for k in dp[i][j - 1]:
					dp[i][j].add(abs(k - A[i][j]))
					dp[i][j].add(abs(k + A[i][j]))
	print(min(dp[H - 1][W - 1]))
main()


import sys

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

def main():
	(H, W) = LI()
	A = [LI() for _ in range(H)]
	B = [LI() for _ in range(H)]
	D = [[abs(A[h][w] - B[h][w]) for w in range(W)] for h in range(H)]
	dp = [[0] * W for _ in range(H)]
	dp[0][0] = 1 << D[0][0] << 20000
	for h in range(H):
		for w in range(W):
			if h > 0:
				dp[h][w] |= dp[h - 1][w] << D[h][w]
				dp[h][w] |= dp[h - 1][w] >> D[h][w]
			if w > 0:
				dp[h][w] |= dp[h][w - 1] << D[h][w]
				dp[h][w] |= dp[h][w - 1] >> D[h][w]
	for i in range(81):
		bit = 1 << 20000 + i
		bit |= 1 << 20000 - i
		if dp[H - 1][W - 1] & bit:
			return i
print(main())


(h, w) = map(int, input().split())
alist = [list(map(int, input().split())) for i in range(h)]
blist = [list(map(int, input().split())) for i in range(h)]
dlist = [[0] * w for i in range(h)]
for i in range(h):
	for j in range(w):
		dlist[i][j] = abs(alist[i][j] - blist[i][j])
l = [[0] * w for i in range(h)]
l[0][0] = 1 << 80 + dlist[0][0] | 1 << 80 - dlist[0][0]
for i in range(h):
	for j in range(w):
		if i > 0:
			l[i][j] |= l[i - 1][j] << 80 + dlist[i][j]
			l[i][j] |= l[i - 1][j] << 80 - dlist[i][j]
		if j > 0:
			l[i][j] |= l[i][j - 1] << 80 + dlist[i][j]
			l[i][j] |= l[i][j - 1] << 80 - dlist[i][j]
dblist = list(bin(l[h - 1][w - 1]))[::-1]
m = 80 * (h + w - 1)
ans = m
for i in range(len(dblist)):
	if dblist[i] == '1':
		ans = min(ans, abs(m - i))
print(ans)


def main():
	(H, W) = [int(i) for i in 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)]
	M = (H + W) * 80 + 1
	u = [[False] * M for _ in range(W)]
	v = [[False] * M for _ in range(W)]
	F = [False] * M
	v[0][0] = True
	for h in range(H):
		(v, u) = (u, v)
		vl = F
		for w in range(W):
			(a, b) = (A[h][w], B[h][w])
			d = a - b if a > b else b - a
			uu = u[w]
			vv = v[w]
			for i in range(M):
				di = i - d if i > d else d - i
				vv[i] = i + d < M and (uu[i + d] or vl[i + d]) or (uu[di] or vl[di])
			vl = vv
	va = v[-1]
	for i in range(M):
		if va[i]:
			print(i)
			return
	raise
main()


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


def solve():
	from sys import stdin
	f_i = stdin
	(H, W) = map(int, f_i.readline().split())
	A = tuple((tuple(map(int, f_i.readline().split())) for i in range(H)))
	B = tuple((tuple(map(int, f_i.readline().split())) for i in range(H)))
	dp1 = [0] * W
	bit_width = 80 * (H + W - 2)
	dp1[0] = 1 << bit_width
	for (A_i, B_i) in zip(A, B):
		dp2 = [0] * W
		diff = abs(A_i[0] - B_i[0])
		dp2[0] = dp1[0] << diff | dp1[0] >> diff
		for (j, t) in enumerate(zip(A_i[1:], B_i[1:]), start=1):
			(A_ij, B_ij) = t
			diff = abs(A_ij - B_ij)
			b = dp1[j] | dp2[j - 1]
			dp2[j] = b << diff | b >> diff
		dp1 = dp2
	b = bin(dp2[-1])
	b = b[-bit_width - 1:]
	print(b.index('1'))
solve()


import numpy as np
import itertools
(H, W) = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H * 2)]
M = [[abs(A[i][j] - A[H + i][j]) for j in range(W)] for i in range(H)]
m = max((max((j for j in i)) for i in M))
r = m * (H + W) + 1
dp = np.zeros((H, W, r), dtype='bool')
dp[0][0][M[0][0]] = True
it = itertools.product(range(H), range(W))
next(it)
for (h, w) in it:
	m = M[h][w]
	dp[h][w][m:] |= dp[h][w - 1][:r - m] | dp[h - 1][w][:r - m]
	dp[h][w][:r - m] |= dp[h][w - 1][m:] | dp[h - 1][w][m:]
	dp[h][w][:m] |= dp[h][w - 1][m:0:-1] | dp[h - 1][w][m:0:-1]
for i in range(r):
	if dp[-1][-1][i]:
		print(i)
		break


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 _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
dp = [[0 for _ in range(W)] for _ in range(H)]
SIZE = 20000
D = abs(A[0][0] - B[0][0])
dp[0][0] = 1 << SIZE - D | 1 << SIZE + D
for i in range(H):
	for j in range(W):
		D = abs(A[i][j] - B[i][j])
		dp[i][j] |= dp[i - 1][j] << D | dp[i - 1][j] >> D
		dp[i][j] |= dp[i][j - 1] << D | dp[i][j - 1] >> D
for i in range(SIZE):
	if dp[-1][-1] >> SIZE + i & 1:
		print(i)
		exit()


(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 = [list(map(int, input().split())) for i in range(h)]
b = [list(map(int, input().split())) for i in range(h)]
m = 13120
DP = [[[0] * m for i in range(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 == 0 and j == 0:
			DP[i][j][k] = 1
		else:
			for l in range((i + j + 1) * 80 + 1):
				DP[i][j][l] = DP[i][j - 1][abs(l - k)] | DP[i][j - 1][l + k] | DP[i - 1][j][abs(l - k)] | DP[i - 1][j][l + k]
print(DP[-1][-1].index(1))


(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) = map(int, input().split())
(A, B) = ([], [])
for i in range(H):
	X = list(map(int, input().split()))
	A.append(X)
for i in range(H):
	X = list(map(int, input().split()))
	B.append(X)
C = []
m = 0
for i in range(H):
	D = []
	for j in range(W):
		D.append(abs(A[i][j] - B[i][j]))
		m += abs(A[i][j] - B[i][j])
	C.append(D)
dp = [[set() for i in range(W)] for i in range(H)]
dp[0][0].add(C[0][0])
dp[0][0].add(-C[0][0])
for i in range(H):
	for j in range(W):
		if i != 0:
			for k in dp[i - 1][j]:
				dp[i][j].add(abs(k - C[i][j]))
				dp[i][j].add(abs(k + C[i][j]))
		if j != 0:
			for k in dp[i][j - 1]:
				dp[i][j].add(abs(k - C[i][j]))
				dp[i][j].add(abs(k + C[i][j]))
print(min(dp[-1][-1]))


import sys
import numpy as np
input = sys.stdin.readline
(h, w) = [int(j) for j in input().split()]
a = []
b = []
a = [[int(j) for j in input().split()] for i in range(h)]
b = [[int(j) for j in input().split()] for i in range(h)]
x = (h + w) * 80
l = 2 * x + 1
dp = [[0] * w for i in range(h)]
dp[0][0] = np.zeros(l, np.bool)
d = abs(a[0][0] - b[0][0])
dp[0][0][x + d] = 1
for i in range(h):
	for (j, (s, t)) in enumerate(zip(a[i], b[i])):
		if i == j == 0:
			continue
		d = abs(s - t)
		p = np.zeros(l, np.bool)
		if i != 0:
			p[d:] |= dp[i - 1][j][:l - d]
			p[:l - d] |= dp[i - 1][j][d:]
		if j != 0:
			p[d:] |= dp[i][j - 1][:l - d]
			p[:l - d] |= dp[i][j - 1][d:]
		dp[i][j] = p
ans = np.abs(np.where(dp[-1][-1])[0] - x).min()
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)]
dp = [w * [0] for _ in range(h)]
dp[0][0] = 1 << 80 + (a[0][0] - b[0][0]) | 1 << 80 + (b[0][0] - a[0][0])
for i in range(h):
	for j in range(w):
		if i != 0:
			dp[i][j] |= dp[i - 1][j] << 80 + (a[i][j] - b[i][j])
			dp[i][j] |= dp[i - 1][j] << 80 + (b[i][j] - a[i][j])
		if j != 0:
			dp[i][j] |= dp[i][j - 1] << 80 + (a[i][j] - b[i][j])
			dp[i][j] |= dp[i][j - 1] << 80 + (b[i][j] - a[i][j])
ans = 1 << 50000
for i in range(50000):
	if dp[h - 1][w - 1] & 1 << i:
		ans = min(abs(i - (h + w - 1) * 80), ans)
print(ans)


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


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)]
	C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
	M = 160 * 80
	dp = [[0 for _ in range(W)] for __ in range(H)]
	dp[0][0] = 1 << C[0][0] + M
	for i in range(H):
		for j in range(W):
			for (ni, nj) in [(i, j + 1), (i + 1, j)]:
				if ni >= H or nj >= W:
					continue
				t = C[ni][nj]
				dp[ni][nj] |= dp[i][j] << t
				dp[ni][nj] |= dp[i][j] >> t
	a = dp[H - 1][W - 1]
	ans = 0
	while True:
		if a & 1 << M + ans or a & 1 >> M - ans:
			break
		ans += 1
	print(ans)
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 = [[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
input = sys.stdin.readline
(h, w) = map(int, input().split())
a = []
for i in range(h):
	s = list(map(int, input().split()))
	a.append(s)
b = []
for i in range(h):
	s = list(map(int, input().split()))
	b.append(s)
dp = [[0] * w for i in range(h)]
t = 80 * (h + w)
dp[0][0] = 2 ** (t + abs(a[0][0] - b[0][0]))
for i in range(h):
	for j in range(w):
		r = abs(a[i][j] - b[i][j])
		if j > 0:
			dp[i][j] |= dp[i][j - 1] << r | dp[i][j - 1] >> r
		if i > 0:
			dp[i][j] |= dp[i - 1][j] << r | dp[i - 1][j] >> r
a = dp[h - 1][w - 1] >> t
m = a & -a
print(m.bit_length() - 1)


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)


def chk(a):
	L = []
	for i in range(13000):
		if a >> i & 1:
			L.append(i)
	return L
(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 = [[A[i][j] - B[i][j] for j in range(W)] for i in range(H)]
m = (1 << 12800) - 1
X = [[0] * W for _ in range(H)]
X[0][0] = 1 << 80 + C[0][0] | 1 << 80 - C[0][0]
for i in range(H):
	for j in range(W):
		if i:
			X[i][j] |= X[i - 1][j] << 80 + C[i][j] | X[i - 1][j] << 80 - C[i][j]
		if j:
			X[i][j] |= X[i][j - 1] << 80 + C[i][j] | X[i][j - 1] << 80 - C[i][j]
		X[i][j] &= m
s = bin(X[-1][-1] + (1 << 13000))
mi = 1 << 100
for i in range(len(s)):
	if s[-i - 1] == '1':
		mi = min(mi, abs(i - (H + W - 1) * 80))
print(mi)


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