CodeEff/ECCO huggingface dataset:
# Original solution
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

h,w = ma()
A = []
for i in range(h):
    A.append(lma())
B= []
for i in range(h):
    B.append(lma())
D = [[abs(A[i][j]-B[i][j]) for j in range(w)] for i in range(h)]
mx = 0
for i in range(h):
    for j in range(w):
        mx = max(mx,D[i][j])
prev = [[False]*((h+w)*mx+1) for j in range(w)]
nex = [[False]*((h+w)*mx+1) for j in range(w)]
prev[0][0]=True
for i in range(h):
    for j in range(w):
        d = D[i][j]
        if j==0:
            for k in range((h+w)*mx+1-d):
                nex[j][k+d] = nex[j][k+d] or prev[j][k]
                nex[j][abs(k-d)] = nex[j][abs(k-d)] or prev[j][k]
        else:
            for k in range((h+w)*mx+1-d):
                nex[j][k+d] = nex[j][k+d] or prev[j][k] or nex[j-1][k]
                nex[j][abs(k-d)] = nex[j][abs(k-d)] or prev[j][k] or nex[j-1][k]
    prev=nex
    nex=[[False]*((h+w)*mx+1) for j in range(w)]
for i in range((h+w)*mx+1):
    if prev[w-1][i]:
        print(i)
        exit()


# Optimized solution
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

h,w = ma()
A = []
for i in range(h):
    A.append(lma())
B= []
for i in range(h):
    B.append(lma())
D = [[abs(A[i][j]-B[i][j]) for j in range(w)] for i in range(h)]
mx = 0
for i in range(h):
    for j in range(w):
        mx = max(mx,D[i][j])

le = (h+w)*mx
prev = [0]*w
nex = [0]*w
prev[0]=prev[0] | 2**le
for i in range(h):
    for j in range(w):
        d = D[i][j]
        if j==0:
            nex[j] = nex[j] | prev[j]>>d |prev[j]<<d
        else:
            nex[j] = nex[j] | prev[j]>>d |prev[j]<<d
            nex[j] = nex[j] | nex[j-1]>>d |nex[j-1]<<d
        #print(i,j,d,bin(nex[j]))
    prev=nex
    nex = [0]*w
for i in range(le+1):
    if (prev[w-1]>>le+i &1) or (prev[w-1]>>le-i &1):
        print(i)
        exit()
# Original solution
import sys
import math
import bisect
import heapq


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


def main():
    H, W = list(map(int, input().split()))
    M = 80
    A = [[0] * W for _ in range(H)]
    K = M * (H + W) + 1
    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 = [[[False] * K for i in range(W)] for i in range(H)]
    # dp[i][j][k]
    dp[0][0][abs(A[0][0])] = True

    for i in range(H):
        for j in range(W):
            if i != 0:
                for k in range(K):
                    if dp[i - 1][j][k] is True:
                        dp[i][j][abs(k - A[i][j])] = True
                        dp[i][j][abs(k + A[i][j])] = True

            if j != 0:
                for k in range(K):
                    if dp[i][j - 1][k] == True:
                        dp[i][j][abs(k - A[i][j])] = True
                        dp[i][j][abs(k + A[i][j])] = True


    for k in range(K):
        if dp[H-1][W-1][k] ==True:
            print(k)
            exit()

if __name__ == "__main__":
    main()


# Optimized solution
import sys
import math
import bisect
import heapq


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


def main():
    H, W = list(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[i][j][k]
    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])))


if __name__ == "__main__":
    main()
# Original solution
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
from collections import defaultdict
from itertools import product
def resolve():
    h,w=list(map(int,input().split()))
    A=[list(map(int,input().split())) for _ in range(h)]
    B=[list(map(int,input().split())) for _ in range(h)]
    dp=[[defaultdict(int) for _ in range(w)] for _ in range(h)]
    x=A[-1][-1]-B[-1][-1]
    dp[-1][-1][x]=1
    dp[-1][-1][-x]=1

    for i,j in product(list(range(h-1,-1,-1)),list(range(w-1,-1,-1))):
        x=A[i][j]-B[i][j]
        if(i+1<h):
            for key,val in list(dp[i+1][j].items()):
                if(val==1):
                    dp[i][j][key+x]=1
                    dp[i][j][key-x]=1
        if(j+1<w):
            for key,val in list(dp[i][j+1].items()):
                if(val==1):
                    dp[i][j][key+x]=1
                    dp[i][j][key-x]=1

    ans=INF
    for key,val in list(dp[0][0].items()):
        if(val==1): ans=min(ans,abs(key))

    print(ans)
resolve()

# Optimized solution
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    h,w=list(map(int,input().split()))
    A=[list(map(int,input().split())) for _ in range(h)]
    B=[list(map(int,input().split())) for _ in range(h)]
    dp=[[0]*w for _ in range(h)]
    x=abs(A[0][0]-B[0][0])
    dp[0][0]|=1<<x

    from itertools import product
    for i,j in product(list(range(h)),list(range(w))):
        x=abs(A[i][j]-B[i][j])
        if(i-1>=0):
            for k,s in enumerate(bin(dp[i-1][j])[2:][::-1]):
                if(s=='1'):
                    dp[i][j]|=1<<(k+x)
                    dp[i][j]|=1<<(abs(k-x))
        if(j-1>=0):
            for k,s in enumerate(bin(dp[i][j-1])[2:][::-1]):
                if(s=='1'):
                    dp[i][j]|=1<<(k+x)
                    dp[i][j]|=1<<(abs(k-x))

    for k,s in enumerate(bin(dp[-1][-1])[2::][::-1]):
        if(s=='1'):
            print(k)
            return
resolve()
# Original solution
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    h,w=list(map(int,input().split()))
    A=[list(map(int,input().split())) for _ in range(h)]
    B=[list(map(int,input().split())) for _ in range(h)]
    dp=[[0]*w for _ in range(h)]
    x=abs(A[0][0]-B[0][0])
    dp[0][0]|=1<<x

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

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

# Optimized solution
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    h,w=list(map(int,input().split()))
    A=[list(map(int,input().split())) for _ in range(h)]
    B=[list(map(int,input().split())) for _ in range(h)]
    dp=[[0]*w for _ in range(h)]
    x=abs(A[0][0]-B[0][0])
    offset=12800
    dp[0][0]|=1<<(x+offset)
    dp[0][0]|=1<<(-x+offset)

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

    for i in range(12800):
        if((dp[-1][-1]>>(i+offset))&1):
            print(i)
            return
resolve()
# Original solution
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


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] * M for _ in range(W)] for _ in range(H)]

def nasu(t):
  if t >= 0 and t < M:
    return True
  else:
    return False

DP[0][0][D[0][0]] = 1
for i in range(H + W):
  for j in range(H):
    if i - j >= W: continue
    x = i - j
    y = j
    if x < 0: continue
    if x - 1 >= 0:
      for k in range(M):
        if DP[y][x - 1][k] == 1:
          t = k + D[y][x]
          if nasu(t):
            DP[y][x][t] = 1
          t = abs(k - D[y][x])
          if nasu(t):
            DP[y][x][t] = 1
    if y - 1 >= 0:
      for k in range(M):
        if DP[y - 1][x][k] == 1:
          t = k + D[y][x]
          if nasu(t):
            DP[y][x][t] = 1
          t = abs(k - D[y][x])
          if nasu(t):
            DP[y][x][t] = 1

for i in range(M):
  if DP[-1][-1][i] == 1:
    print(i)
    break

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

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

for i in range(H):
    for j in range(W):
        dif = abs(A[i][j] - B[i][j])
        if i > 0:
            for k in range(H*W*80):
                if dp[i-1][j][k] == 1:
                    dp[i][j][k+dif] = 1
                    if k >= dif:
                        dp[i][j][k-dif] = 1
                    else:
                        dp[i][j][dif-k] = 1
        if j > 0:
            for k in range(H*W*80):
                if dp[i][j-1][k] == 1:
                    dp[i][j][k+dif] = 1
                    if k >= dif:
                        dp[i][j][k-dif] = 1
                    else:
                        dp[i][j][dif-k] = 1

for k in range(H*W*80):
    if dp[H-1][W-1][k] == 1:
        ans = k
        break

print(ans)


# Optimized solution
H, W = list(map(int, input().split()))
A = []
for i in range(H):
    A.append(list(map(int, input().split())))
B = []
for i in range(H):
    B.append(list(map(int, input().split())))

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

for i in range(H):
    for j in range(W):
        dif = abs(A[i][j] - B[i][j])
        if i > 0:
            for k in range((H+W)*80):
                if dp[i-1][j][k] == 1:
                    dp[i][j][k+dif] = 1
                    if k >= dif:
                        dp[i][j][k-dif] = 1
                    else:
                        dp[i][j][dif-k] = 1
        if j > 0:
            for k in range((H+W)*80):
                if dp[i][j-1][k] == 1:
                    dp[i][j][k+dif] = 1
                    if k >= dif:
                        dp[i][j][k-dif] = 1
                    else:
                        dp[i][j][dif-k] = 1

for k in range(H*W*80):
    if dp[H-1][W-1][k] == 1:
        ans = k
        break

print(ans)
# Original solution
H, W = list(map(int, input().split()))
A = []
for i in range(H):
    A.append(list(map(int, input().split())))
B = []
for i in range(H):
    B.append(list(map(int, input().split())))

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

for i in range(H):
    for j in range(W):
        dif = abs(A[i][j] - B[i][j])
        if i > 0:
            for k in range((H+W)*80):
                if dp[i-1][j][k] == 1:
                    dp[i][j][k+dif] = 1
                    if k >= dif:
                        dp[i][j][k-dif] = 1
                    else:
                        dp[i][j][dif-k] = 1
        if j > 0:
            for k in range((H+W)*80):
                if dp[i][j-1][k] == 1:
                    dp[i][j][k+dif] = 1
                    if k >= dif:
                        dp[i][j][k-dif] = 1
                    else:
                        dp[i][j][dif-k] = 1

for k in range(H*W*80):
    if dp[H-1][W-1][k] == 1:
        ans = k
        break

print(ans)


# Optimized solution
H, W = list(map(int, input().split()))
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]
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)
# Original solution
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

MAX = 30000   # (80+80)*80=12800
MID = 15000   # 15000を0とする
H, W = list(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])

dp = [[[0]*MAX for _ in range(W)] for _ in range(H)]
dp[0][0][MID+C[0][0]] = 1
dp[0][0][MID-C[0][0]] = 1
for i in range(H):
    for j in range(W):
        if i:
            for k in range(MAX):
                if dp[i-1][j][k] == 1:
                    dp[i][j][k+C[i][j]] = 1
                    dp[i][j][k-C[i][j]] = 1
        if j:
            for k in range(MAX):
                if dp[i][j-1][k] == 1:
                    dp[i][j][k+C[i][j]] = 1
                    dp[i][j][k-C[i][j]] = 1
ans = MAX
for i in range(MAX):
    if dp[H-1][W-1][i] == 1:
        ans = min(ans, abs(i-MID))
print(ans)

# Optimized solution
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

H, W = list(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) # (80+80)*80=12800

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)
# Original solution
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)]
	
	for i in range(h):
		A=list(map(int,input().split()))
		for j in range(w):
			G[i][j]+=A[j]
	for i in range(h):
		B = list(map(int, input().split()))
		for j in range(w):
			G[i][j] =abs(G[i][j]-B[j])
	M=(h+w)*80
	
	li = [[[] for i in range(w)] for j in range(h)]
	for i in range(h):
		for j in range(w):
			if i == j == 0:
				li[i][j].append(G[i][j])
			elif i == 0:
				for k in li[i][j - 1]:
					aa = abs(k - G[i][j])
					bb = abs(k + G[i][j])
					if aa <= M:
						li[i][j].append(aa)
					if abs(bb) <= M:
						li[i][j].append(bb)
			elif j == 0:
				for k in li[i - 1][j]:
					aa = abs(k - G[i][j])
					bb = abs(k + G[i][j])
					if aa <= M:
						li[i][j].append(aa)
					if abs(bb) <= M:
						li[i][j].append(bb)
			else:
				for k in li[i - 1][j]:
					aa = abs(k - G[i][j])
					bb = abs(k + G[i][j])
					if aa <= M:
						li[i][j].append(aa)
					if abs(bb) <= M:
						li[i][j].append(bb)
				for k in li[i][j - 1]:
					aa = abs(k - G[i][j])
					bb = abs(k + G[i][j])
					if aa <= M:
						li[i][j].append(aa)
					if abs(bb) <= M:
						li[i][j].append(bb)
			
			s = set(li[i][j])
			li[i][j] = list(s)
	
	print((min(li[h - 1][w - 1])))


if __name__ == "__main__":
	main()

# Optimized solution
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()
# Original solution
import sys

input=sys.stdin.readline

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

C=[[0 for i in range(0,W)] for j in range(0,H)]
for i in range(0,H):
    for j in range(0,W):
        C[i][j]=abs(A[i][j]-B[i][j])

dp=[[[False for k in range(0,min(80*(i+j+1)+1,80*(H-1-i+W-1-j+1)+1))] for i in range(0,W)] for j in range(0,H)]
dp[0][0][C[0][0]]=True
for i in range(0,H):
    for j in range(0,W):
        for k in range(0,min(80*(i+j+1)+1,80*(H-1-i+W-1-j+1)+1)):
            ans=False
            if i>=1:
                if k+C[i][j]<len(dp[i-1][j]):
                    ans= ans or dp[i-1][j][k+C[i][j]]
                if abs(k-C[i][j])<len(dp[i-1][j]):
                    ans= ans or dp[i-1][j][abs(k-C[i][j])]
            if j>=1:
                if k+C[i][j]<len(dp[i][j-1]):
                    ans= ans or dp[i][j-1][k+C[i][j]]
                if abs(k-C[i][j])<len(dp[i][j-1]):
                    ans= ans or dp[i][j-1][abs(k-C[i][j])]
            if (i,j)!=(0,0):
                dp[i][j][k]=ans

ans=100
for i in range(0,80+1):
    test=100
    if dp[H-1][W-1][i]:
        test=i
    if ans>test:
        ans=test

print(ans)

# Optimized solution
import sys

input=sys.stdin.readline

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

C=[[0 for i in range(0,W)] for j in range(0,H)]
for i in range(0,H):
    for j in range(0,W):
        C[i][j]=abs(A[i][j]-B[i][j])

dp=[[0 for i in range(0,W)] for i in range(0,H)]
if C[0][0]!=0:
    dp[0][0]=pow(2,6400+C[0][0])+pow(2,6400-C[0][0])
else:
    dp[0][0]=pow(2,6400)
for i in range(0,H):
    for j in range(0,W):
        ans1=0
        ans2=0
        if i!=0:
            ans1=(dp[i-1][j]<<C[i][j])|(dp[i-1][j]>>C[i][j])
        if j!=0:
            ans2=(dp[i][j-1]<<C[i][j])|(dp[i][j-1]>>C[i][j])
        if (i,j)!=(0,0):
            dp[i][j]=ans1|ans2

ans=100
for i in range(0,80+1):
    test=100
    if dp[H-1][W-1]>>(6400+i) &1 ==1:
        test=i
    if ans>test:
        ans=test

print(ans)
# Original solution
import sys

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


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

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

    res = f_inf
    for num in dp[-1][-1]:
        res = min(res, abs(num))
    print(res)


if __name__ == '__main__':
    resolve()


# Optimized solution
import sys

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


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

    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)


if __name__ == '__main__':
    resolve()