import sys

input = sys.stdin.buffer.readline



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

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

import sys

input = sys.stdin.readline



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

	g = [list(abs(x - y) for x, y in zip(l1, l2)) for l1, l2 in zip(a, b)]

	poss = [0] * w

	poss[0] |= 1 << 6400 + g[0][0]

	poss[0] |= 1 << 6400 - g[0][0]

	for i in range(h):

		for j in range(w):

			if j < w - 1:

				d = g[i][j + 1]

				poss[j + 1] |= poss[j] << d

				poss[j + 1] |= poss[j] >> d

			if i < h - 1:

				d = g[i + 1][j]

				poss[j] = poss[j] << d | poss[j] >> d

	res = poss[-1]

	for i in range(6400):

		if res & (1 << 6400 + i) or res & (1 << 6400 - i):

			print(i)

			exit()



if __name__ == "__main__":

	main()
import sys

input = sys.stdin.buffer.readline
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)]
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()

import sys

input = sys.stdin.readline


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)]
    g = [list(abs(x - y) for x, y in zip(l1, l2)) for l1, l2 in zip(a, b)]
    poss = [0] * w
    poss[0] |= 1 << 6400 + g[0][0]
    poss[0] |= 1 << 6400 - g[0][0]
    for i in range(h):
        for j in range(w):
            if j < w - 1:
                d = g[i][j + 1]
                poss[j + 1] |= poss[j] << d
                poss[j + 1] |= poss[j] >> d
            if i < h - 1:
                d = g[i + 1][j]
                poss[j] = poss[j] << d | poss[j] >> d
    res = poss[-1]
    for i in range(6400):
        if res & (1 << 6400 + i) or res & (1 << 6400 - i):
            print(i)
            exit()


if __name__ == "__main__":
    main()

-input = sys.stdin.buffer.readline -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)] -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: +input = sys.stdin.readline + + +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)] +    g = [list(abs(x - y) for x, y in zip(l1, l2)) for l1, l2 in zip(a, b)] +    poss = [0] * w +    poss[0] |= 1 << 6400 + g[0][0] +    poss[0] |= 1 << 6400 - g[0][0] +    for i in range(h): -            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() +            if j < w - 1: +                d = g[i][j + 1] +                poss[j + 1] |= poss[j] << d +                poss[j + 1] |= poss[j] >> d +            if i < h - 1: +                d = g[i + 1][j] +                poss[j] = poss[j] << d | poss[j] >> d +    res = poss[-1] +    for i in range(6400): +        if res & (1 << 6400 + i) or res & (1 << 6400 - i): +            print(i) +            exit() + + +if __name__ == "__main__": +    main()