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

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)

dp = [set() for i in range(W)]
dp[0].add(0)
for i,crow in enumerate(C):
    dp2 = [set() for j in range(W)]
    for j,(c,dpr) in enumerate(zip(C[i],dp)):
        for d in dpr:
            dp2[j].add(d+c)
            dp2[j].add(d-c)
    for j,c in enumerate(C[i]):
        if j==0: continue
        for d in dp2[j-1]:
            dp2[j].add(d+c)
            dp2[j].add(d-c)
    dp = dp2

print((min(abs(a) for a in dp[-1])))

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

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

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)

# 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)]
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)
# Original solution
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 h in range(H):
        for w in range(W):
            C[h][w] = abs(A[h][w] - B[h][w])

    dp = [[[0] * (80 * 80) for _ in range(W)] for _ in range(H)]
    dh = [1,0]
    dw = [0,1]
    dp[0][0][C[0][0]] = 1
    for h in range(H):
        for w in range(W):
            for i in range(80*80):
                for j in range(2):
                    nh = h - dh[j]
                    nw = w - dw[j]
                    if 0 <= nh < H and 0 <= nw < W:
                        if abs(C[h][w] + i) < 6400:
                            dp[h][w][abs(C[h][w] + i)] |= dp[nh][nw][i]
                        if abs(C[h][w] - i) < 6400:
                            dp[h][w][abs(C[h][w] - i)] |= dp[nh][nw][i]
                    
    ans = float('inf')
    for i in range(80*80):
        if dp[H-1][W-1][i]:
            ans = min(i, ans)

    print(ans) 



if __name__ == "__main__":
    main()

# Optimized solution
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 h in range(H):
        for w in range(W):
            C[h][w] = abs(A[h][w] - B[h][w])

    dp = [[0 for _ in range(6400)] for _ in range(H*W)]
    dp[0][C[0][0]] = 1

    for d in range(H*W):
        h, w = divmod(d, W)
        for k in range(6400):
            if not dp[h*W+w][k]:
                continue
            nh, nw = h+1, w+1
            if nh < H:
                if k + C[nh][w] < 6400:
                    dp[nh*W+w][k+C[nh][w]] = 1
                dp[nh*W+w][abs(k-C[nh][w])] = 1
            if nw < W:
                if k + C[h][nw] < 6400:
                    dp[h*W+nw][k+C[h][nw]] = 1
                dp[h*W+nw][abs(k-C[h][nw])] = 1

    for k in range(6400):
        if dp[-1][k]:
            print(k)
            exit()

if __name__ == "__main__":
    main()
# Original solution
import sys

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

dp = [[[False for i in range(80 * h + 80 * w + 1)] for _ in range(1 + w)] for j in range(1 + h)]
# dp[i][j][k]=マスの(i,j)において絶対値をkにすることができるかどうか
dp[0][0][0] = True
dp[1][0][0] = True
dp[0][1][0] = True

# 初期値の設定
for i in range(1, h + 1):
    for j in range(1, w + 1):
      
        a_num = a[i - 1][j - 1]
        b_num = b[i - 1][j - 1]
        dif = a_num - b_num
        if i == 1:
            for k in range(80 * i + 80 * j - 79):
                if dp[1][j - 1][abs(k - dif)] or dp[1][j - 1][abs(k + dif)]:
                    dp[1][j][abs(k)] = True

        elif j == 1:
            for k in range(80 * i + 80 * j - 79):
                if dp[i - 1][1][abs(k - dif)] or dp[i - 1][1][abs(k + dif)]:
                    dp[i][1][k] = True

        else:
            for k in range(80 * i + 80 * j - 79):
                if dp[i - 1][j][abs(k - dif)] or dp[i - 1][j][abs(k + dif)] or dp[i][j - 1][abs(k - dif)] or dp[i][j - 1][abs(k + dif)]:
                    dp[i][j][k] = True

ans = 0
for i in range(80 * h + 80 * w + 1):
    if dp[h][w][i]:
        ans = i
        break

print(ans)


# Optimized solution
# Atcoder Problem Solving
H,W=list(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)]

# Bitset演算
# 答えは絶対値が絶対-80から80以内に収まる

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

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
        #lim = 1280
        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]])))

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

arr = [set() for _ in range(W)]
arr[0].add(0)
for y in range(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]])))
# Original solution
h,w = list(map(int,input().split()))
A = []
for _ in range(h):
    a = list(map(int,input().split()))
    A.append(a)
B = []
for _ in range(h):
    b = list(map(int,input().split()))
    B.append(b)
g = [[0]*(w+1) for _ in range(h+1)]
m = 0
for i in range(1,h+1):
    for j in range(1,w+1):
        g[i][j] = abs(A[i-1][j-1]-B[i-1][j-1])
m  = 80*(h+w-1)//2
dp = [[[False for _ in range(m+1)] for _ in range(w+1)] for _ in range(h+1)]
dp[1][1][g[1][1]] = True

for i in range(1,h+1):
    for j in range(1,w+1):
        for k in range(m+1):
            if k + g[i][j]<m:
                dp[i][j][k] = dp[i][j][k] or dp[i-1][j][abs(k-g[i][j])] or dp[i-1][j][k+g[i][j]] or dp[i][j-1][abs(k-g[i][j])] or dp[i][j-1][k+g[i][j]]
            else:
                dp[i][j][k] = dp[i][j][k] or dp[i-1][j][abs(k-g[i][j])] or dp[i][j-1][abs(k-g[i][j])]
            

for k in range(m+1):
    if dp[-1][-1][k]:
        print(k);exit()



# Optimized solution
H, W = list(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)]# 答えから80*(H+W-1)を引く
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
# Original solution
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
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
        else:
            for l in range(ma-k):
                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()

# Optimized solution
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
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
        elif i == 0:
            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]
        elif j == 0:
            for l in range(up):
                DP[i][j][l] = DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
        else:
            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()
# Original solution
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
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])
        up = (i+j+1)*80+1
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        elif i == 0:
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k]
        elif j == 0:
            for l in range(up):
                DP[i][j][l] = DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
        else:
            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()

# Optimized solution
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
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()
# Original solution
def x():
	return list(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)))

# 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)]
# zeroは真ん中にする
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()
# Original solution
import bisect
import collections
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)]

q = collections.deque()

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

dp = [[[False]*((M*H*W) + 1) 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][C[i][j]] = True
            else:
                for m in range(M+1):
                    if dp[i][j-1][m]:
                        dp[i][j][abs(m - C[i][j])] = True
                        dp[i][j][m+C[i][j]] = True

        else:
            if j != 0:
                for m in range(M+1):
                    if dp[i][j-1][m]:
                        dp[i][j][abs(m - C[i][j])] = True
                        dp[i][j][m+C[i][j]] = True

            for m in range(M+1):
                if dp[i-1][j][m]:
                    dp[i][j][abs(m - C[i][j])] = True
                    dp[i][j][m+C[i][j]] = True
for i in range((M*H*W)+1):
    if dp[H-1][W-1][i]:
        print(i)
        break


# Optimized solution
import bisect
import collections
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)]

q = collections.deque()

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

dp = [[[False]*((M*(H+W)) + 1) 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][C[i][j]] = True
            else:
                for m in range((H+W)*M+1):
                    if dp[i][j-1][m]:
                        dp[i][j][abs(m - C[i][j])] = True
                        dp[i][j][m+C[i][j]] = True

        else:
            if j != 0:
                for m in range((H+W)*M+1):
                    if dp[i][j-1][m]:
                        dp[i][j][abs(m - C[i][j])] = True
                        dp[i][j][m+C[i][j]] = True

            for m in range((H+W)*M+1):
                if dp[i-1][j][m]:
                    dp[i][j][abs(m - C[i][j])] = True
                    dp[i][j][m+C[i][j]] = True
for i in range((M*(H+W))+1):
    if dp[H-1][W-1][i]:
        print(i)
        break
# Original solution
import bisect
import collections
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)]

q = collections.deque()

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

dp = [[[False]*((M*(H+W)) + 1) 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][C[i][j]] = True
            else:
                for m in range((H+W)*M+1):
                    if dp[i][j-1][m]:
                        dp[i][j][abs(m - C[i][j])] = True
                        dp[i][j][m+C[i][j]] = True

        else:
            if j != 0:
                for m in range((H+W)*M+1):
                    if dp[i][j-1][m]:
                        dp[i][j][abs(m - C[i][j])] = True
                        dp[i][j][m+C[i][j]] = True

            for m in range((H+W)*M+1):
                if dp[i-1][j][m]:
                    dp[i][j][abs(m - C[i][j])] = True
                    dp[i][j][m+C[i][j]] = True
for i in range((M*(H+W))+1):
    if dp[H-1][W-1][i]:
        print(i)
        break


# Optimized solution
import bisect
import collections
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)]

q = collections.deque()

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

dp = [[set() 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].add(C[i][j])
            else:
                for x in dp[i][j-1]:
                    dp[i][j].add(abs(x-C[i][j]))
                    dp[i][j].add(x+C[i][j])

        else:
            if j != 0:
                for x in dp[i][j-1]:
                    dp[i][j].add(abs(x-C[i][j]))
                    dp[i][j].add(x+C[i][j])

            for x in dp[i-1][j]:
                dp[i][j].add(abs(x-C[i][j]))
                dp[i][j].add(x+C[i][j])

print((min(dp[H-1][W-1])))
# Original solution
import bisect
import collections
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)]

q = collections.deque()

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

dp = [[set() 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].add(C[i][j])
            else:
                for x in dp[i][j-1]:
                    dp[i][j].add(abs(x-C[i][j]))
                    dp[i][j].add(x+C[i][j])

        else:
            if j != 0:
                for x in dp[i][j-1]:
                    dp[i][j].add(abs(x-C[i][j]))
                    dp[i][j].add(x+C[i][j])

            for x in dp[i-1][j]:
                dp[i][j].add(abs(x-C[i][j]))
                dp[i][j].add(x+C[i][j])

print((min(dp[H-1][W-1])))


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

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)
# Original solution
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    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)]
    abss = [[abs(A[i][j]-B[i][j])for j in range(w)] for i in range(h)]
    kmax = 6400
    dp = [[[0]*kmax for j in range(w)]for i in range(h)]
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h-1:
                        dp[i+1][j][abs(k-abss[i+1][j])] = 1
                        if k+abss[i+1][j] < kmax:
                            dp[i+1][j][k+abss[i+1][j]] = 1
                    if j != w-1:
                        dp[i][j+1][abs(k-abss[i][j+1])] = 1
                        if k+abss[i][j+1] < kmax:
                            dp[i][j+1][k+abss[i][j+1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == '__main__':
    main()

# Optimized solution
import sys
def input(): return sys.stdin.readline().rstrip()


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


if __name__ == '__main__':
    main()