from collections import*

I=9**9

(h,w),(s,t),(f,g)=eval('map(int,input().split()),'*3)

m,d=list(zip(*eval('(input(),[I]*w),'*h)))

q=deque([(s-1,t-1,1)])

while q:

 y,x,c=q.popleft()

 if d[y][x]>c:

  d[y][x]=c

  for i in range(y-2,y+3):

   for j in range(x-2,x+3):h>i>-1<j<w<I==d[i][j]!=m[i][j]>'#'!=(abs(y-i)+abs(x-j)<2!=q.appendleft((i,j,c))or q.append((i,j,c+1)))

print((d[f-1][g-1]%I-1))
from collections import*

I=9**9

(h,w),(s,t),(f,g)=eval('map(int,input().split()),'*3)

m,d=list(zip(*eval('(input(),[I]*w),'*h)))

q=deque([(s-1,t-1,1)])

while q:

 y,x,c=q.popleft()

 if d[y][x]>c:

  d[y][x]=c

  for i in range(y-2,y+3):

   for j in range(x-2,x+3):h>i>-1<j<w<d[i][j]!=m[i][j]>'#'!=(abs(y-i)+abs(x-j)<2!=q.appendleft((i,j,c))or q.append((i,j,c+1)))

print((d[f-1][g-1]%I-1))
from collections import *

I = 9**9
(h, w), (s, t), (f, g) = eval("map(int,input().split())," * 3)
m, d = list(zip(*eval("(input(),[I]*w)," * h)))
q = deque([(s - 1, t - 1, 1)])
while q:
    y, x, c = q.popleft()
    if d[y][x] > c:
        d[y][x] = c
        for i in range(y - 2, y + 3):
            for j in range(x - 2, x + 3):
                h > i > -1 < j < w < I == d[i][j] != m[i][j] > "#" != (
                    abs(y - i) + abs(x - j) < 2 != q.appendleft((i, j, c))
                    or q.append((i, j, c + 1))
                )
print((d[f - 1][g - 1] % I - 1))

from collections import *

I = 9**9
(h, w), (s, t), (f, g) = eval("map(int,input().split())," * 3)
m, d = list(zip(*eval("(input(),[I]*w)," * h)))
q = deque([(s - 1, t - 1, 1)])
while q:
    y, x, c = q.popleft()
    if d[y][x] > c:
        d[y][x] = c
        for i in range(y - 2, y + 3):
            for j in range(x - 2, x + 3):
                h > i > -1 < j < w < d[i][j] != m[i][j] > "#" != (
                    abs(y - i) + abs(x - j) < 2 != q.appendleft((i, j, c))
                    or q.append((i, j, c + 1))
                )
print((d[f - 1][g - 1] % I - 1))

-                h > i > -1 < j < w < I == d[i][j] != m[i][j] > "#" != ( +                h > i > -1 < j < w < d[i][j] != m[i][j] > "#" != (