### BUGGY CODE ###

from pprint import pprint
from collections import deque
def LI():return list(map(int,input().split()))
INF=float("inf")

h,w=LI()
ch,cw=LI()
dh,dw=LI()
maze=[list("#"*(w+2))]+[list("#"+input()+"#") for i in range(h)]+[list("#"*(w+2))]

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

q=deque()
q.append((ch,cw))
p=deque()
while q:
    x,y=q.popleft()
    visited[x][y]=True
    for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
        xn,yn=x+i,y+j
        if visited[xn][yn]:
          continue
        if maze[xn][yn]==".":
            visited[xn][yn]=True
            q.append((xn,yn))
    for i,j in ([-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],[-1,-2],[-1,-1],[-1,1],[-1,2],[0,-2],[0,2],[1,-2],[1,-1],[1,1],[1,2],[2,-2],[2,-1],[2,0],[2,1],[2,2]):
      xm,ym=x+i,y+j
      try:
        if visited[xm][ym]:
          continue
        if maze[xm][ym]=="#":
          continue
        p.append((xm,ym))
      except:pass
if visited[dh][dw]:
    print(0)
    exit()

for (px,py) in p:
    if visited[px][py]==False:
        q.append((px,py))
if len(q)==0:
    print(-1)
    exit()
for ttt in range(1,100000):
    while q:
        x,y=q.popleft()
        visited[x][y]=True
        if visited[dh][dw]:
            print(ttt)
            exit()
        for i,j in ((-1,0),(1,0),(0,1),(0,-1)):
            xn,yn=x+i,y+j
            if visited[xn][yn]:
              continue
            if maze[xn][yn]==".":
                visited[xn][yn]=True
                q.append((xn,yn))
        for i,j in ([-2,-2],[-2,-1],[-2,0],[-2,1],[-2,2],[-1,-2],[-1,-1],[-1,1],[-1,2],[0,-2],[0,2],[1,-2],[1,-1],[1,1],[1,2],[2,-2],[2,-1],[2,0],[2,1],[2,2]):
          xm,ym=x+i,y+j
          try:
            if visited[xm][ym]:
              continue
            if maze[xm][ym]=="#":
              continue
            p.append((xm,ym))
          except:pass
    
    if visited[dh][dw]==True:
        print(ttt)
        exit()
        
    for (px,py) in p:
        if visited[px][py]==False:
            q.append((px,py))
    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)