CodeEff/ECCO huggingface dataset:
# 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)]

for i in range(h):
    for j in range(w):
        a[i][j] = abs(a[i][j] - b[i][j])

dp = [[0] * 9600 for _ in range(w*h)]
for i in range(h):
    for j in range(w):
        min_ = max(0, 3200 - min(i+j, 160-i-j)*80)
        max_ = min(9599, 3200 + min(i+j, 160-i-j)*80)
        a_tmp = a[i][j]
        if(i==0)&(j==0):
            dp[i*w+j][3200+a_tmp] += 1
            dp[i*w+j][3200-a_tmp] += 1
        elif(i==0):
            for k in range(min_, max_+1):
                if(k+a_tmp < 9600):
                    dp[i*w+j][k+a_tmp] += dp[i*w+j-1][k]
                if(k-a_tmp >= 0):
                    dp[i*w+j][k-a_tmp] += dp[i*w+j-1][k]
        elif(j==0):
            for k in range(min_, max_+1):
                if(k+a_tmp < 9600):
                    dp[i*w+j][k+a_tmp] += dp[i*w+j-w][k]
                if(k+a_tmp >= 0):
                    dp[i*w+j][k-a_tmp] += dp[i*w+j-w][k]
        else:
            for k in range(min_, max_+1):
                if(k+a_tmp < 9600):
                    dp[i*w+j][k+a_tmp] += dp[i*w+j-1][k]
                    dp[i*w+j][k+a_tmp] += dp[i*w+j-w][k]
                if(k+a_tmp >= 0):
                    dp[i*w+j][k-a_tmp] += dp[i*w+j-1][k]
                    dp[i*w+j][k-a_tmp] += dp[i*w+j-w][k]

ans = 3200
for i in range(3100, 3300):
    if(dp[-1][i] > 0):
        ans = min(ans, abs(i-3200))

print(ans)

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

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)
# Original solution
h,w = list(map(int, input().split()))
al = []
bl = []

for _ in range(h):
    row = list(map(int, input().split()))
    al.append(row)

for _ in range(h):
    row = list(map(int, input().split()))
    bl.append(row)


cl = [ [0]*w for _ in range(h) ]
for i in range(h):
    for j in range(w):
        cl[i][j] = abs(al[i][j] - bl[i][j]) 

INF = 10**9
lim = (h+w)*80
# dp = [ [INF]*6400 for _ in range(h) ]
dp = [ [ [False]*(lim+1)  for _ in range(w) ] for _ in range(h) ]

dp[0][0][cl[0][0]] = True

for i in range(h):
    for j in range(w):
        for k in range(0,lim+1):
            if dp[i][j][k] == True:
                if i+1 < h:
                    v = cl[i+1][j]
                    dp[i+1][j][k+v] = True
                    dp[i+1][j][abs(k-v)] = True
                if j+1 < w:
                    v = cl[i][j+1]
                    dp[i][j+1][k+v] = True
                    dp[i][j+1][abs(k-v)] = True


for i,v in enumerate(dp[-1][-1]):
    if v == True:
        print(i)
        break

# Optimized solution
h,w = list(map(int, input().split()))
al = []
bl = []

for _ in range(h):
    row = list(map(int, input().split()))
    al.append(row)

for _ in range(h):
    row = list(map(int, input().split()))
    bl.append(row)


cl = [ [0]*w for _ in range(h) ]
for i in range(h):
    for j in range(w):
        cl[i][j] = abs(al[i][j] - bl[i][j]) 

INF = 10**9
lim = (h+w)*80//2+1
# dp = [ [INF]*6400 for _ in range(h) ]
dp = [ [ [False]*(lim+1)  for _ in range(w) ] for _ in range(h) ]

dp[0][0][cl[0][0]] = True

for i in range(h):
    for j in range(w):
        for k in range(0,lim+1):
            if dp[i][j][k] == True:
                if i+1 < h:
                    v = cl[i+1][j]
                    if k+v <= lim: dp[i+1][j][k+v] = True
                    dp[i+1][j][abs(k-v)] = True
                if j+1 < w:
                    v = cl[i][j+1]
                    if k+v <= lim: dp[i][j+1][k+v] = True
                    dp[i][j+1][abs(k-v)] = True


for i,v in enumerate(dp[-1][-1]):
    if v == True:
        print(i)
        break
# Original solution
from itertools import product

H, W = list(map(int, input().split()))
grid_A = [list(map(int, input().split())) for _ in range(H)]
grid_B = [list(map(int, input().split())) for _ in range(H)]
diff = []
M = 0
for A, B in zip(grid_A, grid_B):
    temp = [abs(a - b) for a, b in zip(A, B)]
    M = max(max(temp), M)
    diff.append(temp)
dp = [[[False]*((H+W)*(M+1)) for _ in range(W)] for _ in range(H)]
dp[0][0][diff[0][0]] = True
for i, j in product(list(range(H)), list(range(W))):
    for m in range((H+W)*(M+1)):
        if dp[i][j][m]:
            if i + 1 < H:
                d = diff[i+1][j]
                dp[i+1][j][m + d] = True
                dp[i+1][j][abs(m - d)] = True
            if j + 1 < W:
                d = diff[i][j+1]
                dp[i][j+1][m + d] = True
                dp[i][j+1][abs(m - d)] = True
for i in range((H+W)*(M+1)):
    if dp[H-1][W-1][i]:
        print(i)
        break

# Optimized solution
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 = list(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(list(range(H)), list(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))
# Original solution
import sys
input = sys.stdin.readline

H,W = list(map(int,input().split()))
#差分だけで良い，これを*1 or -1していく
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])
        
 # i,jマスにおいて，偏りがkになれるかどうかでDP
K = 4001 #適当
kk = int((K-1)/2)
dp = [[[False]*K for _ in range(W)] for _ in range(H)]
 
dp[0][0][kk+diff[0][0]] = True
dp[0][0][kk-diff[0][0]] = True
 
for i in range(H):
    for j in range(W):
        a= min((i+j)*80,kk)
        for k in range(kk-a,kk+a+1):
            if i-1>=0:
                if k+diff[i][j]<K:
                    if dp[i-1][j][k+diff[i][j]]:
                        dp[i][j][k] =True
                if k-diff[i][j]>=0:
                    if dp[i-1][j][k-diff[i][j]]:
                        dp[i][j][k] =True
            if j-1>=0:
                if k+diff[i][j]<K:
                    if dp[i][j-1][k+diff[i][j]]:
                        dp[i][j][k] =True
                if k-diff[i][j]>=0:
                    if dp[i][j-1][k-diff[i][j]]:
                        dp[i][j][k] =True
ans = K

for k in range(K):
    if dp[-1][-1][k]:
        ans = min(ans,abs(k-kk))
print(ans)

# Optimized solution
import sys
input = sys.stdin.readline

H,W = list(map(int,input().split()))
#差分だけで良い，これを*1 or -1していく
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])
        
 # i,jマスにおいて，偏りがkになれるかどうかでDP
#dp[i][j]にはkという数字にたどり着けるかどうかを示す2進数を入れる

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


#0のところから見て，最初に1が出てきたところが答え
for k in range(ofs):
    if (dp[-1][-1]>>(k+ofs)&1):
        print(k)
        break
# Original solution
H,W = list(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)]

dp = [[{} for _ in range(W)] for _ in range(H)]
t = A[0][0]-B[0][0]
if t < 0:
    dp[0][0][-t*2] = True
    dp[0][0][-t*2-1] = True
elif t > 0:
    dp[0][0][2*t-1] = True
    dp[0][0][2*t] = True
else:
    dp[0][0][0] = True

for i in range(H):
    for j in range(W):
        if i > 0:
            for k in dp[i-1][j]:
                if k % 2 == 0:
                    num = -k//2
                else:
                    num = (k+1)//2
                num += A[i][j]-B[i][j]
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                num += (-A[i][j]+B[i][j])*2
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
        if j > 0:
            for k in dp[i][j-1]:
                if k % 2 == 0:
                    num = -k//2
                else:
                    num = (k+1)//2
                num += A[i][j]-B[i][j]
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                num += (-A[i][j]+B[i][j])*2
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                    
ans = 10**9
for k in dp[H-1][W-1]:
    ans = min(ans, k//2+k%2)
        
print(ans)

# Optimized solution
H,W = list(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)
# Original 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)]
d=[[[0]*80*H*W for i in range(W+1)] for j in range(H+1)]
d[1][1][A[0][0]-B[0][0]]=d[1][1][B[0][0]-A[0][0]]=1
for h in range(H):
    for w in range(W):
        x=A[h][w]-B[h][w]
        for y in range(80*H*W):
            if d[h][w+1][y] or d[h+1][w][y]:
                d[h+1][w+1][(y+x)%(80*H*W)]=d[h+1][w+1][(y-x)%(80*H*W)]=1
for i in range(40*H*W):
    if d[H][W][i] or d[H][W][-i]:
        print(i)
        exit()

# 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)]
d=[[set() for i in range(W+1)] for j in range(H+1)]
d[1][1].add(A[0][0]-B[0][0])
d[1][1].add(B[0][0]-A[0][0])
for h in range(H):
    for w in range(W):
        x=A[h][w]-B[h][w]
        for y in d[h][w+1]:
            d[h+1][w+1].add((y+x)%(80*H*W))
            d[h+1][w+1].add((y-x)%(80*H*W))
        for y in d[h+1][w]:
            d[h+1][w+1].add((y+x)%(80*H*W))
            d[h+1][w+1].add((y-x)%(80*H*W))
for i in range(80*H*W):
    if i in d[H][W] or -i in d[H][W]:
        print(i)
        exit()
# Original solution
h,w = list(map(int, input().split( )))
a = []
b = []
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])
###差のmaxまでをlistで確保する代わりに可能な値をsetで保持
dp = [[set() for j in range(w)] for i in range(h)]
dp[0][0].add(a[0][0])

for j in range(1,w):
    for k in dp[0][j-1]:
        dp[0][j].add(abs(k-a[0][j]))
        dp[0][j].add(k+a[0][j])

for i in range(1,h):
    for j in range(w):
        for k in dp[i-1][j]:
            dp[i][j].add(abs(k-a[i][j]))
            dp[i][j].add(k+a[i][j])
        if j:
            for k in dp[i][j-1]:
                dp[i][j].add(abs(k-a[i][j]))
                dp[i][j].add(k+a[i][j])
#print(dp)
ans = 10**10
for k in dp[h-1][w-1]:
    ans = min(ans,k)
        
print(ans)





# Optimized solution

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])
###差のmaxまでをlistで確保する代わりに可能な値をsetで保持
###値が小さいのでsetからbitで保持に変更
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):###ループ一つ抜けてるわ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

###mxa=0でrange(0)になる式だと答えが出ない
for k in range(mxa*(h+w)+10):
    if tmp1&1:
        print(k)
        exit()
    tmp1>>=1
# Original solution
import sys
input = sys.stdin.readline

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)]
c = [[a[i][j] - b[i][j] for j in range(w)] for i in range(h)]

MAX_K = 80 * (h + w) + 5
dp = [[[False] * (MAX_K + 80) for _ in range(w)] for _ in range(h)]
dp[0][0][abs(c[0][0])] = True

for i in range(h):
    for j in range(w):
        for k in range(MAX_K):
            if dp[i][j][k]:
                if i < h - 1:
                    dp[i + 1][j][abs(k + c[i + 1][j])] = True
                    dp[i + 1][j][abs(k - c[i + 1][j])] = True

                if j < w - 1:
                    dp[i][j + 1][abs(k + c[i][j + 1])] = True
                    dp[i][j + 1][abs(k - c[i][j + 1])] = True

for k in range(MAX_K):
    if dp[h - 1][w - 1][k]:
        ans = k
        break
print(ans)


# Optimized solution
import sys
input = sys.stdin.readline

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)]
c = [[a[i][j] - b[i][j] for j in range(w)] for i in range(h)]

dp = [[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add(c[0][0])

for i in range(h):
    for j in range(w):
        for x in dp[i][j]:
            if i < h - 1:
                dp[i + 1][j].add(abs(x + c[i + 1][j]))
                dp[i + 1][j].add(abs(x - c[i + 1][j]))

            if j < w - 1:
                dp[i][j + 1].add(abs(x + c[i][j + 1]))
                dp[i][j + 1].add(abs(x - c[i][j + 1]))

print((min(dp[h - 1][w - 1])))
# 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)]
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])
a = b = 0
dp = [[[False]*6400 for _ in range(w+1)] for _ in range(h+1)]
dp[0][1][0] = True
dp[1][0][0] = True
for i in range(1,h+1):
  for j in range(1,w+1):
    for x in range(6400):
      if dp[i-1][j][x] or dp[i][j-1][x]:
        dp[i][j][abs(x-g[i-1][j-1])] |= True
        if x + g[i-1][j-1] < 6400:
            dp[i][j][x+g[i-1][j-1]] |= True
for x in range(6400):
  if dp[h][w][x]:
    print(x)
    break


# Optimized solution
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
# Original solution
M = 6400*4
mid = M//2
INF = 10**18

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

# bitset高速化
# 初期値
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:
        # print(i-mid)
        ans = min(ans, abs(i-mid))
print(ans)

# Optimized solution
M = 6400
mid = M//2
INF = 10**18

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

# bitset高速化
# 初期値
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:
        # print(i-mid)
        ans = min(ans, abs(i-mid))
print(ans)
# Original solution
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    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)]

    dp = [[[0]*12800 for _ in range(W)] for _ in range(H)]
    dp[0][0][6400] = 1
    for i in range(H):
        for j in range(W):
            if i == H - 1 and j == W - 1:
                break
            a,b = A[i][j],B[i][j]
            for k in range(12800):
                if dp[i][j][k] == 0:
                    continue
                if 0 <= k + a - b < 12800:
                    if i + 1 < H:
                        dp[i + 1][j][k + a - b] |= dp[i][j][k]
                    if j + 1  < W:
                        dp[i][j + 1][k + a - b] |= dp[i][j][k]
                if 0 <= k - a + b < 12800:
                    if i + 1 < H:
                        dp[i + 1][j][k - a + b] |= dp[i][j][k]
                    if j + 1 < W:
                        dp[i][j + 1][k - a + b] |= dp[i][j][k]
    
    ans = INF
    a,b = A[-1][-1],B[-1][-1]
    for i in range(12800):
        if (0 <= i + a - b < 12800 and dp[-1][-1][i + a - b]) or (0 <= i - a + b < 12800 and dp[-1][-1][i - a + b]):
            ans = min(ans,abs(i - 6400))

    print(ans)                
if __name__ == '__main__':
    main()
    


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

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

    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)
if __name__ == '__main__':
    main()
# Original solution
def main():
    H, W = list(map(int, input().split()))
    A = []
    for _ in range(H):
        A += list(map(int, input().split()))  # 速度のため一次元で管理
    B = []
    for _ in range(H):
        B += list(map(int, input().split()))  # 速度のため一次元で管理

    dp = [0] * (H * W)  # 速度のため一次元で管理,dp[pos]:=bit,[-80*H*W,80*H*W]->[0,2*80*H*W]
    d = abs(A[0] - B[0])
    dp[0] |= 1 << (80 * H * W + d)
    dp[0] |= 1 << (80 * H * W - d)

    for r in range(H):
        for c in range(W):
            p = r * W + c
            d = abs(A[p] - B[p])
            if r > 0:
                dp[p] |= (dp[p - W] << d)
                dp[p] |= (dp[p - W] >> d)
            if c > 0:
                dp[p] |= (dp[p - 1] << d)
                dp[p] |= (dp[p - 1] >> d)

    ans = 2 * 80 * H * W + 1
    for j in range(2 * 80 * H * W + 1):
        if (dp[H * W - 1] >> j) & 1:
            t = j - 80 * H * W
            if ans > abs(t):
                ans = abs(t)
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    H, W = list(map(int, input().split()))

    MAX_BIAS = 80 * (H + W - 1)

    A = []
    for _ in range(H):
        A += list(map(int, input().split()))  # 速度のため一次元で管理
    B = []
    for _ in range(H):
        B += list(map(int, input().split()))  # 速度のため一次元で管理

    dp = [0] * (H * W)  # 速度のため一次元で管理,dp[pos]:=bit,[-80*(H+W-1),80*(H+W-1)]->[0,2*80*(H+W-1)]
    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)


if __name__ == '__main__':
    main()