import sys

def input(): return sys.stdin.readline().rstrip()





def main():

    h, w = list(map(int, input().split()))

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

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

    abss = [[abs(A[i][j]-B[i][j])for j in range(w)] for i in range(h)]

    kmax = 6400

    dp = [[[0]*kmax for j in range(w)]for i in range(h)]

    dp[0][0][abss[0][0]] = 1

    for i in range(h):

        for j in range(w):

            for k in range(kmax):

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

                    if i != h-1:

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

                        if k+abss[i+1][j] < kmax:

                            dp[i+1][j][k+abss[i+1][j]] = 1

                    if j != w-1:

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

                        if k+abss[i][j+1] < kmax:

                            dp[i][j+1][k+abss[i][j+1]] = 1

    for k in range(kmax):

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

            print(k)

            break





if __name__ == '__main__':

    main()
import sys

def input(): return sys.stdin.readline().rstrip()





def main():

    h, w = list(map(int, input().split()))

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

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

    abss = [[abs(A[i][j]-B[i][j])for j in range(w)] for i in range(h)]

    dp = [[0]*w for i in range(h)]

    dp[0][0] = 1 << (abss[0][0]+13000)    

    for i in range(h):

        for j in range(w):

            if i != h-1:

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

                                                           1][j] | dp[i][j] >> abss[i+1][j]

            if j != w-1:

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

                                                              1] | dp[i][j] >> abss[i][j+1]

    a = 0

    ans = dp[-1][-1]

    while True:

        if (ans & (1 << (13000-a))):

            print(a)

            break

        elif (ans & (1 << (13000+a))):

            print(a)

            break

        a += 1





if __name__ == '__main__':

    main()
import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    h, w = list(map(int, input().split()))
    A = [list(map(int, input().split())) for i in range(h)]
    B = [list(map(int, input().split())) for i in range(h)]
    abss = [[abs(A[i][j] - B[i][j]) for j in range(w)] for i in range(h)]
    kmax = 6400
    dp = [[[0] * kmax for j in range(w)] for i in range(h)]
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h - 1:
                        dp[i + 1][j][abs(k - abss[i + 1][j])] = 1
                        if k + abss[i + 1][j] < kmax:
                            dp[i + 1][j][k + abss[i + 1][j]] = 1
                    if j != w - 1:
                        dp[i][j + 1][abs(k - abss[i][j + 1])] = 1
                        if k + abss[i][j + 1] < kmax:
                            dp[i][j + 1][k + abss[i][j + 1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == "__main__":
    main()

import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    h, w = list(map(int, input().split()))
    A = [list(map(int, input().split())) for i in range(h)]
    B = [list(map(int, input().split())) for i in range(h)]
    abss = [[abs(A[i][j] - B[i][j]) for j in range(w)] for i in range(h)]
    dp = [[0] * w for i in range(h)]
    dp[0][0] = 1 << (abss[0][0] + 13000)
    for i in range(h):
        for j in range(w):
            if i != h - 1:
                dp[i + 1][j] = (
                    dp[i + 1][j]
                    | dp[i][j] << abss[i + 1][j]
                    | dp[i][j] >> abss[i + 1][j]
                )
            if j != w - 1:
                dp[i][j + 1] = (
                    dp[i][j + 1]
                    | dp[i][j] << abss[i][j + 1]
                    | dp[i][j] >> abss[i][j + 1]
                )
    a = 0
    ans = dp[-1][-1]
    while True:
        if ans & (1 << (13000 - a)):
            print(a)
            break
        elif ans & (1 << (13000 + a)):
            print(a)
            break
        a += 1


if __name__ == "__main__":
    main()

-    kmax = 6400 -    dp = [[[0] * kmax for j in range(w)] for i in range(h)] -    dp[0][0][abss[0][0]] = 1 +    dp = [[0] * w for i in range(h)] +    dp[0][0] = 1 << (abss[0][0] + 13000) -            for k in range(kmax): -                if dp[i][j][k] == 1: -                    if i != h - 1: -                        dp[i + 1][j][abs(k - abss[i + 1][j])] = 1 -                        if k + abss[i + 1][j] < kmax: -                            dp[i + 1][j][k + abss[i + 1][j]] = 1 -                    if j != w - 1: -                        dp[i][j + 1][abs(k - abss[i][j + 1])] = 1 -                        if k + abss[i][j + 1] < kmax: -                            dp[i][j + 1][k + abss[i][j + 1]] = 1 -    for k in range(kmax): -        if dp[-1][-1][k] == 1: -            print(k) +            if i != h - 1: +                dp[i + 1][j] = ( +                    dp[i + 1][j] +                    | dp[i][j] << abss[i + 1][j] +                    | dp[i][j] >> abss[i + 1][j] +                ) +            if j != w - 1: +                dp[i][j + 1] = ( +                    dp[i][j + 1] +                    | dp[i][j] << abss[i][j + 1] +                    | dp[i][j] >> abss[i][j + 1] +                ) +    a = 0 +    ans = dp[-1][-1] +    while True: +        if ans & (1 << (13000 - a)): +            print(a) +        elif ans & (1 << (13000 + a)): +            print(a) +            break +        a += 1