from collections import deque

import sys

input=sys.stdin.readline





def solve():

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

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

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



    l = [[1 if i=="#" else 0 for i in eval(input())] for _ in range(H)]

    q = deque([(0, (Ch-1, Cw-1))])

    INF = 10**12

    costs = [[INF]*W for _ in range(H)]

    costs[Ch-1][Cw-1] = 0



    while q:

        cost, pos = q.popleft()

        h, w = pos

        for dh in range(-2, 3):

            for dw in range(-2, 3):

                if dh==0 and dw==0:

                    continue

                hh, ww = h+dh, w+dw

                if not (0<=hh<H and 0<=ww<W):

                    continue

                if l[hh][ww]:

                    continue

                

                if ((abs(dh)==1 and dw==0) or (dh==0 and abs(dw)==1)) and costs[hh][ww] > costs[h][w]:

                    costs[hh][ww] = costs[h][w]

                    q.appendleft((cost, (hh, ww)))

                elif costs[hh][ww] > costs[h][w]+1:

                    costs[hh][ww] = costs[h][w]+1

                    q.append((cost+1, (hh, ww)))



    ans = costs[Dh-1][Dw-1]

    print((ans if ans!=INF else -1))





solve()

from collections import deque



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

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

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



l = [[1 if i=="#" else 0 for i in eval(input())] for _ in range(H)]

q = deque([(0, (Ch-1, Cw-1))])

INF = 10**12

costs = [[INF]*W for _ in range(H)]

costs[Ch-1][Cw-1] = 0



while q:

    cost, pos = q.popleft()

    h, w = pos

    for dh in range(-2, 3):

        for dw in range(-2, 3):

            if dh==0 and dw==0:

                continue

            hh, ww = h+dh, w+dw

            if not (0<=hh<H and 0<=ww<W):

                continue

            if l[hh][ww]:

                continue

            

            if ((abs(dh)==1 and dw==0) or (dh==0 and abs(dw)==1)) and costs[hh][ww] > costs[h][w]:

                costs[hh][ww] = costs[h][w]

                q.appendleft((cost, (hh, ww)))

            elif costs[hh][ww] > costs[h][w]+1:

                costs[hh][ww] = costs[h][w]+1

                q.append((cost+1, (hh, ww)))



ans = costs[Dh-1][Dw-1]

print((ans if ans!=INF else -1))

from collections import deque
import sys

input = sys.stdin.readline


def solve():
    H, W = list(map(int, input().split()))
    Ch, Cw = list(map(int, input().split()))
    Dh, Dw = list(map(int, input().split()))
    l = [[1 if i == "#" else 0 for i in eval(input())] for _ in range(H)]
    q = deque([(0, (Ch - 1, Cw - 1))])
    INF = 10**12
    costs = [[INF] * W for _ in range(H)]
    costs[Ch - 1][Cw - 1] = 0
    while q:
        cost, pos = q.popleft()
        h, w = pos
        for dh in range(-2, 3):
            for dw in range(-2, 3):
                if dh == 0 and dw == 0:
                    continue
                hh, ww = h + dh, w + dw
                if not (0 <= hh < H and 0 <= ww < W):
                    continue
                if l[hh][ww]:
                    continue
                if ((abs(dh) == 1 and dw == 0) or (dh == 0 and abs(dw) == 1)) and costs[
                    hh
                ][ww] > costs[h][w]:
                    costs[hh][ww] = costs[h][w]
                    q.appendleft((cost, (hh, ww)))
                elif costs[hh][ww] > costs[h][w] + 1:
                    costs[hh][ww] = costs[h][w] + 1
                    q.append((cost + 1, (hh, ww)))
    ans = costs[Dh - 1][Dw - 1]
    print((ans if ans != INF else -1))


solve()

from collections import deque

H, W = list(map(int, input().split()))
Ch, Cw = list(map(int, input().split()))
Dh, Dw = list(map(int, input().split()))
l = [[1 if i == "#" else 0 for i in eval(input())] for _ in range(H)]
q = deque([(0, (Ch - 1, Cw - 1))])
INF = 10**12
costs = [[INF] * W for _ in range(H)]
costs[Ch - 1][Cw - 1] = 0
while q:
    cost, pos = q.popleft()
    h, w = pos
    for dh in range(-2, 3):
        for dw in range(-2, 3):
            if dh == 0 and dw == 0:
                continue
            hh, ww = h + dh, w + dw
            if not (0 <= hh < H and 0 <= ww < W):
                continue
            if l[hh][ww]:
                continue
            if ((abs(dh) == 1 and dw == 0) or (dh == 0 and abs(dw) == 1)) and costs[hh][
                ww
            ] > costs[h][w]:
                costs[hh][ww] = costs[h][w]
                q.appendleft((cost, (hh, ww)))
            elif costs[hh][ww] > costs[h][w] + 1:
                costs[hh][ww] = costs[h][w] + 1
                q.append((cost + 1, (hh, ww)))
ans = costs[Dh - 1][Dw - 1]
print((ans if ans != INF else -1))

-import sys -input = sys.stdin.readline - - -def solve(): -    H, W = list(map(int, input().split())) -    Ch, Cw = list(map(int, input().split())) -    Dh, Dw = list(map(int, input().split())) -    l = [[1 if i == "#" else 0 for i in eval(input())] for _ in range(H)] -    q = deque([(0, (Ch - 1, Cw - 1))]) -    INF = 10**12 -    costs = [[INF] * W for _ in range(H)] -    costs[Ch - 1][Cw - 1] = 0 -    while q: -        cost, pos = q.popleft() -        h, w = pos -        for dh in range(-2, 3): -            for dw in range(-2, 3): -                if dh == 0 and dw == 0: -                    continue -                hh, ww = h + dh, w + dw -                if not (0 <= hh < H and 0 <= ww < W): -                    continue -                if l[hh][ww]: -                    continue -                if ((abs(dh) == 1 and dw == 0) or (dh == 0 and abs(dw) == 1)) and costs[ -                    hh -                ][ww] > costs[h][w]: -                    costs[hh][ww] = costs[h][w] -                    q.appendleft((cost, (hh, ww))) -                elif costs[hh][ww] > costs[h][w] + 1: -                    costs[hh][ww] = costs[h][w] + 1 -                    q.append((cost + 1, (hh, ww))) -    ans = costs[Dh - 1][Dw - 1] -    print((ans if ans != INF else -1)) - - -solve() +H, W = list(map(int, input().split())) +Ch, Cw = list(map(int, input().split())) +Dh, Dw = list(map(int, input().split())) +l = [[1 if i == "#" else 0 for i in eval(input())] for _ in range(H)] +q = deque([(0, (Ch - 1, Cw - 1))]) +INF = 10**12 +costs = [[INF] * W for _ in range(H)] +costs[Ch - 1][Cw - 1] = 0 +while q: +    cost, pos = q.popleft() +    h, w = pos +    for dh in range(-2, 3): +        for dw in range(-2, 3): +            if dh == 0 and dw == 0: +                continue +            hh, ww = h + dh, w + dw +            if not (0 <= hh < H and 0 <= ww < W): +                continue +            if l[hh][ww]: +                continue +            if ((abs(dh) == 1 and dw == 0) or (dh == 0 and abs(dw) == 1)) and costs[hh][ +                ww +            ] > costs[h][w]: +                costs[hh][ww] = costs[h][w] +                q.appendleft((cost, (hh, ww))) +            elif costs[hh][ww] > costs[h][w] + 1: +                costs[hh][ww] = costs[h][w] + 1 +                q.append((cost + 1, (hh, ww))) +ans = costs[Dh - 1][Dw - 1] +print((ans if ans != INF else -1))