def fold(x,m):
    y = 0
    for _ in range(m):
        if x & 1:
            y |= 1
        y <<= 1
        x >>= 1
    return x|y



h,w = 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
m = max(max(x) for x in g)
mask = (1<<(m*(h+w+2)//2)) - 1

dp = [[0]*(w+1) for _ in range(h+1)]
dp[0][1] = 1
dp[1][0] = 1
for i in range(1,h+1):
  for j in range(1,w+1):
    par = dp[i-1][j] | dp[i][j-1]
    dp[i][j] = mask&(par<<g[i-1][j-1])|fold(par,g[i-1][j-1])
for i in range(m*(h+w+2)//2):
  if dp[h][w]&(1<<i):
    print(i)
    break
