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

    for i in range(H):

        for j in range(W):

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



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

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



    for i in range(H):

        for j in range(W):

            if i == 0 and j == 0:

                continue

            s = dp[i][j]

            b = G[i][j]

            if i > 0:

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

                    s.add(k + b)

                    s.add(abs(k - b))

            if j > 0:

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

                    s.add(k + b)

                    s.add(abs(k - b))



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

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

    for i in range(H):

        for j in range(W):

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



    dp = [set() for j in range(W)]

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



    for i in range(H):

        for j in range(W):

            if i == 0 and j == 0:

                continue

            s = set()

            b = G[i][j]

            if i > 0:

                for k in dp[j]:

                    s.add(k + b)

                    s.add(abs(k - b))

            if j > 0:

                for k in dp[j - 1]:

                    s.add(k + b)

                    s.add(abs(k - b))

            dp[j] = s



    print((min(dp[W - 1])))

    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)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(G[0][0])
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                continue
            s = dp[i][j]
            b = G[i][j]
            if i > 0:
                for k in dp[i - 1][j]:
                    s.add(k + b)
                    s.add(abs(k - b))
            if j > 0:
                for k in dp[i][j - 1]:
                    s.add(k + b)
                    s.add(abs(k - b))
    print((min(dp[H - 1][W - 1])))
    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)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
    dp = [set() for j in range(W)]
    dp[0].add(G[0][0])
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                continue
            s = set()
            b = G[i][j]
            if i > 0:
                for k in dp[j]:
                    s.add(k + b)
                    s.add(abs(k - b))
            if j > 0:
                for k in dp[j - 1]:
                    s.add(k + b)
                    s.add(abs(k - b))
            dp[j] = s
    print((min(dp[W - 1])))
    return


if __name__ == "__main__":
    main()

-    dp = [[set() for j in range(W)] for i in range(H)] -    dp[0][0].add(G[0][0]) +    dp = [set() for j in range(W)] +    dp[0].add(G[0][0]) -            s = dp[i][j] +            s = set() -                for k in dp[i - 1][j]: +                for k in dp[j]: -                for k in dp[i][j - 1]: +                for k in dp[j - 1]: -    print((min(dp[H - 1][W - 1]))) +            dp[j] = s +    print((min(dp[W - 1])))