from collections import deque

from itertools import product





def bfs(x, y):

    dist = [[10 ** 9] * w for _ in range(h)]

    dist[y][x] = 0

    que = deque([(x,y)])



    while len(que) > 0:

        x, y = que.popleft()

        near_a = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]

        num = product([-2, -1, 0, 1, 2], repeat=2)

        near_b = [(x + dx, y + dy) for dx, dy in num]



        for cnt, l in enumerate((near_a, near_b)):

            for i, j in l:

                if (0 <= i < w and 0 <= j < h and

                        dist[j][i] > dist[y][x] + cnt and s[j][i] == '.'):

                    dist[j][i] = dist[y][x] + cnt

                    if cnt == 0:

                        que.appendleft((i, j))

                    elif cnt == 1:

                        que.append((i, j))

    return dist





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

Ch, Cw = list(map(int, input().split()))

Dh, Dw = list(map(int, input().split()))

s = [eval(input()) for _ in range(h)]



ans = bfs(Cw - 1, Ch - 1)[Dh - 1][Dw - 1]

print((-1 if ans == 10 ** 9 else ans))

from collections import deque

from itertools import product





def bfs(x, y):

    dist = [[10 ** 9] * w for _ in range(h)]

    dist[y][x] = 0

    for i, si in enumerate(s):

        for j, sij in enumerate(si):

            if sij == '#':

                dist[i][j] = -1

    que = deque([(x, y)])



    while len(que) > 0:

        x, y = que.popleft()

        near_a = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]

        num = product([-2, -1, 0, 1, 2], repeat=2)

        near_b = [(x + dx, y + dy) for dx, dy in num]



        for cnt, l in enumerate((near_a, near_b)):

            for i, j in l:

                if (0 <= i < w and 0 <= j < h and

                        dist[j][i] > dist[y][x] + cnt and s[j][i] == '.'):

                    dist[j][i] = dist[y][x] + cnt

                    if cnt == 0:

                        que.appendleft((i, j))

                    elif cnt == 1:

                        que.append((i, j))

    return dist





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

Ch, Cw = list(map(int, input().split()))

Dh, Dw = list(map(int, input().split()))

s = [eval(input()) for _ in range(h)]



ans = bfs(Cw - 1, Ch - 1)[Dh - 1][Dw - 1]

print((-1 if ans == 10 ** 9 else ans))

from collections import deque
from itertools import product


def bfs(x, y):
    dist = [[10**9] * w for _ in range(h)]
    dist[y][x] = 0
    que = deque([(x, y)])
    while len(que) > 0:
        x, y = que.popleft()
        near_a = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
        num = product([-2, -1, 0, 1, 2], repeat=2)
        near_b = [(x + dx, y + dy) for dx, dy in num]
        for cnt, l in enumerate((near_a, near_b)):
            for i, j in l:
                if (
                    0 <= i < w
                    and 0 <= j < h
                    and dist[j][i] > dist[y][x] + cnt
                    and s[j][i] == "."
                ):
                    dist[j][i] = dist[y][x] + cnt
                    if cnt == 0:
                        que.appendleft((i, j))
                    elif cnt == 1:
                        que.append((i, j))
    return dist


h, w = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
s = [eval(input()) for _ in range(h)]
ans = bfs(Cw - 1, Ch - 1)[Dh - 1][Dw - 1]
print((-1 if ans == 10**9 else ans))

from collections import deque
from itertools import product


def bfs(x, y):
    dist = [[10**9] * w for _ in range(h)]
    dist[y][x] = 0
    for i, si in enumerate(s):
        for j, sij in enumerate(si):
            if sij == "#":
                dist[i][j] = -1
    que = deque([(x, y)])
    while len(que) > 0:
        x, y = que.popleft()
        near_a = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
        num = product([-2, -1, 0, 1, 2], repeat=2)
        near_b = [(x + dx, y + dy) for dx, dy in num]
        for cnt, l in enumerate((near_a, near_b)):
            for i, j in l:
                if (
                    0 <= i < w
                    and 0 <= j < h
                    and dist[j][i] > dist[y][x] + cnt
                    and s[j][i] == "."
                ):
                    dist[j][i] = dist[y][x] + cnt
                    if cnt == 0:
                        que.appendleft((i, j))
                    elif cnt == 1:
                        que.append((i, j))
    return dist


h, w = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
s = [eval(input()) for _ in range(h)]
ans = bfs(Cw - 1, Ch - 1)[Dh - 1][Dw - 1]
print((-1 if ans == 10**9 else ans))

+    for i, si in enumerate(s): +        for j, sij in enumerate(si): +            if sij == "#": +                dist[i][j] = -1