from heapq import heapify, heappop, heappush

import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

input = sys.stdin.readline



H, W = list(map(int, input().split()))

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

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

Ch -= 1

Cw -= 1

Dh -= 1

Dw -= 1

S = [input().rstrip() for _ in [0]*H]



INF = 10**10

D = [[INF]*W for _ in [0]*H]

used = set()



q = []

heappush(q, (0, Ch*W+Cw))





while q:

    d, ij = heappop(q)

    i, j = divmod(ij, W)



    for di in range(-2, 3):

        for dj in range(-2, 3):

            if di == dj == 0:

                continue

            I = i+di; J = j+dj

            if 0 <= I < H and 0 <= J < W and S[I][J] == '.':

                if abs(di)+abs(dj) == 1:

                    if D[I][J] <= d:

                        continue

                    D[I][J] = d

                    heappush(q, (d, I*W+J))

                else:

                    if D[I][J] <= d+1:

                        continue

                    D[I][J] = d+1

                    heappush(q, (d+1, I*W+J))



ans = D[Dh][Dw]

print((ans if ans < INF else -1))

from collections import deque



import sys

read = sys.stdin.read

readlines = sys.stdin.readlines

input = sys.stdin.readline



H, W = list(map(int, input().split()))

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

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

Ch -= 1

Cw -= 1

Dh -= 1

Dw -= 1

S = [input().rstrip() for _ in [0]*H]



INF = 10**10

D = [[INF]*W for _ in [0]*H]

used = set()



q = deque()

q.append((0, Ch*W+Cw))



while q:

    d, ij = q.popleft()

    i, j = divmod(ij, W)



    for di in range(-2, 3):

        for dj in range(-2, 3):

            if di == dj == 0:

                continue

            I = i+di

            J = j+dj

            if 0 <= I < H and 0 <= J < W and S[I][J] == '.':

                if abs(di)+abs(dj) == 1:

                    if D[I][J] <= d:

                        continue

                    D[I][J] = d

                    q.appendleft((d, I*W+J))

                else:

                    if D[I][J] <= d+1:

                        continue

                    D[I][J] = d+1

                    q.append((d+1, I*W+J))

ans = D[Dh][Dw]

print((ans if ans < INF else -1))
from heapq import heapify, heappop, heappush
import sys

read = sys.stdin.read
readlines = sys.stdin.readlines
input = sys.stdin.readline
H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
Ch -= 1
Cw -= 1
Dh -= 1
Dw -= 1
S = [input().rstrip() for _ in [0] * H]
INF = 10**10
D = [[INF] * W for _ in [0] * H]
used = set()
q = []
heappush(q, (0, Ch * W + Cw))
while q:
    d, ij = heappop(q)
    i, j = divmod(ij, W)
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if di == dj == 0:
                continue
            I = i + di
            J = j + dj
            if 0 <= I < H and 0 <= J < W and S[I][J] == ".":
                if abs(di) + abs(dj) == 1:
                    if D[I][J] <= d:
                        continue
                    D[I][J] = d
                    heappush(q, (d, I * W + J))
                else:
                    if D[I][J] <= d + 1:
                        continue
                    D[I][J] = d + 1
                    heappush(q, (d + 1, I * W + J))
ans = D[Dh][Dw]
print((ans if ans < INF else -1))

from collections import deque
import sys

read = sys.stdin.read
readlines = sys.stdin.readlines
input = sys.stdin.readline
H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
Ch -= 1
Cw -= 1
Dh -= 1
Dw -= 1
S = [input().rstrip() for _ in [0] * H]
INF = 10**10
D = [[INF] * W for _ in [0] * H]
used = set()
q = deque()
q.append((0, Ch * W + Cw))
while q:
    d, ij = q.popleft()
    i, j = divmod(ij, W)
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if di == dj == 0:
                continue
            I = i + di
            J = j + dj
            if 0 <= I < H and 0 <= J < W and S[I][J] == ".":
                if abs(di) + abs(dj) == 1:
                    if D[I][J] <= d:
                        continue
                    D[I][J] = d
                    q.appendleft((d, I * W + J))
                else:
                    if D[I][J] <= d + 1:
                        continue
                    D[I][J] = d + 1
                    q.append((d + 1, I * W + J))
ans = D[Dh][Dw]
print((ans if ans < INF else -1))

-from heapq import heapify, heappop, heappush +from collections import deque -q = [] -heappush(q, (0, Ch * W + Cw)) +q = deque() +q.append((0, Ch * W + Cw)) -    d, ij = heappop(q) +    d, ij = q.popleft() -                    heappush(q, (d, I * W + J)) +                    q.appendleft((d, I * W + J)) -                    heappush(q, (d + 1, I * W + J)) +                    q.append((d + 1, I * W + J))