import sys



read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60

MOD = 1000000007





def main():

    H, W = list(map(int, readline().split()))

    A = [list(map(int, readline().split())) for _ in range(H)]

    B = [list(map(int, readline().split())) for _ in range(H)]



    G = [[0] * W for _ in range(H)]

    M = 0

    for i in range(H):

        for j in range(W):

            G[i][j] = abs(A[i][j] - B[i][j])

            if M < G[i][j]:

                M = G[i][j]



    dp = [[set() for j in range(W + 1)] for i in range(H + 1)]

    dp[0][1].add(0)

    dp[1][0].add(0)



    for i in range(H):

        for j in range(W):

            b = G[i][j]

            for k in dp[i + 1][j]:

                dp[i + 1][j + 1].add(k + b)

                dp[i + 1][j + 1].add(abs(k - b))

            for k in dp[i][j + 1]:

                dp[i + 1][j + 1].add(k + b)

                dp[i + 1][j + 1].add(abs(k - b))



    print((min(dp[H][W])))

    return





if __name__ == '__main__':

    main()

import sys



read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60

MOD = 1000000007





def main():

    H, W = list(map(int, readline().split()))

    A = [list(map(int, readline().split())) for _ in range(H)]

    B = [list(map(int, readline().split())) for _ in range(H)]



    G = [[0] * W for _ in range(H)]

    M = 0

    for i in range(H):

        for j in range(W):

            G[i][j] = abs(A[i][j] - B[i][j])

            if M < G[i][j]:

                M = G[i][j]



    dp = [[set() for j in range(W + 1)] for i in range(H + 1)]

    dp[0][1].add(0)

    dp[1][0].add(0)



    for i in range(H):

        for j in range(W):

            s = dp[i + 1][j + 1]

            b = G[i][j]

            for k in dp[i + 1][j]:

                s.add(k + b)

                s.add(abs(k - b))

            for k in dp[i][j + 1]:

                s.add(k + b)

                s.add(abs(k - b))



    print((min(dp[H][W])))

    return





if __name__ == '__main__':

    main()

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = list(map(int, readline().split()))
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]
    G = [[0] * W for _ in range(H)]
    M = 0
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]
    dp = [[set() for j in range(W + 1)] for i in range(H + 1)]
    dp[0][1].add(0)
    dp[1][0].add(0)
    for i in range(H):
        for j in range(W):
            b = G[i][j]
            for k in dp[i + 1][j]:
                dp[i + 1][j + 1].add(k + b)
                dp[i + 1][j + 1].add(abs(k - b))
            for k in dp[i][j + 1]:
                dp[i + 1][j + 1].add(k + b)
                dp[i + 1][j + 1].add(abs(k - b))
    print((min(dp[H][W])))
    return


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = list(map(int, readline().split()))
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]
    G = [[0] * W for _ in range(H)]
    M = 0
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]
    dp = [[set() for j in range(W + 1)] for i in range(H + 1)]
    dp[0][1].add(0)
    dp[1][0].add(0)
    for i in range(H):
        for j in range(W):
            s = dp[i + 1][j + 1]
            b = G[i][j]
            for k in dp[i + 1][j]:
                s.add(k + b)
                s.add(abs(k - b))
            for k in dp[i][j + 1]:
                s.add(k + b)
                s.add(abs(k - b))
    print((min(dp[H][W])))
    return


if __name__ == "__main__":
    main()

+            s = dp[i + 1][j + 1] -                dp[i + 1][j + 1].add(k + b) -                dp[i + 1][j + 1].add(abs(k - b)) +                s.add(k + b) +                s.add(abs(k - b)) -                dp[i + 1][j + 1].add(k + b) -                dp[i + 1][j + 1].add(abs(k - b)) +                s.add(k + b) +                s.add(abs(k - b))