def main():

    # import sys

    # readline = sys.stdin.readline

    # readlines = sys.stdin.readlines

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

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

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

    M = 0

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

    for i in range(H):

        for j in range(W):

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

            grid[i][j] = m

            M = max(M, m)



    L = (H + W) * M

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

    dp[0][0][grid[0][0]] = 1



    for i in range(H):

        for j in range(W):

            for k in range(L + 1):

                if dp[i][j][k] == 1:

                    if i < H - 1:

                        nk = grid[i + 1][j]

                        dp[i + 1][j][k + nk] |= 1

                        dp[i + 1][j][abs(k - nk)] |= 1

                    if j < W - 1:

                        nk = grid[i][j + 1]

                        dp[i][j + 1][k + nk] |= 1

                        dp[i][j + 1][abs(k - nk)] |= 1



    for k in range(L + 1):

        if dp[-1][-1][k] == 1:

            print(k)

            break





if __name__ == "__main__":

    main()

def main():

    # import sys

    # readline = sys.stdin.readline

    # readlines = sys.stdin.readlines

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

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

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

    M = 0

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

    for i in range(H):

        for j in range(W):

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

            grid[i][j] = m

            M = max(M, m)



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

    dp[0][0] = (1 << grid[0][0])



    for i in range(H):

        for j in range(W):

            here = dp[i][j]

            if i < H - 1:

                nk = grid[i + 1][j]

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

                for k in range(1, nk + 1):

                    if (here >> k) & 1:

                        dp[i + 1][j] |= (1 << (nk - k))

            if j < W - 1:

                nk = grid[i][j + 1]

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

                for k in range(1, nk + 1):

                    if (here >> k) & 1:

                        dp[i][j + 1] |= (1 << (nk - k))



    k = dp[-1][-1]

    ans = 0

    while k > 0:

        if k & 1:

            print(ans)

            break

        k >>= 1

        ans += 1





if __name__ == "__main__":

    main()

def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    H, W = list(map(int, input().split()))
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    M = 0
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            m = abs(A[i][j] - B[i][j])
            grid[i][j] = m
            M = max(M, m)
    L = (H + W) * M
    dp = [[[0] * (L + 1) for _ in range(W)] for _ in range(H)]
    dp[0][0][grid[0][0]] = 1
    for i in range(H):
        for j in range(W):
            for k in range(L + 1):
                if dp[i][j][k] == 1:
                    if i < H - 1:
                        nk = grid[i + 1][j]
                        dp[i + 1][j][k + nk] |= 1
                        dp[i + 1][j][abs(k - nk)] |= 1
                    if j < W - 1:
                        nk = grid[i][j + 1]
                        dp[i][j + 1][k + nk] |= 1
                        dp[i][j + 1][abs(k - nk)] |= 1
    for k in range(L + 1):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == "__main__":
    main()

def main():
    # import sys
    # readline = sys.stdin.readline
    # readlines = sys.stdin.readlines
    H, W = list(map(int, input().split()))
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    M = 0
    grid = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            m = abs(A[i][j] - B[i][j])
            grid[i][j] = m
            M = max(M, m)
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << grid[0][0]
    for i in range(H):
        for j in range(W):
            here = dp[i][j]
            if i < H - 1:
                nk = grid[i + 1][j]
                dp[i + 1][j] |= (here << nk) | (here >> nk)
                for k in range(1, nk + 1):
                    if (here >> k) & 1:
                        dp[i + 1][j] |= 1 << (nk - k)
            if j < W - 1:
                nk = grid[i][j + 1]
                dp[i][j + 1] |= (here << nk) | (here >> nk)
                for k in range(1, nk + 1):
                    if (here >> k) & 1:
                        dp[i][j + 1] |= 1 << (nk - k)
    k = dp[-1][-1]
    ans = 0
    while k > 0:
        if k & 1:
            print(ans)
            break
        k >>= 1
        ans += 1


if __name__ == "__main__":
    main()

-    L = (H + W) * M -    dp = [[[0] * (L + 1) for _ in range(W)] for _ in range(H)] -    dp[0][0][grid[0][0]] = 1 +    dp = [[0] * W for _ in range(H)] +    dp[0][0] = 1 << grid[0][0] -            for k in range(L + 1): -                if dp[i][j][k] == 1: -                    if i < H - 1: -                        nk = grid[i + 1][j] -                        dp[i + 1][j][k + nk] |= 1 -                        dp[i + 1][j][abs(k - nk)] |= 1 -                    if j < W - 1: -                        nk = grid[i][j + 1] -                        dp[i][j + 1][k + nk] |= 1 -                        dp[i][j + 1][abs(k - nk)] |= 1 -    for k in range(L + 1): -        if dp[-1][-1][k] == 1: -            print(k) +            here = dp[i][j] +            if i < H - 1: +                nk = grid[i + 1][j] +                dp[i + 1][j] |= (here << nk) | (here >> nk) +                for k in range(1, nk + 1): +                    if (here >> k) & 1: +                        dp[i + 1][j] |= 1 << (nk - k) +            if j < W - 1: +                nk = grid[i][j + 1] +                dp[i][j + 1] |= (here << nk) | (here >> nk) +                for k in range(1, nk + 1): +                    if (here >> k) & 1: +                        dp[i][j + 1] |= 1 << (nk - k) +    k = dp[-1][-1] +    ans = 0 +    while k > 0: +        if k & 1: +            print(ans) +        k >>= 1 +        ans += 1