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



    M = 0

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

            if M < G[i][j]:

                M = G[i][j]



    M = 20000

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

    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])



    for i in range(H):

        for j in range(W):

            if i + 1 < H:

                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j])

            if j + 1 < W:

                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1])



    ans = INF

    for i in range(2 * M + 2):

        if dp[H - 1][W - 1] >> i & 1 and ans > abs(i - M):

            ans = abs(i - M)



    print(ans)

    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)]
    M = 0
    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])
            if M < G[i][j]:
                M = G[i][j]
    M = 20000
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])
    for i in range(H):
        for j in range(W):
            if i + 1 < H:
                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j])
            if j + 1 < W:
                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1])
    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] >> i & 1 and ans > abs(i - M):
            ans = abs(i - M)
    print(ans)
    return


if __name__ == "__main__":
    main()

+    M = 0 -    dp = [set() for j in range(W)] -    dp[0].add(G[0][0]) +            if M < G[i][j]: +                M = G[i][j] +    M = 20000 +    dp = [[0] * W for _ in range(H)] +    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0]) -            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]))) +            if i + 1 < H: +                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j]) +            if j + 1 < W: +                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1]) +    ans = INF +    for i in range(2 * M + 2): +        if dp[H - 1][W - 1] >> i & 1 and ans > abs(i - M): +            ans = abs(i - M) +    print(ans)