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)



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

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



    for i in range(H):

        for j in range(W):

            here = dp[i][j]

            k = grid[i][j]



            dp[i][j] = (here << k) | (here >> k)

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

                if (here >> l) & 1:

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



            if i < H - 1:

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

            if j < W - 1:

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



    V = dp[-1][-1]

    ans = 0

    while V > 0:

        if V & 1:

            print(ans)

            break

        V >>= 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)
    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)
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << 0
    for i in range(H):
        for j in range(W):
            here = dp[i][j]
            k = grid[i][j]
            dp[i][j] = (here << k) | (here >> k)
            for l in range(1, k + 1):
                if (here >> l) & 1:
                    dp[i][j] |= 1 << (k - l)
            if i < H - 1:
                dp[i + 1][j] |= dp[i][j]
            if j < W - 1:
                dp[i][j + 1] |= dp[i][j]
    V = dp[-1][-1]
    ans = 0
    while V > 0:
        if V & 1:
            print(ans)
            break
        V >>= 1
        ans += 1


if __name__ == "__main__":
    main()

-    dp[0][0] = 1 << grid[0][0] +    dp[0][0] = 1 << 0 +            k = grid[i][j] +            dp[i][j] = (here << k) | (here >> k) +            for l in range(1, k + 1): +                if (here >> l) & 1: +                    dp[i][j] |= 1 << (k - l) -                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) +                dp[i + 1][j] |= dp[i][j] -                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] +                dp[i][j + 1] |= dp[i][j] +    V = dp[-1][-1] -    while k > 0: -        if k & 1: +    while V > 0: +        if V & 1: -        k >>= 1 +        V >>= 1