from collections import deque
def LI():
    return list(map(int,input().split()))

h,w=LI()
ch,cw=LI()
# ch-=1
# cw-=1
dh,dw=LI()
# dh-=1
# dw-=1

maze=[list("#"*(w+2))]+[list("#"+input()+"#") for i in range(h)]+[list("#"*(w+2))]
# S=[input() for _ in range(h)]

visited=[[False]*(w+2) for _ in range(h+2)]

q=deque()
q.append((ch*10**4+cw))
p=deque()
while q:
    num=q.popleft()
    x,y=num//10**4,num%10**4
    visited[x][y]=True
    for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
        xn,yn=x+i,y+j
        if maze[xn][yn]=="." and visited[xn][yn]==False:
            visited[xn][yn]=True
            q.append((xn*10**4+yn))
    for i in range(-2,3,1):
        for j in range(-2,3,1):
            xm,ym=x+i,y+j
            try:
                if visited[xm][ym]==False:
                    if maze[xm][ym]==".":
                        p.append((xm*10**4+ym))
            except:pass
if visited[dh][dw]==True:
    print(0)
    exit()

for pp in p:
    x,y=pp//10**4,pp%10**4
    if visited[x][y]==False:
        q.append(pp)
if len(q)==0:
    print(-1)
    exit()
for ttt in range(1,100000):
    while q:
        num=q.popleft()
    	x,y=num//10**4,num%10**4
        visited[x][y]=True
        for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
            xn,yn=x+i,y+j
            if maze[xn][yn]=="." and visited[xn][yn]==False:
                visited[xn][yn]=True
                q.append((xn*10**4+yn))
        for i in range(-2,3,1):
            for j in range(-2,3,1):
                xm,ym=x+i,y+j
                try:
                    if visited[xm][ym]==False:
                        if maze[xm][ym]==".":
                            p.append((xm*10**4+ym))
                except:pass
    
    if visited[dh][dw]==True:
        print(ttt)
        exit()
    for pp in p:
    x,y=pp//10**4,pp%10**4
    if visited[x][y]==False:
        q.append(pp)
    if len(q)==0:
        print(-1)
        exit()
    


### FIXED CODE ###

from pprint import pprint
from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
ch-=1;cw-=1
dh,dw=map(int,input().split())
dh-=1;dw-=1
INF=float("inf")
maze=[input() for _ in range(h)]
dist=[[INF]*w for _ in range(h)]
q=deque()
p=[]
q.append((ch,cw))
dist[ch][cw]=0
cur=0
# pprint(dist)
# exit()
while q:
    while q:
        x,y=q.popleft()
        # (x,y)は到達訪問済み
        d=dist[x][y]
        for i in range(-2,3):
            for j in range(-2,3):
                # absの和が1、上下左右
                if abs(i)+abs(j)==1:
                    xx=x+i
                    yy=y+j
                    # 範囲内の時
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        # 道のとき
                        if maze[xx][yy]==".":
                            # 未到達の時
                            if dist[xx][yy]>d:
                                # 移動前のdist[x][y]と同じコスト
                                dist[xx][yy]=d
                                q.append((xx,yy))
                # ワープ 
                if abs(i)+abs(j)>=2:
                    xx=x+i
                    yy=y+j
                    if 0<=xx<=h-1 and 0<=yy<=w-1:
                        if maze[xx][yy]==".":
                            if dist[xx][yy]>d+1:
                                dist[xx][yy]=d+1
                                p.append((xx,yy))
                                
   
    cur+=1
    q=deque(p)
    p=[]
            

# pprint(dist)
ans=dist[dh][dw]
if ans==INF:
    print(-1)
else:
    print(ans)