import sys

input=sys.stdin.buffer.readline

inputs=sys.stdin.buffer.readlines

sys.setrecursionlimit(10**9)

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

nx,ny=list(map(int,input().split()))

gx,gy=list(map(int,input().split()))

maze=[eval(input())for i in range(h)]

from heapq import heapify, heappop, heappush

seen=set()

gx-=1;gy-=1

SIZE=h*w

hq=[(nx-1)*w+ny-1]

while hq:

    d=heappop(hq)

    c,d=divmod(d,SIZE)

    x,y=divmod(d,w)

    if w*x +y in seen:continue

    seen.add(w*x +y)

    if x==gx and y==gy:print(c);exit()

    if x and w*(x-1)+y not in seen and maze[x-1 ][y]!=35:

        heappush(hq,(c*SIZE+w*(x-1)+y))

    if y and w*x+y-1 not in seen and maze[x][y-1]!=35:

        heappush(hq,(c*SIZE+w*x+y-1))

    if x+1<h  and w*(x+1)+y not in seen and maze[x+1][y]!=35:

        heappush(hq,(c*SIZE+w*(x+1)+y))

    if y+1<w  and w*x+y+1 not in seen and maze[x][y+1]!=35:

        heappush(hq,(c*SIZE+w*x+y+1))

    for i in range(max(0,x-2),min(h,x+3)):

        for j in range(max(0,y-2),min(w,y+3)):

            if i==x and j==y:continue

            if maze[i][j]==46 and w*i + j not in seen:

                heappush(hq,(c+1)*SIZE+w*i+j)



print((-1))

import sys

input=sys.stdin.buffer.readline

inputs=sys.stdin.buffer.readlines

sys.setrecursionlimit(10**9)

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

nx,ny=list(map(int,input().split()))

gx,gy=list(map(int,input().split()))

maze=[eval(input())for i in range(h)]

#. 46 # 35

nx-=1;ny-=1;gx-=1;gy-=1

from collections import deque

dq=deque([(nx,ny)])

dq2=deque([])

T=[[float('inf')]*w for  a in range(h)]

T[nx][ny]=0

moves=((1,0),(-1,0),(0,1),(0,-1))

from itertools import product as pr

Mmoves=tuple(i for i in pr((-2,-1,0,1,2),repeat=2)if i!=(0,0) )



while dq:

    while dq:

        x,y=dq.popleft()

        for i,j in moves:

            nex,ney=x+i,y+j

            if not(0<=nex<h and 0<=ney<w) or maze[nex][ney]==35:continue

            if T[x][y]<T[nex][ney]:

                T[nex][ney]=T[x][y]

                dq.append((nex,ney))

        for i,j in Mmoves:

            nex,ney=x+i,y+j

            if not(0<=nex<h and 0<=ney<w) or maze[nex][ney]==35:continue

            if T[x][y]+1<T[nex][ney]:

                T[nex][ney]=T[x][y]+1

                dq2.append((nex,ney))

    dq,dq2=dq2,dq

if T[gx][gy]==float('inf'):print((-1))

else:print((T[gx][gy]))
import sys

input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**9)
h, w = list(map(int, input().split()))
nx, ny = list(map(int, input().split()))
gx, gy = list(map(int, input().split()))
maze = [eval(input()) for i in range(h)]
from heapq import heapify, heappop, heappush

seen = set()
gx -= 1
gy -= 1
SIZE = h * w
hq = [(nx - 1) * w + ny - 1]
while hq:
    d = heappop(hq)
    c, d = divmod(d, SIZE)
    x, y = divmod(d, w)
    if w * x + y in seen:
        continue
    seen.add(w * x + y)
    if x == gx and y == gy:
        print(c)
        exit()
    if x and w * (x - 1) + y not in seen and maze[x - 1][y] != 35:
        heappush(hq, (c * SIZE + w * (x - 1) + y))
    if y and w * x + y - 1 not in seen and maze[x][y - 1] != 35:
        heappush(hq, (c * SIZE + w * x + y - 1))
    if x + 1 < h and w * (x + 1) + y not in seen and maze[x + 1][y] != 35:
        heappush(hq, (c * SIZE + w * (x + 1) + y))
    if y + 1 < w and w * x + y + 1 not in seen and maze[x][y + 1] != 35:
        heappush(hq, (c * SIZE + w * x + y + 1))
    for i in range(max(0, x - 2), min(h, x + 3)):
        for j in range(max(0, y - 2), min(w, y + 3)):
            if i == x and j == y:
                continue
            if maze[i][j] == 46 and w * i + j not in seen:
                heappush(hq, (c + 1) * SIZE + w * i + j)
print((-1))

import sys

input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**9)
h, w = list(map(int, input().split()))
nx, ny = list(map(int, input().split()))
gx, gy = list(map(int, input().split()))
maze = [eval(input()) for i in range(h)]
# . 46 # 35
nx -= 1
ny -= 1
gx -= 1
gy -= 1
from collections import deque

dq = deque([(nx, ny)])
dq2 = deque([])
T = [[float("inf")] * w for a in range(h)]
T[nx][ny] = 0
moves = ((1, 0), (-1, 0), (0, 1), (0, -1))
from itertools import product as pr

Mmoves = tuple(i for i in pr((-2, -1, 0, 1, 2), repeat=2) if i != (0, 0))
while dq:
    while dq:
        x, y = dq.popleft()
        for i, j in moves:
            nex, ney = x + i, y + j
            if not (0 <= nex < h and 0 <= ney < w) or maze[nex][ney] == 35:
                continue
            if T[x][y] < T[nex][ney]:
                T[nex][ney] = T[x][y]
                dq.append((nex, ney))
        for i, j in Mmoves:
            nex, ney = x + i, y + j
            if not (0 <= nex < h and 0 <= ney < w) or maze[nex][ney] == 35:
                continue
            if T[x][y] + 1 < T[nex][ney]:
                T[nex][ney] = T[x][y] + 1
                dq2.append((nex, ney))
    dq, dq2 = dq2, dq
if T[gx][gy] == float("inf"):
    print((-1))
else:
    print((T[gx][gy]))

-from heapq import heapify, heappop, heappush - -seen = set() +# . 46 # 35 +nx -= 1 +ny -= 1 -SIZE = h * w -hq = [(nx - 1) * w + ny - 1] -while hq: -    d = heappop(hq) -    c, d = divmod(d, SIZE) -    x, y = divmod(d, w) -    if w * x + y in seen: -        continue -    seen.add(w * x + y) -    if x == gx and y == gy: -        print(c) -        exit() -    if x and w * (x - 1) + y not in seen and maze[x - 1][y] != 35: -        heappush(hq, (c * SIZE + w * (x - 1) + y)) -    if y and w * x + y - 1 not in seen and maze[x][y - 1] != 35: -        heappush(hq, (c * SIZE + w * x + y - 1)) -    if x + 1 < h and w * (x + 1) + y not in seen and maze[x + 1][y] != 35: -        heappush(hq, (c * SIZE + w * (x + 1) + y)) -    if y + 1 < w and w * x + y + 1 not in seen and maze[x][y + 1] != 35: -        heappush(hq, (c * SIZE + w * x + y + 1)) -    for i in range(max(0, x - 2), min(h, x + 3)): -        for j in range(max(0, y - 2), min(w, y + 3)): -            if i == x and j == y: +from collections import deque + +dq = deque([(nx, ny)]) +dq2 = deque([]) +T = [[float("inf")] * w for a in range(h)] +T[nx][ny] = 0 +moves = ((1, 0), (-1, 0), (0, 1), (0, -1)) +from itertools import product as pr + +Mmoves = tuple(i for i in pr((-2, -1, 0, 1, 2), repeat=2) if i != (0, 0)) +while dq: +    while dq: +        x, y = dq.popleft() +        for i, j in moves: +            nex, ney = x + i, y + j +            if not (0 <= nex < h and 0 <= ney < w) or maze[nex][ney] == 35: -            if maze[i][j] == 46 and w * i + j not in seen: -                heappush(hq, (c + 1) * SIZE + w * i + j) -print((-1)) +            if T[x][y] < T[nex][ney]: +                T[nex][ney] = T[x][y] +                dq.append((nex, ney)) +        for i, j in Mmoves: +            nex, ney = x + i, y + j +            if not (0 <= nex < h and 0 <= ney < w) or maze[nex][ney] == 35: +                continue +            if T[x][y] + 1 < T[nex][ney]: +                T[nex][ney] = T[x][y] + 1 +                dq2.append((nex, ney)) +    dq, dq2 = dq2, dq +if T[gx][gy] == float("inf"): +    print((-1)) +else: +    print((T[gx][gy]))