# Original solution
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

    lst=[[0]*w for i in range(h)]

    lst[0][0]={abs(a[0][0]-b[0][0]):1}
    # print(lst)
    d=deque([(0,0)])
    while d:
        now=d.popleft()
        tmp=list(lst[now[0]][now[1]].keys())
        if now[0]+1<h:
            ab=abs(a[now[0]+1][now[1]]-b[now[0]+1][now[1]])
            if lst[now[0]+1][now[1]]==0:
                d.append((now[0]+1,now[1]))
                lst[now[0]+1][now[1]]={}

            for i in tmp:
                if not i+ab in lst[now[0]+1][now[1]]:
                    lst[now[0]+1][now[1]][i+ab]=1
                if not abs(i-ab) in lst[now[0]+1][now[1]]:
                    lst[now[0]+1][now[1]][abs(i-ab)]=1

        # print(lst)
        if now[1]+1<w:
            ab=abs(a[now[0]][now[1]+1]-b[now[0]][now[1]+1])
            if lst[now[0]][now[1]+1]==0:
                d.append((now[0],now[1]+1))
                lst[now[0]][now[1]+1]={}

            for i in tmp:
                if not i+ab in lst[now[0]][now[1]+1]:
                    lst[now[0]][now[1]+1][i+ab]=1
                if not abs(i-ab) in lst[now[0]][now[1]+1]:
                    lst[now[0]][now[1]+1][abs(i-ab)]=1
        # print(d)
        # print(lst)
    anslst=list(lst[-1][-1].keys())
    for i in range(10**9):
        if i in anslst:
            print(i)
            break

main()

# Optimized solution
import sys

input = sys.stdin.buffer.readline

h, w = list(map(int, input().split()))
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()