from collections import deque



def main():

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

    start_h, start_w = [int(x)+1 for x in input().split()]

    goal_h, goal_w = [int(x)+1 for x in input().split()]



    s = ["#"*(w+4)]

    s.append("#"*(w+4))

    for _ in range(h):

        s.append("##" + eval(input()) + "##")

    s.append("#"*(w+4))

    s.append("#"*(w+4))



    ans = [[-1]*(w+4) for _ in range(h+4)]

    for i in range(h+4):

        for j in range(w+4):

            if s[i][j] == "#":

                ans[i][j] = -2

    ans[start_h][start_w] = 0



    move1 = [(-1, 0), (1, 0), (0, -1), (0, 1)]

    move2 = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),(-1, -2), (-1, -1), (-1, 0), (-1, 1), (-1, 2),(0, -2), (0, -1), (0, 0), (0, 1), (0, 2),(1, -2), (1, -1), (1, 0), (1, 1), (1, 2),(2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]



    yet = deque([(start_h, start_w)])

    done = deque()



    while yet:

        x1, y1 = yet.popleft()

        done.append((x1, y1))

        for (p, q) in move1:

            v1, v2 = x1+p, y1+q

            if ans[v1][v2] == -1: #"."でまだ辿り着いていないマス

                yet.append((v1, v2))

                ans[v1][v2] = ans[x1][y1]



        if len(yet) == 0:

            while done:

                x2, y2 = done.popleft()

                for (p, q) in move2:

                    i, j = x2+p, y2+q

                    if ans[i][j] == -1:

                        ans[i][j] = ans[x2][y2]+1

                        yet.append((i, j))



    print((ans[goal_h][goal_w]))



if __name__ == "__main__":

    main()

from collections import deque



def main():

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

    c = list([int(x)+1 for x in input().split()])

    d = list([int(x)+1 for x in input().split()])

    s = ['#'*(w+4)]*2 + ['#'*2 + eval(input()) + '#'*2 for _ in range(h)] + ['#'*(w+4)]*2



    ans = [[-1]*(w+4) for _ in range(h+4)]

    for i in range(h+4):

        for j in range(w+4):

            if s[i][j] == '#':

                ans[i][j] = -2

    ans[c[0]][c[1]] = 0



    move1 = [(0, 1), (0, -1), (1, 0), (-1, 0)]

    move2 = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if abs(i)+abs(j) > 1]



    yet = deque([(c[0], c[1])])

    done = deque()



    while yet:

        y, x = yet.popleft()

        done.append((y, x))

        for dy, dx in move1:

            ydy, xdx = y+dy, x+dx

            if ans[ydy][xdx] == -1:

                yet.append((ydy, xdx))

                ans[ydy][xdx] = ans[y][x]



        if len(yet) == 0:

            while done:

                y, x = done.popleft()

                for dy, dx in move2:

                    ydy, xdx = y+dy, x+dx

                    if ans[ydy][xdx] == -1:

                        ans[ydy][xdx] = ans[y][x] + 1

                        yet.append((ydy, xdx))



    print((ans[d[0]][d[1]]))



if __name__ == '__main__':

    main()

from collections import deque


def main():
    h, w = list(map(int, input().split()))
    start_h, start_w = [int(x) + 1 for x in input().split()]
    goal_h, goal_w = [int(x) + 1 for x in input().split()]
    s = ["#" * (w + 4)]
    s.append("#" * (w + 4))
    for _ in range(h):
        s.append("##" + eval(input()) + "##")
    s.append("#" * (w + 4))
    s.append("#" * (w + 4))
    ans = [[-1] * (w + 4) for _ in range(h + 4)]
    for i in range(h + 4):
        for j in range(w + 4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[start_h][start_w] = 0
    move1 = [(-1, 0), (1, 0), (0, -1), (0, 1)]
    move2 = [
        (-2, -2),
        (-2, -1),
        (-2, 0),
        (-2, 1),
        (-2, 2),
        (-1, -2),
        (-1, -1),
        (-1, 0),
        (-1, 1),
        (-1, 2),
        (0, -2),
        (0, -1),
        (0, 0),
        (0, 1),
        (0, 2),
        (1, -2),
        (1, -1),
        (1, 0),
        (1, 1),
        (1, 2),
        (2, -2),
        (2, -1),
        (2, 0),
        (2, 1),
        (2, 2),
    ]
    yet = deque([(start_h, start_w)])
    done = deque()
    while yet:
        x1, y1 = yet.popleft()
        done.append((x1, y1))
        for (p, q) in move1:
            v1, v2 = x1 + p, y1 + q
            if ans[v1][v2] == -1:  # "."でまだ辿り着いていないマス
                yet.append((v1, v2))
                ans[v1][v2] = ans[x1][y1]
        if len(yet) == 0:
            while done:
                x2, y2 = done.popleft()
                for (p, q) in move2:
                    i, j = x2 + p, y2 + q
                    if ans[i][j] == -1:
                        ans[i][j] = ans[x2][y2] + 1
                        yet.append((i, j))
    print((ans[goal_h][goal_w]))


if __name__ == "__main__":
    main()

from collections import deque


def main():
    h, w = list(map(int, input().split()))
    c = list([int(x) + 1 for x in input().split()])
    d = list([int(x) + 1 for x in input().split()])
    s = (
        ["#" * (w + 4)] * 2
        + ["#" * 2 + eval(input()) + "#" * 2 for _ in range(h)]
        + ["#" * (w + 4)] * 2
    )
    ans = [[-1] * (w + 4) for _ in range(h + 4)]
    for i in range(h + 4):
        for j in range(w + 4):
            if s[i][j] == "#":
                ans[i][j] = -2
    ans[c[0]][c[1]] = 0
    move1 = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    move2 = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if abs(i) + abs(j) > 1]
    yet = deque([(c[0], c[1])])
    done = deque()
    while yet:
        y, x = yet.popleft()
        done.append((y, x))
        for dy, dx in move1:
            ydy, xdx = y + dy, x + dx
            if ans[ydy][xdx] == -1:
                yet.append((ydy, xdx))
                ans[ydy][xdx] = ans[y][x]
        if len(yet) == 0:
            while done:
                y, x = done.popleft()
                for dy, dx in move2:
                    ydy, xdx = y + dy, x + dx
                    if ans[ydy][xdx] == -1:
                        ans[ydy][xdx] = ans[y][x] + 1
                        yet.append((ydy, xdx))
    print((ans[d[0]][d[1]]))


if __name__ == "__main__":
    main()

-    start_h, start_w = [int(x) + 1 for x in input().split()] -    goal_h, goal_w = [int(x) + 1 for x in input().split()] -    s = ["#" * (w + 4)] -    s.append("#" * (w + 4)) -    for _ in range(h): -        s.append("##" + eval(input()) + "##") -    s.append("#" * (w + 4)) -    s.append("#" * (w + 4)) +    c = list([int(x) + 1 for x in input().split()]) +    d = list([int(x) + 1 for x in input().split()]) +    s = ( +        ["#" * (w + 4)] * 2 +        + ["#" * 2 + eval(input()) + "#" * 2 for _ in range(h)] +        + ["#" * (w + 4)] * 2 +    ) -    ans[start_h][start_w] = 0 -    move1 = [(-1, 0), (1, 0), (0, -1), (0, 1)] -    move2 = [ -        (-2, -2), -        (-2, -1), -        (-2, 0), -        (-2, 1), -        (-2, 2), -        (-1, -2), -        (-1, -1), -        (-1, 0), -        (-1, 1), -        (-1, 2), -        (0, -2), -        (0, -1), -        (0, 0), -        (0, 1), -        (0, 2), -        (1, -2), -        (1, -1), -        (1, 0), -        (1, 1), -        (1, 2), -        (2, -2), -        (2, -1), -        (2, 0), -        (2, 1), -        (2, 2), -    ] -    yet = deque([(start_h, start_w)]) +    ans[c[0]][c[1]] = 0 +    move1 = [(0, 1), (0, -1), (1, 0), (-1, 0)] +    move2 = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if abs(i) + abs(j) > 1] +    yet = deque([(c[0], c[1])]) -        x1, y1 = yet.popleft() -        done.append((x1, y1)) -        for (p, q) in move1: -            v1, v2 = x1 + p, y1 + q -            if ans[v1][v2] == -1:  # "."でまだ辿り着いていないマス -                yet.append((v1, v2)) -                ans[v1][v2] = ans[x1][y1] +        y, x = yet.popleft() +        done.append((y, x)) +        for dy, dx in move1: +            ydy, xdx = y + dy, x + dx +            if ans[ydy][xdx] == -1: +                yet.append((ydy, xdx)) +                ans[ydy][xdx] = ans[y][x] -                x2, y2 = done.popleft() -                for (p, q) in move2: -                    i, j = x2 + p, y2 + q -                    if ans[i][j] == -1: -                        ans[i][j] = ans[x2][y2] + 1 -                        yet.append((i, j)) -    print((ans[goal_h][goal_w])) +                y, x = done.popleft() +                for dy, dx in move2: +                    ydy, xdx = y + dy, x + dx +                    if ans[ydy][xdx] == -1: +                        ans[ydy][xdx] = ans[y][x] + 1 +                        yet.append((ydy, xdx)) +    print((ans[d[0]][d[1]]))