You are given a grid with H horizontal rows and W vertical columns. Each square (i, j) on the grid has two numbers Aij and Bij written on it. Takahashi paints one of the numbers red and the other blue for each square. He then travels from square (1, 1) to square (H, W), moving only to adjacent squares. The goal is to minimize the absolute difference between the sum of red numbers and the sum of blue numbers along Takahashi's path. Find the minimum unbalancedness possible. Constraints: 2 ≤ H, W ≤ 80, 0 ≤ Aij, Bij ≤ 80. Input format: H, W, followed by the grid of Aij and Bij values. Output the minimum unbalancedness.

# Buggy version:
H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[0 for j in range(W)] for i in range(H)]
M=80*80*2+1
dp[0][0]=1<<(abs(A[0][0]-B[0][0])+M)

for i in range(H):
    for j in range(W):
        dij=abs(A[i][j]-B[i][j])
        if i>0:
            dp[i][j]|=dp[i-1][j]<<dij
            dp[i][j]|=dp[i-1][j]>>dij
        if j>0:
            dp[i][j]|=dp[i][j-1]>>dij
            dp[i][j]|=dp[i][j-1]<<dij
ans=[]
for i in range(M):
    if (1<<i)&dp[-1][-1]!=0:
        ans.append(abs(i-M))
print(min(ans))

# Fixed version:
H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[0 for j in range(W)] for i in range(H)]
M=161*161
dp[0][0]|=1<<(A[0][0]-B[0][0]+M)
dp[0][0]|=1<<(-A[0][0]+B[0][0]+M)

for i in range(H):
    for j in range(W):
        dij=abs(A[i][j]-B[i][j])
        if i>0:
            dp[i][j]|=dp[i-1][j]<<dij
            dp[i][j]|=dp[i-1][j]>>dij
        if j>0:
            dp[i][j]|=dp[i][j-1]>>dij
            dp[i][j]|=dp[i][j-1]<<dij
ans=[]
for k in range(2*M+1):
    if (1<<k)&dp[-1][-1]!=0:
        ans.append(abs(k-M))
print(min(ans))

# Buggy version:
H,W=map(int,input().split())
A,B=[],[]
for i in range(H):
  X=list(map(int, input().split()))
  A.append(X)
for i in range(H):
  X=list(map(int, input().split()))
  B.append(X)
print(A,B)
C=[]
m=0
for i in range(H):
  D=[]
  for j in range(W):
    D.append(abs(A[i][j]-B[i][j]))
    m+=abs(A[i][j]-B[i][j])
  C.append(D)
print(C)
dp=[[set() for i in range(W)] for i in range(H)]
dp[0][0].add(C[0][0])
dp[0][0].add(-C[0][0])
print(dp)
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-C[i][j]))
        dp[i][j].add(abs(k+C[i][j]))
    if j!=0:
      for k in dp[i][j-1]:
        dp[i][j].add(abs(k-C[i][j]))
        dp[i][j].add(abs(k+C[i][j]))
print(min(dp[-1][-1]))
       

# Fixed version:
H,W=map(int,input().split())
A,B=[],[]
for i in range(H):
  X=list(map(int, input().split()))
  A.append(X)
for i in range(H):
  X=list(map(int, input().split()))
  B.append(X)
C=[]
m=0
for i in range(H):
  D=[]
  for j in range(W):
    D.append(abs(A[i][j]-B[i][j]))
    m+=abs(A[i][j]-B[i][j])
  C.append(D)
dp=[[set() for i in range(W)] for i in range(H)]
dp[0][0].add(C[0][0])
dp[0][0].add(-C[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-C[i][j]))
        dp[i][j].add(abs(k+C[i][j]))
    if j!=0:
      for k in dp[i][j-1]:
        dp[i][j].add(abs(k-C[i][j]))
        dp[i][j].add(abs(k+C[i][j]))
print(min(dp[-1][-1]))

# Buggy version:
import sys
import math
import bisect
import heapq


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


def main():
    H, W = 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 = [[set() for i in range(W)] for i in range(H)]
    # dp[i][j][k]
    dp[0][0].add(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()


# Fixed version:
import sys
import math
import bisect
import heapq


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


def main():
    H, W = 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()

# Buggy version:
import numpy as np
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)]

#dp[i][j]=マス(i,j)における可能なポイントのリスト
dp=[[[] for _ in range(W)] for _ in range(H)]
dp[0][0].append(0)

for i in range(W+H-1):
  I,J=0,0
  if i < H:
    I=i
    J=0
  else:
    I=H-1
    J=i-H+1
  while I>=0 and J<W:
    sa = A[I][J] - B[I][J]
    for point in dp[I][J]:
      if I!=H-1:
        dp[I+1][J].append(point+sa)
        dp[I+1][J].append(point-sa)
      if J!=W-1:
        dp[I][J+1].append(point+sa)
        dp[I][J+1].append(point-sa)
    I-=1
    J+=1

ans=dp[H-1][W-1]
sa = A[H-1][W-1] - B[H-1][W-1]
res=float("inf")
for p in ans:
  res=min(res,abs(p+sa),abs(p-sa))
print(res)
print(dp[0][1])

# Fixed version:
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)]

#dp[i][j]=マス(i,j)における可能なポイントのリスト
dp=[[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(0)

for i in range(W+H-1):
  I,J=0,0
  if i < H:
    I=i
    J=0
  else:
    I=H-1
    J=i-H+1
  while I>=0 and J<W:
    sa = abs(A[I][J] - B[I][J])
    for point in dp[I][J]:
      if I!=H-1:
        dp[I+1][J].add(point+sa)
        dp[I+1][J].add(abs(point-sa))
      if J!=W-1:
        dp[I][J+1].add(point+sa)
        dp[I][J+1].add(abs(point-sa))
    I-=1
    J+=1

ans=dp[H-1][W-1]
sa = abs(A[H-1][W-1] - B[H-1][W-1])
res=float("inf")
for p in ans:
  res=min(res,abs(p+sa),abs(p-sa))
print(res)

# Buggy version:
# Atcoder Problem Solving
H,W=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)]
for i in range(H):
    for j in range(W):
        a[i][j]=abs(a[i][j]-b[i][j])
dp=[[0 for   i in range(W)] for j in range(H)]

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

X=80

dp[0][0]|= (1<<(X+a[0][0]))  
dp[0][0]|= (1<<(X-a[0][0]))
# 初期化の条件

for i in range(H):
    for j in range(W):
        if 0<=i-1<H and 0<=j<W:
            dd=a[i-1][j]
            dp[i][j]|=(dp[i-1][j]<<dd)
            dp[i][j]|=(dp[i-1][j]>>dd) 
            
        if 0<=i<H and 0<=j-1<W:
            d=a[i][j-1]
            dp[i][j]|=(dp[i][j-1]<<d)
            dp[i][j]|=(dp[i][j-1]>>d)
            
A=str(bin(dp[H-1][W-1]))[::-1]
L=len(A)

ans=80
for i in range(L-2):
    if A[i]=="1":
        ans=min(ans,abs(i-X))
print(ans)


# Fixed version:
# Atcoder Problem Solving
H,W=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)

# Buggy version:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])
m = max(map(max, mat))
dp = [[0b0 for i in range(w)] for k in range(h)]
dp[0][0] |= 0b1 << mat[0][0]
for y in range(h):
  for x in range(w):
        if x > 0:
          dp[y][x] |= dp[y][x-1] << mat[y][x]
          dp[y][x] |= dp[y][x-1] >> mat[y][x]
          anded = dp[y][x-1] & int('1'*(mat[y][x]+1), 2)
          reverse = format(anded, "0"+str(mat[y][x]+1)+"b")[::-1]

          dp[y][x] |= int(reverse, 2)
        if y > 0:
          dp[y][x] |= dp[y-1][x] << mat[y][x]
          dp[y][x] |= dp[y-1][x] >> mat[y][x]
          anded = dp[y-1][x] & int('1'*(mat[y][x]+1), 2)
          reverse = format(anded, "0"+str(mat[y][x]+1)+"b")[::-1]

          dp[y][x] |= int(reverse, 2)
        print(bin(dp[y][x]))

keta = 0
while dp[h-1][w-1] % 2 == 0:
  keta += 1
  dp[h-1][w-1] //= 2
print(keta)


# Fixed version:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])
m = max(map(max, mat))
dp = [[0b0 for i in range(w)] for k in range(h)]
dp[0][0] |= 0b1 << mat[0][0]
for y in range(h):
  for x in range(w):
        if x > 0:
          dp[y][x] |= dp[y][x-1] << mat[y][x]
          dp[y][x] |= dp[y][x-1] >> mat[y][x]
          anded = dp[y][x-1] & int('1'*(mat[y][x]+1), 2)
          reverse = format(anded, "0"+str(mat[y][x]+1)+"b")[::-1]

          dp[y][x] |= int(reverse, 2)
        if y > 0:
          dp[y][x] |= dp[y-1][x] << mat[y][x]
          dp[y][x] |= dp[y-1][x] >> mat[y][x]
          anded = dp[y-1][x] & int('1'*(mat[y][x]+1), 2)
          reverse = format(anded, "0"+str(mat[y][x]+1)+"b")[::-1]

          dp[y][x] |= int(reverse, 2)
        #print(bin(dp[y][x]))

keta = 0
while dp[h-1][w-1] % 2 == 0:
  keta += 1
  dp[h-1][w-1] //= 2
print(keta)

# Buggy version:
h,w=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)]

pos = 160*80
dp=[[0]*w for i in range(h)]
sub=abs(a[0][0]-b[0][0])
dp[0][0]=1<<pos

for c in range(h):
    for l in range(w):
        sub=abs(a[c][l]-b[c][l])
        if l!=0:
            dp[c][l]|=dp[c][l-1]<<sub
            dp[c][l]|=dp[c][l-1]>>sub
        if c!=0:
            dp[c][l]|=dp[c-1][l]<<sub
            dp[c][l]|=dp[c-1][l]>>sub
bit=dp[-1][-1]
for i in range(pos):
    if (bit&1<<(pos+i))or(bit&1<<(pos-i)):
        print(i)
        break

# Fixed version:
h,w=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)]

pos = 160*80
dp=[[0]*w for i in range(h)]
sub=abs(a[0][0]-b[0][0])
dp[0][0]=1<<(pos-sub)
dp[0][0]=1<<(pos+sub)

for c in range(h):
    for l in range(w):
        sub=abs(a[c][l]-b[c][l])
        if l!=0:
            dp[c][l]|=dp[c][l-1]<<sub
            dp[c][l]|=dp[c][l-1]>>sub
        if c!=0:
            dp[c][l]|=dp[c-1][l]<<sub
            dp[c][l]|=dp[c-1][l]>>sub
bit=dp[-1][-1]
for i in range(pos):
    if (bit&1<<(pos+i))or(bit&1<<(pos-i)):
        print(i)
        break

# Buggy version:
import sys
def main():
    input=sys.stdin.readline
    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)]
    """
    ofset = 80*164
    #mask = (1 << (ofset*2)) - 1
    dp=[[0 for i in range(w)] for j in range(h)]
    dp[0][0] |= 1 << (a[0][0] - b[0][0] + ofset)
    dp[0][0] |= 1 << (b[0][0] - a[0][0] + ofset)
    for i in range(h):
        for j in range(w):
            if i < h - 1:
                A = abs(a[i+1][j] - b[i+1][j])
                dp[i+1][j] |= (dp[i][j] << A)
                dp[i+1][j] |= (dp[i][j] >> A)
            if j < w - 1:
                A = abs(a[i][j+1] - b[i][j+1])
                dp[i][j+1] |= (dp[i][j] << A)
                dp[i][j+1] |= (dp[i][j] >> A)
    ans = 10000000000
    for i in range(ofset, 80*330):
        if (dp[h-1][w-1]>>i)&1:
            ans = i - ofset
            break
    for i in reversed(range(ofset)):
        if (dp[h-1][w-1]>>i)&1:
            ans = min(ans, ofset - i)
            break
    print(ans)
    """
if __name__=='__main__':
  main()


# Fixed version:
import sys
def main():
    input=sys.stdin.readline
    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)]
    ofset = 80*164
    mask = (1 << (ofset*2)) - 1
    dp=[[0 for i in range(w)] for j in range(h)]
    dp[0][0] |= 1 << (a[0][0] - b[0][0] + ofset)
    dp[0][0] |= 1 << (b[0][0] - a[0][0] + ofset)
    for i in range(h):
        for j in range(w):
            if i < h - 1:
                A = abs(a[i+1][j] - b[i+1][j])
                dp[i+1][j] |= (dp[i][j] << A) & mask
                dp[i+1][j] |= (dp[i][j] >> A) & mask
            if j < w - 1:
                A = abs(a[i][j+1] - b[i][j+1])
                dp[i][j+1] |= (dp[i][j] << A) & mask
                dp[i][j+1] |= (dp[i][j] >> A) & mask
    ans = 10000000000
    for i in range(ofset, 80*330):
        if (dp[h-1][w-1]>>i)&1: ans = min(ans, i - ofset)
    for i in range(ofset):
        if (dp[h-1][w-1]>>i)&1: ans = min(ans, ofset - i)
    print(ans)
if __name__=='__main__':
  main()

# Buggy version:
import time
t1 = time.time()
h,w = map(int,input().split())
grid = []
for i in range(h):
    grid.append(list(map(int,input().split())))
for i in range(h):
    lsti = list(map(int,input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j]-lst[j])
lst = [[set() for _ in range(w)] for _ in range(h)]
lst[0][0].add(grid[0][0])
for j in range(1,w):
    for x in lst[0][j-1]:
        lst[0][j].add(abs(grid[0][j]-x))
        if (grid[0][j] + x) <= 6400:
            lst[0][j].add(grid[0][j]+x)
for i in range(1,h):
    for x in lst[i-1][0]:
        lst[i][0].add(abs(grid[i][0]-x))
        if grid[i][0] + x <= 6400:
            lst[i][0].add(grid[i][0]+x)
for i in range(1,h):
    for j in range(1,w):
        for x in lst[i][j-1]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        for x in lst[i-1][j]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        t = time.time()
        if t - t1 > 1.8:
            print(0)
            exit()
print(min(lst[h-1][w-1]))

# Fixed version:
import time
t1 = time.time()
h,w = map(int,input().split())
grid = []
for i in range(h):
    grid.append(list(map(int,input().split())))
for i in range(h):
    lst = list(map(int,input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j]-lst[j])
lst = [[set() for _ in range(w)] for _ in range(h)]
lst[0][0].add(grid[0][0])
for j in range(1,w):
    for x in lst[0][j-1]:
        lst[0][j].add(abs(grid[0][j]-x))
        if (grid[0][j] + x) <= 6400:
            lst[0][j].add(grid[0][j]+x)
for i in range(1,h):
    for x in lst[i-1][0]:
        lst[i][0].add(abs(grid[i][0]-x))
        if grid[i][0] + x <= 6400:
            lst[i][0].add(grid[i][0]+x)
for i in range(1,h):
    for j in range(1,w):
        for x in lst[i][j-1]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        for x in lst[i-1][j]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        t = time.time()
        if t - t1 > 1.8:
            print(0)
            exit()
print(min(lst[h-1][w-1]))

# Buggy version:
import sys
#input = sys.stdin.readline

def main():
    inp = lambda: list(map(int, input().split())) 
    h,w=inp()
    a=[inp() for _ in range(h)]
    b=[inp() for _ in range(h)]
    d=[[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            d[i][j]=abs(a[i][j]-b[i][j])
    zero=80*(h+w)+2
    dp=[[0]*w for _ in range(h)]
    dp[0][0]=1<<(zero+d[0][0])|1<<(zero-d[0][0])
    for i in range(h):
        for j in range(w):
            if i==0:
                if j==0:
                    continue
                else:
                    dp[i][j]=dp[i][j-1]<<d[i][j]|dp[i][j-1]>>d[i][j]
            else:
                if j==0:
                    dp[i][j]=dp[i-1][j]<<d[i][j]|dp[i-1][j]>>d[i][j]
                else:
                    dp[i][j]=dp[i-1][j]<<d[i][j]|dp[i-1][j]>>d[i][j]
                    dp[i][j]=dp[i][j]|(dp[i][j-1]<<d[i][j]|dp[i][j-1]>>d[i][j])
    ans=dp[-1][-1]
    print(dp)
    for i in range(zero):
        if ans>>(zero+i)&1 or ans>>(zero-i)&1:
            print(i)
            exit()

if __name__ == '__main__':
    main()

# Fixed version:
import sys
#input = sys.stdin.readline

def main():
    inp = lambda: list(map(int, input().split())) 
    h,w=inp()
    a=[inp() for _ in range(h)]
    b=[inp() for _ in range(h)]
    d=[[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            d[i][j]=abs(a[i][j]-b[i][j])
    zero=80*(h+w)+2
    dp=[[0]*w for _ in range(h)]
    dp[0][0]=1<<(zero+d[0][0])|1<<(zero-d[0][0])
    for i in range(h):
        for j in range(w):
            if i==0:
                if j==0:
                    continue
                else:
                    dp[i][j]=dp[i][j-1]<<d[i][j]|dp[i][j-1]>>d[i][j]
            else:
                if j==0:
                    dp[i][j]=dp[i-1][j]<<d[i][j]|dp[i-1][j]>>d[i][j]
                else:
                    dp[i][j]=dp[i-1][j]<<d[i][j]|dp[i-1][j]>>d[i][j]
                    dp[i][j]=dp[i][j]|(dp[i][j-1]<<d[i][j]|dp[i][j-1]>>d[i][j])
    ans=dp[-1][-1]
    for i in range(zero):
        if ans>>(zero+i)&1 or ans>>(zero-i)&1:
            print(i)
            exit()

if __name__ == '__main__':
    main()

# Buggy version:
h,w = 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)]
grid = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    grid[i][j] = abs(a[i][j]-b[i][j])
dp = [[0 for j in range(w)] for k in range(h)]
buf = (h+w)*80
for i in range(h):
  for j in range(w):
      t = grid[i][j]
      if i == j == 0:
        dp[i][j] = 1 << buf+t | 1 << buf-t
      if j > 0:
        dp[i][j] = dp[i][j] | dp[i][j-1] << t | dp[i][j-1] >> t
      if i > 0:
        dp[i][j] = dp[i][j] | dp[i-1][j] << t | dp[i-1][j] >> t
print(bin(dp[h-1][w-1]))
for i in range(buf):
  if (dp[h-1][w-1] & 1 << buf+i) or (dp[h-1][w-1] & 1 << buf-i):
    print(i)
    exit()

# Fixed version:
h,w = 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)]
grid = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    grid[i][j] = abs(a[i][j]-b[i][j])
dp = [[0 for j in range(w)] for k in range(h)]
buf = (h+w)*80
for i in range(h):
  for j in range(w):
      t = grid[i][j]
      if i == j == 0:
        dp[i][j] = 1 << buf+t | 1 << buf-t
      if j > 0:
        dp[i][j] = dp[i][j] | dp[i][j-1] << t | dp[i][j-1] >> t
      if i > 0:
        dp[i][j] = dp[i][j] | dp[i-1][j] << t | dp[i-1][j] >> t
for i in range(buf):
  if (dp[h-1][w-1] & 1 << buf+i) or (dp[h-1][w-1] & 1 << buf-i):
    print(i)
    exit()

# Buggy version:
H,W = map(int,input().split())
A = np.zeros((H,W))
for c in [1,-1]:
    for i in range(H):
        num = list(map(int,input().split()))
        A[i,:] += np.array(num) * c

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):
        if i > 0:
            dp[i][j] |= dp[i-1][j]
        if j > 0:
            dp[i][j] |= dp[i][j-1]
        a = abs(A[i,j])
        dp[i][j] = (dp[i][j] << int(a)) | (dp[i][j] >> int(a))
i = M
while True:
    if (dp[H-1][W-1] >> i) & 1:
        print(i-M)
        break
    i += 1

# Fixed version:
import numpy as np

H,W = map(int,input().split())
A = np.zeros((H,W))
for c in [1,-1]:
    for i in range(H):
        num = list(map(int,input().split()))
        A[i,:] += np.array(num) * c

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):
        if i > 0:
            dp[i][j] |= dp[i-1][j]
        if j > 0:
            dp[i][j] |= dp[i][j-1]
        a = abs(A[i,j])
        dp[i][j] = (dp[i][j] << int(a)) | (dp[i][j] >> int(a))
i = M
while True:
    if (dp[H-1][W-1] >> i) & 1:
        print(i-M)
        break
    i += 1

# Buggy version:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 1000
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*(w+1) for _ in range(h+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = 0
            t |= dp[i-1][j]
            t |= dp[i][j-1]
            dp[i][j] |= ((t << x) | (t >> x))

    b = map(int, list(bin(dp[h-1][w-1])[-ofs-1:]))
    for i, s in enumerate(b):
        if(s):
            print(i)
            print(0)
            return


if __name__ == "__main__":
    resolve()


# Fixed version:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 700
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = dp[j] | dp[j-1]
            dp[j] = (t << x) | (t >> x)

    for i, s in enumerate(bin(dp[w-1])[-ofs-1:]):
        if(int(s)):
            print(i)
            return


if __name__ == "__main__":
    solve()

# Buggy version:
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)]

x = (h + w) * 80

dp = [[0] * w for _ in range(h)]
d = abs(a[0][0] - b[0][0])
dp[0][0] = 1 << (x - d)

for i in range(h):
    for j, (ae, be) in enumerate(zip(a[i], b[i])):
        if h == w == 0:
            continue

        d = abs(ae - be)
        bt = 0
        if i != 0:
            bt |= dp[i-1][j] << d
            bt |= dp[i-1][j] >> d

        if j != 0:
            bt |= dp[i][j-1] << d
            bt |= dp[i][j-1] >> d

        dp[i][j] = bt

dp = dp[-1][-1]

b = bin(dp)[2:]
can_make = [i - x for i, be in enumerate(b[::-1]) if be == "1"]

ans = min(x if x >= 0 else -x for x in can_make)
print(ans)


# Fixed version:
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)]

x = (h + w) * 80

dp = [[0] * w for _ in range(h)]
d = abs(a[0][0] - b[0][0])
dp[0][0] = 1 << (x - d)

for i in range(h):
    for j, (ae, be) in enumerate(zip(a[i], b[i])):
        if i == j == 0:
            continue

        d = abs(ae - be)
        bt = 0
        if i != 0:
            bt |= dp[i-1][j] << d
            bt |= dp[i-1][j] >> d

        if j != 0:
            bt |= dp[i][j-1] << d
            bt |= dp[i][j-1] >> d

        dp[i][j] = bt

dp = dp[-1][-1]

b = bin(dp)[2:]
can_make = [i - x for i, be in enumerate(b[::-1]) if be == "1"]

ans = min(x if x >= 0 else -x for x in can_make)
print(ans)

# Buggy version:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 6400
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = 0
            if(i-1 >= 0):
                t |= dp[i-1][j]
            if(j-1 >= 0):
                t |= dp[i][j-1]
            dp[i][j] |= ((t << x) | (t >> x))

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            write("%d\n" % i+1)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()


# Fixed version:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 6400
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

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

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            write("%d\n" % i)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()

# Buggy version:
import sys
def main():
  input = sys.stdin.readline
  h, w = 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 = [[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:
        add1 = dp[i][j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp[j+1][i].add
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()


# Fixed version:
import sys
def main():
  input = sys.stdin.readline
  h, w = 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()

# Buggy version:
R, C = map(int, input().split())

A = [list(map(int, input().split())) for _ in range(R)]
B = [list(map(int, input().split())) for _ in range(R)]

dp = [0] * R
dp[0] = s = 1 << 6400

for row_a, row_b in zip(A, B):
    for i, (a, b) in enumerate(zip(row_a, row_b)):
        d = abs(a - b)
        if i > 0:
            dp[i] |= dp[i - 1]
        dp[i] <<= d
        dp[i] >>= d

g = dp[-1]
for i in range(6401):
    if g & (s << i) or g & (s >> i):
        print(i)
        quit()

# Fixed version:
R, C = map(int, input().split())

A = [list(map(int, input().split())) for _ in range(R)]
B = [list(map(int, input().split())) for _ in range(R)]

dp = [0] * C
dp[0] = s = 1 << 6400

for row_a, row_b in zip(A, B):
    for i, (a, b) in enumerate(zip(row_a, row_b)):
        d = abs(a - b)
        if i > 0:
            dp[i] |= dp[i - 1]
        dp[i] = (dp[i] << d) | (dp[i] >> d)

g = dp[-1]
for i in range(6401):
    if g & (s << i) or g & (s >> i):
        print(i)
        quit()

# Buggy version:
import numpy as np

H, W = map(int, input().split())
lst1 = []
lst2 = []
for _ in range(H):
    lst1.append([int(i) for i in input().split()])
for _ in range(H):
    lst2.append([int(i) for i in input().split()])
arr1, arr2 = np.array(lst1), np.array(lst2)
arr = np.abs(arr1 - arr2)

d = [[0] * W for _ in range(H)]
m = 80 * (H + W)
d[0][0] = 1 << m
for h in range(H):
    for w in range(W):
        x = arr[h][w]
        if h > 0:
            print(h,w)
            d[h][w] |= d[h - 1][w]
        if w > 0:
            d[h][w] |= d[h][w - 1]
        d[h][w] = d[h][w] << x | d[h][w] >> x

x, y= 1 << m, 1 << m
for i in range(m + 1):
    if d[H - 1][W - 1] & x or d[H - 1][W - 1] & y:
        print(i)
        quit()
    x<<=1
    y>>=1

# Fixed version:
import numpy as np

H, W = map(int, input().split())
lst1 = []
lst2 = []
for _ in range(H):
    lst1.append([int(i) for i in input().split()])
for _ in range(H):
    lst2.append([int(i) for i in input().split()])
arr1, arr2 = np.array(lst1), np.array(lst2)
arr = np.abs(arr1 - arr2)

d = [[0] * W for _ in range(H)]
m = 80 * (H + W)
d[0][0] = 1 << m
for h in range(H):
    for w in range(W):
        x = arr[h][w]
        if h > 0:
            d[h][w] |= d[h - 1][w]
        if w > 0:
            d[h][w] |= d[h][w - 1]
        d[h][w] = d[h][w] << x | d[h][w] >> x

x, y= 1 << m, 1 << m
for i in range(m + 1):
    if d[H - 1][W - 1] & x or d[H - 1][W - 1] & y:
        print(i)
        quit()
    x<<=1
    y>>=1

# Buggy version:
def inpl(): return list(map(int, input().split()))

H, W = inpl()
A = [inpl() for _ in range(H)]
B = [inpl() for _ in range(W)]

DP = [[0 for _ in range(W+1)] for _ in range(H+1)]
DP[0][0] = 1 << 12800

for h in range(H):
    for w in range(W):
        d = abs(A[h][w] - B[h][w])
        DP[h+1][w] |= DP[h][w] >> d
        DP[h+1][w] |= DP[h][w] << d
        DP[h][w+1] |= DP[h][w] >> d
        DP[h][w+1] |= DP[h][w] << d
l = 1 << 12800
r = 1 << 12800
for i in range(12800):
    if (DP[-1][-2]& l) | (DP[-1][-2] & r):
        print(i)
        break
    l = l << 1
    r = r >> 1

# Fixed version:
def inpl(): return list(map(int, input().split()))

H, W = inpl()
A = [inpl() for _ in range(H)]
B = [inpl() for _ in range(H)]

DP = [[0 for _ in range(W+1)] for _ in range(H+1)]
DP[0][0] = 1 << 12800

for h in range(H):
    for w in range(W):
        d = abs(A[h][w] - B[h][w])
        DP[h+1][w] |= DP[h][w] >> d
        DP[h+1][w] |= DP[h][w] << d
        DP[h][w+1] |= DP[h][w] >> d
        DP[h][w+1] |= DP[h][w] << d
l = 1 << 12800
r = 1 << 12800
for i in range(12801):
    if (DP[-1][-2]& l) | (DP[-1][-2] & r):
        print(i)
        break
    l = l << 1
    r = r >> 1

# Buggy version:
H, W = map(int, input().split())
M = (H + W) * 80

A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]

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

for h in range(H):
    for w, (a, b) in enumerate(zip(A[h], B[h])):
        d = abs(a - b)
        if d == 0:
            continue
        mask = 0
        if h > 0:
            mask |= (dp[h - 1][w] << d)
            mask |= (dp[h - 1][w] >> d)
        if w > 0:
            mask |= (dp[h][w - 1] << d)
            mask |= (dp[h][w - 1] >> d)
        dp[h][w] = mask

state = dp[-1][-1]
nums = []
for digit, b in enumerate(bin(state)[2:][:: -1]):
    if b == '1':
        nums.append(M - digit)
ans = min(abs(n) for n in nums)
print(ans)


# Fixed version:
H, W = map(int, input().split())
M = (H + W) * 80

A = [tuple(map(int, input().split())) for _ in range(H)]
B = [tuple(map(int, input().split())) for _ in range(H)]

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

for h in range(H):
    for w, (a, b) in enumerate(zip(A[h], B[h])):
        d = abs(a - b)
        mask = 0
        if h > 0:
            mask |= (dp[h - 1][w] << d)
            mask |= (dp[h - 1][w] >> d)
        if w > 0:
            mask |= (dp[h][w - 1] << d)
            mask |= (dp[h][w - 1] >> d)
        dp[h][w] |= mask

state = dp[-1][-1]
nums = []
for digit, b in enumerate(bin(state)[2:][:: -1]):
    if b == '1':
        nums.append(digit - M)
ans = min(abs(n) for n in nums)
print(ans)

# Buggy version:
#https://atcoder.jp/contests/abc147/submissions/8874645
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    ofs=6400
    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)]
    dp=[[0]*w for _ in range(h)]
    x=abs(A[0][0]-B[0][0])
    dp[0][0]|=(1<<(ofs+x))|(1<<(ofs-x))

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

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


# Fixed version:
#https://atcoder.jp/contests/abc147/submissions/8874645
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    ofs=6400
    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)]
    dp=[[0]*w for _ in range(h)]
    x=abs(A[0][0]-B[0][0])
    dp[0][0]|=((1<<(ofs+x))|(1<<(ofs-x)))

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

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

# Buggy version:
from itertools import product

def offset(a, b):
    result = 0
    for i in range(b+1):
        if (a >> i) & 1:
            result |= (1 << b) >> i
    return result

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

# Fixed version:
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 = 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(range(H), 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)

# Buggy version:
import sys
def input():
    return sys.stdin.readline()[:-1]
h,w=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 for i in range(w)] for j in range(h)]
maxd=1
for i in range(h):
    for j in range(w):
        d[i][j] = abs(a[i][j]-b[i][j])
        maxd=max(maxd,d[i][j])
dp=[[0 for j in range(w)] for k in range(h)]
c=20000
dp[0][0] |= 1 << c+d[0][0]
for i in range(h):
    for j in range(w):
        if i+1<h:
            dp[i+1][j] |= dp[i][j] << d[i+1][j]
            dp[i+1][j] |= dp[i][j] >> d[i+1][j]
        if j+1<w:
            dp[i][j+1] |= dp[i][j] << d[i][j+1]
            dp[i][j+1] |= dp[i][j] >> d[i][j+1]
m=1000000
cnt = 0
for x in bin(dp[h-1][w-1])[2:][::-1]:
    m=min(m,abs(cnt-c))
    cnt+=1
print(m)

# Fixed version:
import sys
def input():
    return sys.stdin.readline()[:-1]
h,w=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 for i in range(w)] for j in range(h)]
maxd=1
for i in range(h):
    for j in range(w):
        d[i][j] = abs(a[i][j]-b[i][j])
        maxd=max(maxd,d[i][j])
dp=[[0 for j in range(w)] for k in range(h)]
c=20000
dp[0][0] |= 1 << c+d[0][0]
for i in range(h):
    for j in range(w):
        if i+1<h:
            dp[i+1][j] |= dp[i][j] << d[i+1][j]
            dp[i+1][j] |= dp[i][j] >> d[i+1][j]
        if j+1<w:
            dp[i][j+1] |= dp[i][j] << d[i][j+1]
            dp[i][j+1] |= dp[i][j] >> d[i][j+1]
m=1000000
cnt = 0
for x in bin(dp[h-1][w-1])[2:][::-1]:
    if x=='1':
        m=min(m,abs(cnt-c))
    cnt+=1
print(m)

# Buggy version:
H, W = [int(x) for x in input().split()] A = [[0]*W for i in range(H)] for c in [1, -1]: for i in range(H): arr = [int(x) for x in input().split()] for j in range(W): A[i][j] += arr[j] * c M = 160*80 dp = [[0]*W for i in range(H)] dp[0][0] = 1 << M for i in range(H): for j in range(W): if i > 0: dp[i][j] |= dp[i-1][j] if j > 0: dp[i][j] |= dp[i][j-1] a = abs(A[i][j]) dp[i][j] = (dp[i][j] << a) | (dp[i][j] >> a) ans = M for i in range(2*M): if dp[H-1][W-1] & (1 << i) > 0: ans = min(ans, abs(M-i)) print(ans)


# Fixed version:
H, W = [int(x) for x in input().split()]

A = [[0]*W for i in range(H)]
for c in [1, -1]:
    for i in range(H):
        arr = [int(x) for x in input().split()]
        for j in range(W):
            A[i][j] += arr[j] * c

M = 160*80
dp = [[0]*W for i in range(H)]
dp[0][0] = 1 << M
for i in range(H):
    for j in range(W):
        if i > 0:
            dp[i][j] |= dp[i-1][j]
        if j > 0:
            dp[i][j] |= dp[i][j-1]
        a = abs(A[i][j])
        dp[i][j] = (dp[i][j] << a) | (dp[i][j] >> a)

ans = M
for i in range(2*M):
    if dp[H-1][W-1] & (1 << i) > 0:
        ans = min(ans, abs(M-i))
print(ans)