def main():

    from sys import stdin

    def input():

        return stdin.readline().strip()



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

    g = []

    for _ in range(h):

        g.append(list(map(int, input().split())))

    for i in range(h):

        k = list(map(int, input().split()))

        for j in range(w):

            g[i][j] = abs(g[i][j] - k[j])



    m = (h + w) * 80



    if m == 0:

        print((0))

        exit()



    dp = [[False] * m for _ in range(w)]

    dp[0][g[0][0]] = True

    for j in range(1, w):

        for k in range(m):

            if dp[j-1][k]:

                dp[j][abs(k - g[0][j])] = True

                dp[j][k + g[0][j]] = True

    

    for i in range(1, h):

        new_dp = [[False] * m for _ in range(w)]

        for k in range(m):

            if dp[0][k]:

                new_dp[0][abs(k - g[i][0])] = True

                new_dp[0][k + g[i][0]] = True

        

        for j in range(1, w):

            for k in range(m):

                if new_dp[j-1][k] or dp[j][k]:

                    new_dp[j][abs(k - g[i][j])] = True

                    new_dp[j][k + g[i][j]] = True

        dp = new_dp



    for j in range(m):

        if dp[w-1][j]:

            print(j)

            break



main()
def main():

    from sys import stdin

    def input():

        return stdin.readline().strip()



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

    g = []

    for _ in range(h):

        g.append(list(map(int, input().split())))

    for i in range(h):

        k = list(map(int, input().split()))

        for j in range(w):

            g[i][j] = abs(g[i][j] - k[j])



    m = (h + w) * 50



    if m == 0:

        print((0))

        exit()



    dp = [[False] * m for _ in range(w)]

    dp[0][g[0][0]] = True

    for j in range(1, w):

        for k in range(m):

            if dp[j-1][k]:

                dp[j][abs(k - g[0][j])] = True

                try:

                    dp[j][k + g[0][j]] = True

                except:

                    pass

    

    for i in range(1, h):

        new_dp = [[False] * m for _ in range(w)]

        for k in range(m):

            if dp[0][k]:

                new_dp[0][abs(k - g[i][0])] = True

                try:

                    new_dp[0][k + g[i][0]] = True

                except:

                    pass

        

        for j in range(1, w):

            for k in range(m):

                if new_dp[j-1][k] or dp[j][k]:

                    new_dp[j][abs(k - g[i][j])] = True

                    try:

                        new_dp[j][k + g[i][j]] = True

                    except:

                        pass

        dp = new_dp



    for j in range(m):

        if dp[w-1][j]:

            print(j)

            break



main()
def main():
    from sys import stdin

    def input():
        return stdin.readline().strip()

    h, w = list(map(int, input().split()))
    g = []
    for _ in range(h):
        g.append(list(map(int, input().split())))
    for i in range(h):
        k = list(map(int, input().split()))
        for j in range(w):
            g[i][j] = abs(g[i][j] - k[j])
    m = (h + w) * 80
    if m == 0:
        print((0))
        exit()
    dp = [[False] * m for _ in range(w)]
    dp[0][g[0][0]] = True
    for j in range(1, w):
        for k in range(m):
            if dp[j - 1][k]:
                dp[j][abs(k - g[0][j])] = True
                dp[j][k + g[0][j]] = True
    for i in range(1, h):
        new_dp = [[False] * m for _ in range(w)]
        for k in range(m):
            if dp[0][k]:
                new_dp[0][abs(k - g[i][0])] = True
                new_dp[0][k + g[i][0]] = True
        for j in range(1, w):
            for k in range(m):
                if new_dp[j - 1][k] or dp[j][k]:
                    new_dp[j][abs(k - g[i][j])] = True
                    new_dp[j][k + g[i][j]] = True
        dp = new_dp
    for j in range(m):
        if dp[w - 1][j]:
            print(j)
            break


main()

def main():
    from sys import stdin

    def input():
        return stdin.readline().strip()

    h, w = list(map(int, input().split()))
    g = []
    for _ in range(h):
        g.append(list(map(int, input().split())))
    for i in range(h):
        k = list(map(int, input().split()))
        for j in range(w):
            g[i][j] = abs(g[i][j] - k[j])
    m = (h + w) * 50
    if m == 0:
        print((0))
        exit()
    dp = [[False] * m for _ in range(w)]
    dp[0][g[0][0]] = True
    for j in range(1, w):
        for k in range(m):
            if dp[j - 1][k]:
                dp[j][abs(k - g[0][j])] = True
                try:
                    dp[j][k + g[0][j]] = True
                except:
                    pass
    for i in range(1, h):
        new_dp = [[False] * m for _ in range(w)]
        for k in range(m):
            if dp[0][k]:
                new_dp[0][abs(k - g[i][0])] = True
                try:
                    new_dp[0][k + g[i][0]] = True
                except:
                    pass
        for j in range(1, w):
            for k in range(m):
                if new_dp[j - 1][k] or dp[j][k]:
                    new_dp[j][abs(k - g[i][j])] = True
                    try:
                        new_dp[j][k + g[i][j]] = True
                    except:
                        pass
        dp = new_dp
    for j in range(m):
        if dp[w - 1][j]:
            print(j)
            break


main()

-    m = (h + w) * 80 +    m = (h + w) * 50 -                dp[j][k + g[0][j]] = True +                try: +                    dp[j][k + g[0][j]] = True +                except: +                    pass -                new_dp[0][k + g[i][0]] = True +                try: +                    new_dp[0][k + g[i][0]] = True +                except: +                    pass -                    new_dp[j][k + g[i][j]] = True +                    try: +                        new_dp[j][k + g[i][j]] = True +                    except: +                        pass