import sys



read = sys.stdin.buffer.read

input = sys.stdin.buffer.readline

inputs = sys.stdin.buffer.readlines





# mod=10**9+7

# rstrip().decode('utf-8')

# map(int,input().split())

# import numpy as np



def main():

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

	

	G = [[0] * w for i in range(h)]

	A = [0]*h

	B = [0]*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()))

	

	for i in range(h):

		for j in range(w):

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



	M = (h + w) * 80



	li = [[[0] * M for i in range(w)] for j in range(h)]

	

	li[0][0][G[0][0]] = 1

	for i in range(h):

		for j in range(w):

			for k in range(M):

				

				if li[i][j][k] == 1:

					if i < h - 1:

						li[i + 1][j][abs(k - G[i+1][j])] = 1

						li[i + 1][j][abs(k + G[i+1][j])] = 1

					if j < w - 1:

						li[i][j + 1][abs(k - G[i][j+1])] = 1

						li[i][j + 1][abs(k + G[i][j+1])] = 1



	for i in range(M):

		if li[h-1][w - 1][i] == 1:

			print(i)

			exit(0)





if __name__ == "__main__":

	main()

import sys

#input = sys.stdin.readline

input = sys.stdin.buffer.readline



# mod=10**9+7

# rstrip().decode('utf-8')

# map(int,input().split())

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

	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)



if __name__ == "__main__":

	main()

import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines
# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np
def main():
    h, w = list(map(int, input().split()))
    G = [[0] * w for i in range(h)]
    A = [0] * h
    B = [0] * 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()))
    for i in range(h):
        for j in range(w):
            G[i][j] = abs(A[i][j] - B[i][j])
    M = (h + w) * 80
    li = [[[0] * M for i in range(w)] for j in range(h)]
    li[0][0][G[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(M):
                if li[i][j][k] == 1:
                    if i < h - 1:
                        li[i + 1][j][abs(k - G[i + 1][j])] = 1
                        li[i + 1][j][abs(k + G[i + 1][j])] = 1
                    if j < w - 1:
                        li[i][j + 1][abs(k - G[i][j + 1])] = 1
                        li[i][j + 1][abs(k + G[i][j + 1])] = 1
    for i in range(M):
        if li[h - 1][w - 1][i] == 1:
            print(i)
            exit(0)


if __name__ == "__main__":
    main()

import sys

# input = sys.stdin.readline
input = sys.stdin.buffer.readline
# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# 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)]
    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)


if __name__ == "__main__":
    main()

-read = sys.stdin.buffer.read +# input = sys.stdin.readline -inputs = sys.stdin.buffer.readlines -    G = [[0] * w for i in range(h)] -    A = [0] * h -    B = [0] * 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())) +    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)] -            G[i][j] = abs(A[i][j] - B[i][j]) -    M = (h + w) * 80 -    li = [[[0] * M for i in range(w)] for j in range(h)] -    li[0][0][G[0][0]] = 1 +            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 k in range(M): -                if li[i][j][k] == 1: -                    if i < h - 1: -                        li[i + 1][j][abs(k - G[i + 1][j])] = 1 -                        li[i + 1][j][abs(k + G[i + 1][j])] = 1 -                    if j < w - 1: -                        li[i][j + 1][abs(k - G[i][j + 1])] = 1 -                        li[i][j + 1][abs(k + G[i][j + 1])] = 1 -    for i in range(M): -        if li[h - 1][w - 1][i] == 1: -            print(i) -            exit(0) +            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)