### BUGGY CODE ###

H,W=map(int,input().split())
Ch,Cw=map(int,input().split())
Dh,Dw=map(int,input().split())
smat=[[x=="." for x in input()] for _ in range(H)]
#print(smat)

from collections import defaultdict
import heapq
def dijkstra_heap(s):
  #始点sから各頂点への最短距離
  dist,prev={},{}
  used=set()
  for v in vlist:
    dist[v]=float("inf")
    prev[v]=-1
  dist[s]=0
  used.add(s)

  hq_edge=[]
  for v,w in graph[s]:
    heapq.heappush(hq_edge,(w,v,s))

  while hq_edge:
    min_w,min_v,u=heapq.heappop(hq_edge)
    #まだ使われてない頂点の中から最小の距離のものを探す
    if min_v in used:
      continue

    dist[min_v]=min_w
    prev[min_v]=u
    used.add(min_v)
    for v,w in graph[min_v]:
      if not v in used:
        heapq.heappush(hq_edge,(min_w+w,v,min_v))

  return dist,prev

vlist=[]
#ここで点の集合をvlistに入れておく
for i in range(H):
  for j in range(W):
    if smat[i][j]:
      vlist.append((i,j))

graph=defaultdict(list)
#graph[i]: iから出る道の[重み,行先]の辞書
for i in range(H):
  for j in range(W):
    if not smat[i][j]:
      continue
    
    if i-1>=0 and smat[i-1][j]:
      graph[(i,j)].append([(i-1,j),0])
    if i+1<H and smat[i+1][j]:
      graph[(i,j)].append([(i+1,j),0])
    if j-1>=0 and smat[i][j-1]:
      graph[(i,j)].append([(i,j-1),0])
    if j+1<W and smat[i][j+1]:
      graph[(i,j)].append([(i,j+1),0])
    
    if i-1>=0 and j-1>=0 and smat[i-1][j-1]:
      graph[(i,j)].append([(i-1,j-1),1])
    if i-1>=0 and j+1<W and smat[i-1][j+1]:
      graph[(i,j)].append([(i-1,j+1),1])
    if i+1<H and j-1>=0 and smat[i+1][j-1]:
      graph[(i,j)].append([(i+1,j-1),1])
    if i+1<H and j+1<W and smat[i+1][j+1]:
      graph[(i,j)].append([(i+1,j+1),1])

    for k in range(-2,3):
      if i-2>=0 and 0<=j+k<W and smat[i-2][j+k]:
        graph[(i,j)].append([(i-2,j+k),1])
      if i+2<H and 0<=j+k<W and smat[i+2][j+k]:
        graph[(i,j)].append([(i+2,j+k),1])
        
    for k in range(-1,2):
      if 0<=i+k<H and j-2>=0 and smat[i+k][j-2]:
        graph[(i,j)].append([(i+k,j-2),1])
      if 0<=i+k<H and j+2<W and smat[i+k][j+2]:
        graph[(i,j)].append([(i+k,j+2),1])

dist,prev=dijkstra_heap((Ch-1,Cw-1))
#print(dist)
#print(prev)

if dist[(Dh-1,Dw-1)]==float("inf"):
  print(-1)
else:
  print(dist[(Dh-1,Dw-1)])

### FIXED CODE ###

from collections import defaultdict
import sys
H,W=map(int,input().split())
Ch,Cw=map(int,input().split())
Dh,Dw=map(int,input().split())
smat=[[x=="." for x in input()] for _ in range(H)]
#print(smat)

graph=defaultdict(list)
#graph[i]: iから出る道の[重み,行先]の辞書
for i in range(H):
  for j in range(W):
    if not smat[i][j]:
      continue
    
    for k in [-1,1]:
      if 0<=i+k<H and smat[i+k][j]:
        graph[i*W+j].append(((i+k)*W+j,0))
      if 0<=j+k<W and smat[i][j+k]:
        graph[i*W+j].append((i*W+j+k,0))

    for k1 in [-1,1]:
      for k2 in [-1,1]:
        if 0<=i+k1<H and 0<=j+k2<W and smat[i+k1][j+k2]:
          graph[i*W+j].append(((i+k1)*W+j+k2,1))

    for k in range(-2,3):
      if i-2>=0 and 0<=j+k<W and smat[i-2][j+k] :
        graph[i*W+j].append(((i-2)*W+j+k,1))
      if i+2<H and 0<=j+k<W and smat[i+2][j+k] :
        graph[i*W+j].append(((i+2)*W+j+k,1))
        
    for k in range(-1,2):
      if 0<=i+k<H and j-2>=0 and smat[i+k][j-2]:
        graph[i*W+j].append(((i+k)*W+j-2,1))
      if 0<=i+k<H and j+2<W and smat[i+k][j+2]:
        graph[i*W+j].append(((i+k)*W+j+2,1))

dist_mat=[-1]*(H*W)
queue=[(Ch-1)*W+Cw-1]
d=0
while queue:
  new_queue=set()

  while queue:
    u=queue.pop()
    if u==(Dh-1)*W+Dw-1:
      print(d)
      sys.exit(0)
    dist_mat[u]=d
    
    for v,w in graph[u]:
      if dist_mat[v]!=-1:
        continue
      if w==0:
        queue.append(v)
      else:
        new_queue.add(v)

  queue=list(new_queue)
  d+=1
#print(dist)

print(-1)