### BUGGY 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=[]
smat.append([False]*(W+4))
smat.append([False]*(W+4))
for _ in range(H):
  line=[False,False]+[x=="." for x in input()]+[False,False]
  smat.append(line)
smat.append([False]*(W+4))
smat.append([False]*(W+4))
#print(smat)

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

    for k in range(-2,3):
      if smat[i-2][j+k]:
        graph[(i,j)].append([(i-2,j+k),1])
      if smat[i+2][j+k]:
        graph[(i,j)].append([(i+2,j+k),1])
        
    for k in range(-1,2):
      if smat[i+k][j-2]:
        graph[(i,j)].append([(i+k,j-2),1])
      if smat[i+k][j+2]:
        graph[(i,j)].append([(i+k,j+2),1])

#print(dist)
#print(prev)

dist_mat=[[-1]*(W+4) for _ in range(H+4)]
queue=[(Ch+1,Cw+1)]
d=0
while queue:
  new_queue=set()

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

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

print(-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)