CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input = sys.stdin.readline

def main():
    H, W = list(map(int, input().split()))
    P = W+1
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    L = 80*80
    dp = [[False]*(L+1) for _ in range((W+1)*(H+1))]
    for h in range(H+1):
        dp[h*P][0] = True
    for w in range(H+1):
        dp[w][0] = True
    
    for h in range(H):
        for w in range(W):
            c = abs(A[h][w] - B[h][w])
            for l in range(L+1):
                b1 = abs(l-c)
                b2 = abs(l+c)
                if b1 <= L:
                    dp[(h+1)*P+w+1][l] = dp[h*P+w+1][b1] or dp[(h+1)*P+w][b1]
                if b2 <= L:
                    dp[(h+1)*P+w+1][l] = dp[(h+1)*P+w+1][l] or dp[h*P+w+1][b2] or dp[(h+1)*P+w][b2]

    for l in range(L+1):
        if dp[H*P+W][l]:
            print(l)
            break


if __name__ == "__main__":
    main()

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

def main():
    H, W = list(map(int, input().split()))
    P = W+1
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    L = 80*80
    dp = [[False]*(L+1) for _ in range((W+1)*(H+1))]
    dp[P+1][abs(A[0][0]-B[0][0])] = True
    
    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0: continue
            c = abs(A[h][w] - B[h][w])
            for l in range(L-c+1):
                b1 = abs(l-c)
                b2 = abs(l+c)
                dp[(h+1)*P+w+1][l] = dp[h*P+w+1][b1] or dp[(h+1)*P+w][b1] or dp[h*P+w+1][b2] or dp[(h+1)*P+w][b2]

    for l in range(L+1):
        if dp[H*P+W][l]:
            print(l)
            break


if __name__ == "__main__":
    main()
# Original solution
h,w = list(map(int,input().split()))
al = []
for _ in range(h):
    a = list(map(int,input().split()))
    al.append(a)

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

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

from collections import defaultdict
dic = [[[0 for i in range(12801)] for _ in range(w)] for _ in range(h)]

dic[0][0][abl[i][j] + 6400] = 1
dic[0][0][-abl[i][j]+ 6400] = 1

for i in range(h):
    for j in range(w):
        for v, t in enumerate(dic[i][j]):
            if t == 1:
                if j + 1 < w:
                    if 0 <= v+abl[i][j+1] <= 12800:
                        dic[i][j+1][v+abl[i][j+1]] = 1
                    if 0 <= v-abl[i][j+1] <= 12800:
                        dic[i][j+1][v-abl[i][j+1]] = 1
                if i + 1 < h:
                    if 0 <=v+abl[i+1][j] <= 12800:
                        dic[i+1][j][v+abl[i+1][j]] = 1
                    if 0 <= v-abl[i+1][j] <= 12800:
                        dic[i+1][j][v-abl[i+1][j]] = 1

ans = float("inf")
for idx,i in enumerate(dic[-1][-1]):
    if i == 1:
        ans = min(ans, abs(idx-6401))
print(ans)




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

bitdp = [[0 for j in range(w)] for i in range(h)]
al = []
bl = []
for _ in range(h):
   a = list(map(int,input().split()))
   al.append(a)
for _ in range(h):
   b = list(map(int,input().split()))
   bl.append(b)
abl = [[] for i in range(h)]
for i in range(h):
   for j in range(w):
      abl[i].append(abs(al[i][j] - bl[i][j]))

bitdp[0][0] = 1 << h*w+abl[0][0]
for i in range(h):
   for j in range(w):
      if j != w-1:
         bitdp[i][j+1] |= bitdp[i][j] << abl[i][j+1] | bitdp[i][j] >> abl[i][j+1]
      if i != h-1:
         bitdp[i+1][j] |= bitdp[i][j] << abl[i+1][j] | bitdp[i][j] >> abl[i+1][j]

ans = []
for i in range(h*w*2+1):
   if (bitdp[-1][-1] >> i & 1):
      ans.append(abs(h*w-i))

print((min(ans)))
# Original solution
import sys
input = sys.stdin.readline

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

SA=[[0]*W for i in range(H)]

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

DP=[[0 for i in range(W)] for i in range(H)]

DP[0][0]=1<<SA[0][0]

def next(i,j):
    RET=[]
    if i+j<=6400:
        RET.append(i+j)
    if abs(i-j)<=6400:
        RET.append(abs(i-j))

    return RET

for i in range(H):
    for j in range(W):
        for k in range(6400):
            if 1<<k & DP[i][j]==0:
                continue
            if i+1<H:
                for r in next(k,SA[i+1][j]):
                    DP[i+1][j]|=1<<r

            if j+1<W:
                for r in next(k,SA[i][j+1]):
                    DP[i][j+1]|=1<<r
                
for i in range(6401):
    if 1<<i & DP[-1][-1]!=0:
        print(i)
        break


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

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

SA=[[0]*W for i in range(H)]

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

DP=[[0 for i in range(W)] for j in range(H)]

DP[0][0]=1<<(H+W)*80+SA[0][0]

for i in range(H):
    for j in range(W):
        tempbit=DP[i][j]
        if i+1<H:
            DP[i+1][j]|=tempbit<<SA[i+1][j]
            DP[i+1][j]|=tempbit>>SA[i+1][j]

        if j+1<W:
            DP[i][j+1]|=tempbit<<SA[i][j+1]
            DP[i][j+1]|=tempbit>>SA[i][j+1]

ANS=1<<30
for i in range((H+W)*160+1):
    if 1<<i & DP[-1][-1]!=0:
        ANS=min(ANS,abs(i-(H+W)*80))

print(ANS)
# Original solution
import sys
def main():
  input = sys.stdin.readline
  h, w = list(map(int, input().split()))
  A = []
  AB = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB.append(row)
 
  dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
  dp[0][0].add(AB[0][0])
  for i in range(h):
    for j in range(w):
      dif1 = 0
      dif2 = 0
      if j < w - 1:
        dif1 = AB[i][j+1]
      if i < h - 1:
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          dp[i][j+1].add(item+dif1)
          dp[i][j+1].add(abs(item-dif1))
        if i < h - 1:
          dp[i+1][j].add(item+dif2)
          dp[i+1][j].add(abs(item-dif2))
  print((min(dp[h-1][w-1])))
if __name__ == '__main__':
  main()


# Optimized solution
import sys
def main():
  input = sys.stdin.readline
  h, w = list(map(int, input().split()))
  A = []
  AB = []
  A_append = A.append
  AB_append = AB.append
  for i in range(h):
    row = list(map(int, input().split()))
    A_append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB_append(row)
 
  dp_cur = [set([]) for _ in range(w)]
  dp_cur[0].add(AB[0][0])
  for i in range(h):
    dp_next = [set([]) for _ in range(w)]
    for j in range(w):
      if j < w - 1:
        add1 = dp_cur[j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp_next[j].add
        dif2 = AB[i+1][j]
      for item in dp_cur[j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
    if i == h - 1:
      break
    dp_cur = dp_next
  print((min(dp_cur[w-1])))
if __name__ == '__main__':
  main()
# Original solution
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    H,W=list(map(int,input().split()))
    A=[[] for _ in range(H)]
    B=[[] for _ in range(H)]
    C=[[0]*W for _ in range(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):
            C[i][j]=abs(A[i][j]-B[i][j])
    C_sum=sum([sum(C[i]) for i in range(H)])
    dp=[[[False]*(C_sum*2+1) for _ in range(W)] for _ in range(H)]
    dp[0][0][C[0][0]]=True
    dp[0][0][-C[0][0]]=True
    for i in range(1,H):
        for k in range(C_sum*2+1):
            if dp[i-1][0][k]==True:
                dp[i][0][(k+C[i][0])%(C_sum*2+1)]=True
                dp[i][0][k-C[i][0]]=True
    for j in range(1,W):
        for k in range(C_sum*2+1):
            if dp[0][j-1][k]==True:
                dp[0][j][(k+C[0][j])%(C_sum*2+1)]=True
                dp[0][j][k-C[0][j]]=True
    
    for i in range(1,H):
        for j in range(1,W):
            for k in range(C_sum*2+1):
                if dp[i-1][j][k]==True:
                    dp[i][j][(k+C[i][j])%(C_sum*2+1)]=True
                    dp[i][j][k-C[i][j]]=True
                if dp[i][j-1][k]==True:
                    dp[i][j][(k+C[i][j])%(C_sum*2+1)]=True
                    dp[i][j][k-C[i][j]]=True
    for i in range(C_sum+1):
        if dp[H-1][W-1][i]==True:
            print(i)
            exit()
    
    

if __name__ == '__main__':
    main()


# Optimized solution
import sys
sys.setrecursionlimit(10**9)
INF = 10**18


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


def main():
    H, W = list(map(int, input().split()))
    A = [[] for _ in range(H)]
    B = [[] for _ in range(H)]
    C = [[0] * W for _ in range(H)]
    minusdigit = 80 * 80 * 2
    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):
            C[i][j] = abs(A[i][j] - B[i][j])
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = ((1 << minusdigit) << C[0][0]) | ((1 << minusdigit) >> C[0][0])
    for i in range(1, H):
        dp[i][0] = (dp[i - 1][0] << C[i][0]) | (dp[i - 1][0] >> C[i][0])
    for j in range(1, W):
        dp[0][j] = (dp[0][j - 1] << C[0][j]) | (dp[0][j - 1] >> C[0][j])

    for i in range(1, H):
        for j in range(1, W):
            dp[i][j] = (dp[i - 1][j] << C[i][j]) | (dp[i - 1][j] >> C[i][j]) | (dp[i][j - 1] << C[i][j]) | (dp[i][j - 1] >> C[i][j])
    ans = dp[H - 1][W - 1]
    for i in range(len(bin(ans)) - (2 + minusdigit)):
        if (ans >> (i + minusdigit)) & 0b1 == 1 or (-i + minusdigit >= 0 and (ans >> (-i + minusdigit)) & 0b1 == 1):
            print(i)
            exit()


if __name__ == '__main__':
    main()
# Original solution
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect,copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ;count = 0 ;pro = 1

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)]
a=[set() for j in range(w)]
dp=[copy.deepcopy(a) for i in range(h)]
# for di in dp:
#     print(*dp)
# dp[0][0]=set()
dp[0][0].add(abs(A[0][0]-B[0][0]))
# print(dp)
# for di in dp:
#     print(*dp)
for i in range(h):
    for j in range(w):
        # dp[i][j]=set()
        d=abs(A[i][j]-B[i][j])
        if i-1>=0:
            for si in dp[i-1][j]:
                dp[i][j].add(si+d)
                dp[i][j].add(abs(si-d))
        # print(dp[i][j])
        if j-1>=0:
            for si in dp[i][j-1]:
                dp[i][j].add(si+d)
                dp[i][j].add(abs(si-d))
        # print(dp[i][j])
ans=min(dp[h-1][w-1])
print(ans)
# for di in dp:
#     print(di)



# Optimized solution
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect,copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ;count = 0 ;pro = 1

h,w=list(map(int,input().split()))
A=[tuple(map(int,input().split())) for i in range(h)]
B=[tuple(map(int,input().split())) for i in range(h)]
a=[[] for j in range(w)]
dp=[copy.deepcopy(a) for i in range(h)]
dp[0][0].append(abs(A[0][0]-B[0][0]))
for i in range(h):
    for j in range(w):
        d=abs(A[i][j]-B[i][j])
        if i-1>=0:
            for si in dp[i-1][j]:
                dp[i][j].append(si+d)
                dp[i][j].append(abs(si-d))
        if j-1>=0:
            for si in dp[i][j-1]:
                dp[i][j].append(si+d)
                dp[i][j].append(abs(si-d))
ans=min(dp[h-1][w-1])
print(ans)
# for di in dp:
#     print(di)
# 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)]
Diff = [[False] * (W + 1) for _ in range(H + 1)]
M = 0
for i in range(H):
    for j in range(W):
        m = abs(A[i][j] - B[i][j])
        Diff[i][j] = m
        M += m

dp = [[[0] * (M + 1) for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][Diff[0][0]] = 1

for i in range(0, H):
    for j in range(0, W):
        for k in range(M + 1):
            if dp[i][j][k]:
                dp[i + 1][j][k + Diff[i + 1][j]] |= True
                dp[i + 1][j][abs(k - Diff[i + 1][j])] |= True
                dp[i][j + 1][k + Diff[i][j + 1]] |= True
                dp[i][j + 1][abs(k - Diff[i][j + 1])] |= True

for k in range(M + 1):
    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 _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
Diff = [[0] * (W + 1) for _ in range(H + 1)]
M = 0
for i in range(H):
    for j in range(W):
        m = abs(A[i][j] - B[i][j])
        Diff[i][j] = m
        M += m

dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[0][0] |= (1 << Diff[i][j])

for i in range(0, H):
    for j in range(0, W):
        here = dp[i][j]
        s = Diff[i][j]
        ni = 0
        nj = 0
        ni |= (here << s)
        nj |= (here << s)
        ni |= (here >> s)
        nj |= (here >> s)
        ex = 0
        for k in range(1, s + 1):
            if (here >> k) & 1:
                ex |= (1 << (s - k))
        ni |= ex
        nj |= ex
        dp[i + 1][j] |= ni
        dp[i][j + 1] |= nj

a = dp[H - 1][W - 1]
ans = 0
for k in range(M + 1):
    if (a >> k) & 1:
        ans = k
        break

print(k)
# Original solution
def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    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 = 0
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            m = abs(A[i][j] - B[i][j])
            grid[i][j] = m
            M = max(M, m)

    L = (H + W) * M
    dp = [[[0] * (L + 1) for _ in range(W)] for _ in range(H)]
    dp[0][0][grid[0][0]] = 1

    for i in range(H):
        for j in range(W):
            for k in range(L + 1):
                if dp[i][j][k] == 1:
                    if i < H - 1:
                        nk = grid[i + 1][j]
                        dp[i + 1][j][k + nk] |= 1
                        dp[i + 1][j][abs(k - nk)] |= 1
                    if j < W - 1:
                        nk = grid[i][j + 1]
                        dp[i][j + 1][k + nk] |= 1
                        dp[i][j + 1][abs(k - nk)] |= 1

    for k in range(L + 1):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    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 = 0
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            m = abs(A[i][j] - B[i][j])
            grid[i][j] = m
            M = max(M, m)

    dp = [[0] * W for _ in range(H)]
    dp[0][0] = (1 << grid[0][0])

    for i in range(H):
        for j in range(W):
            here = dp[i][j]
            if i < H - 1:
                nk = grid[i + 1][j]
                dp[i + 1][j] |= (here << nk) | (here >> nk)
                for k in range(1, nk + 1):
                    if (here >> k) & 1:
                        dp[i + 1][j] |= (1 << (nk - k))
            if j < W - 1:
                nk = grid[i][j + 1]
                dp[i][j + 1] |= (here << nk) | (here >> nk)
                for k in range(1, nk + 1):
                    if (here >> k) & 1:
                        dp[i][j + 1] |= (1 << (nk - k))

    k = dp[-1][-1]
    ans = 0
    while k > 0:
        if k & 1:
            print(ans)
            break
        k >>= 1
        ans += 1


if __name__ == "__main__":
    main()
# Original solution
def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    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 = 0
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            m = abs(A[i][j] - B[i][j])
            grid[i][j] = m
            M = max(M, m)

    dp = [[0] * W for _ in range(H)]
    dp[0][0] = (1 << grid[0][0])

    for i in range(H):
        for j in range(W):
            here = dp[i][j]
            if i < H - 1:
                nk = grid[i + 1][j]
                dp[i + 1][j] |= (here << nk) | (here >> nk)
                for k in range(1, nk + 1):
                    if (here >> k) & 1:
                        dp[i + 1][j] |= (1 << (nk - k))
            if j < W - 1:
                nk = grid[i][j + 1]
                dp[i][j + 1] |= (here << nk) | (here >> nk)
                for k in range(1, nk + 1):
                    if (here >> k) & 1:
                        dp[i][j + 1] |= (1 << (nk - k))

    k = dp[-1][-1]
    ans = 0
    while k > 0:
        if k & 1:
            print(ans)
            break
        k >>= 1
        ans += 1


if __name__ == "__main__":
    main()


# Optimized solution
def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    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 = 0
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            m = abs(A[i][j] - B[i][j])
            grid[i][j] = m
            M = max(M, m)

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

    for i in range(H):
        for j in range(W):
            here = dp[i][j]
            k = grid[i][j]

            dp[i][j] = (here << k) | (here >> k)
            for l in range(1, k + 1):
                if (here >> l) & 1:
                    dp[i][j] |= (1 << (k - l))

            if i < H - 1:
                dp[i + 1][j] |= dp[i][j]
            if j < W - 1:
                dp[i][j + 1] |= dp[i][j]

    V = dp[-1][-1]
    ans = 0
    while V > 0:
        if V & 1:
            print(ans)
            break
        V >>= 1
        ans += 1


if __name__ == "__main__":
    main()
# Original solution
e=enumerate
(h,w),*a=[list(map(int,t.split()))for t in open(0)]
dp=[0]*w
c=12800
for i,a in e(list(zip(a,a[h:]))):
  for j,(x,y)in e(list(zip(*a))):
    x=abs(x-y)
    t=0
    if j:t|=dp[j-1]<<x|dp[j-1]>>x
    if i:t|=dp[j]<<x|dp[j]>>x
    dp[j]=t if i+j else 2**c<<x
*a,=list(map(int,(bin(dp[w-1])[2:]).zfill(c-~c)))
print((min(i for i,(f,g)in e(list(zip(a[c:],a[c::-1])))if f+g)))

# Optimized solution
def main():
  e=enumerate
  (h,w),*a=[list(map(int,t.split()))for t in open(0)]
  dp=[0]*w
  c=12800
  for i,a in e(list(zip(a,a[h:]))):
    for j,(x,y)in e(list(zip(*a))):
      x=abs(x-y)
      t=0
      if j:t|=dp[j-1]<<x|dp[j-1]>>x
      if i:t|=dp[j]<<x|dp[j]>>x
      dp[j]=t if i+j else 2**c<<x
  *a,=list(map(int,(bin(dp[w-1])[2:]).zfill(c-~c)))
  print(([i for i,(f,g)in e(list(zip(a[c:],a[c::-1])))if f+g][0]))
main()
# Original solution
def p_e():
    h, w = list(map(int, input().split()))
    t = [list(map(int, input().split())) for _ in range(h)]
    m = 0
    for i in range(h):
        *b, = list(map(int, input().split()))
        for j, e in enumerate(b):
            t[i][j] = abs(t[i][j] - e)
            if m < abs(t[i][j] - e):
                m = abs(t[i][j] - e)

    # dp[i][j][k]: (i,j)でkになるかどうかbool値
    bool_width = 6400
    dp = [[[False] * bool_width for _ in range(w)] for _ in range(h)]
    for i in range(h):
        for j in range(w):
            if i == j == 0:
                dp[i][j][t[i][j]] = True
                continue
            for k in range(bool_width):
                if i > 0:
                    if abs(k + t[i][j]) < bool_width:
                        dp[i][j][k] |= dp[i - 1][j][abs(k + t[i][j])]
                    if abs(k - t[i][j]) < bool_width:
                        dp[i][j][k] |= dp[i - 1][j][abs(k - t[i][j])]
                if j > 0:
                    if abs(k + t[i][j]) < bool_width:
                        dp[i][j][k] |= dp[i][j - 1][abs(k + t[i][j])]
                    if abs(k - t[i][j]) < bool_width:
                        dp[i][j][k] |= dp[i][j - 1][abs(k - t[i][j])]
    for i, e in enumerate(dp[-1][-1]):
        if e:
            print(i)
            break
            
p_e()

# Optimized solution
def p_e():
    from itertools import product
    h, w = list(map(int, input().split()))
    t1 = [list(map(int, input().split())) for _ in range(h)]
    t2 = [list(map(int, input().split())) for _ in range(h)]

    # pythonでbitsetを用いる->boolのDPで2進数の1をTrue, 0をFalseとして扱う
    # 右にxシフト = その値にx足した値    (10001)<<4 -> (100010000)
    m = 160 * 80
    dp = [[0] * w for _ in range(h)]
    dp[0][0] = 1 << m
    for i, j in product(list(range(h)), list(range(w))):
        v = abs(t1[i][j] - t2[i][j])
        if i > 0:
            dp[i][j] |= dp[i - 1][j]
        if j > 0:
            dp[i][j] |= dp[i][j - 1]
        dp[i][j] = (dp[i][j] << v) | (dp[i][j] >> v)

    ans = m
    for i in range(m * 2):
        if dp[h - 1][w - 1] & (1 << i) > 0:
            ans = min(ans, abs(m - i))
    print(ans)
    
p_e()
# Original solution
def p_e():
    from itertools import product
    h, w = list(map(int, input().split()))
    t1 = [list(map(int, input().split())) for _ in range(h)]
    t2 = [list(map(int, input().split())) for _ in range(h)]

    # pythonでbitsetを用いる->boolのDPで2進数の1をTrue, 0をFalseとして扱う
    # 右にxシフト = その値にx足した値    (10001)<<4 -> (100010000)
    m = 160 * 80
    dp = [[0] * w for _ in range(h)]
    dp[0][0] = 1 << m
    for i, j in product(list(range(h)), list(range(w))):
        v = abs(t1[i][j] - t2[i][j])
        if i > 0:
            dp[i][j] |= dp[i - 1][j]
        if j > 0:
            dp[i][j] |= dp[i][j - 1]
        dp[i][j] = (dp[i][j] << v) | (dp[i][j] >> v)

    ans = m
    for i in range(m * 2):
        if dp[h - 1][w - 1] & (1 << i) > 0:
            ans = min(ans, abs(m - i))
    print(ans)
    
p_e()

# Optimized solution
def p_e():
    h, w = list(map(int, input().split()))
    t1 = [list(map(int, input().split())) for _ in range(h)]
    t2 = [list(map(int, input().split())) for _ in range(h)]
 
    # pythonでbitsetを用いる->boolのDPで2進数の1をTrue, 0をFalseとして扱う
    # 右にxシフト = その値にx足した値    (10001)<<4 -> (100010000)
    m = 160 * 80
    dp = [[0] * w for _ in range(h)]
    dp[0][0] = 1 << m
    for i in range(h):
        for j in range(w):
            v = abs(t1[i][j] - t2[i][j])
            if i > 0:
                dp[i][j] |= dp[i - 1][j]
            if j > 0:
                dp[i][j] |= dp[i][j - 1]
            dp[i][j] = (dp[i][j] << v) | (dp[i][j] >> v)
 
    ans = m
    for i in range(m * 2):
        if dp[h - 1][w - 1] & (1 << i) > 0:
            ans = min(ans, abs(m - i))
    print(ans)
    
p_e()