# -*- coding: utf-8 -*-

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

write = sys.stdout.write

# template





def solve():

    ofs = 6400

    h, w = list(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))



    b = list([s == '1' for s in list(bin(dp[w-1])[-ofs-1:-ofs+100])])

    for i, s in enumerate(b):

        if(s):

            print(i)

            return





if __name__ == "__main__":

    solve()

# -*- coding: utf-8 -*-

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

write = sys.stdout.write

# template





def solve():

    ofs = 1000

    h, w = list(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))



    b = list([s == '1' for s in list(bin(dp[w-1])[-ofs-1:-ofs+100])])

    for i, s in enumerate(b):

        if(s):

            print(i)

            return





if __name__ == "__main__":

    solve()

# -*- coding: utf-8 -*-
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template
def solve():
    ofs = 6400
    h, w = list(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)
    b = list([s == "1" for s in list(bin(dp[w - 1])[-ofs - 1 : -ofs + 100])])
    for i, s in enumerate(b):
        if s:
            print(i)
            return


if __name__ == "__main__":
    solve()

# -*- coding: utf-8 -*-
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template
def solve():
    ofs = 1000
    h, w = list(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)
    b = list([s == "1" for s in list(bin(dp[w - 1])[-ofs - 1 : -ofs + 100])])
    for i, s in enumerate(b):
        if s:
            print(i)
            return


if __name__ == "__main__":
    solve()

-    ofs = 6400 +    ofs = 1000