from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())

maze = [list(input()) for i in range(h)]
ans = [[-1]*w for i in range(h)]

dx=[0,1,0,-1]
dy=[1,0,-1,0]

def bfs(ans):
  ans[ch-1][cw-1]=0
  qu = deque([])
  qu.append([cw-1,ch-1])
  for i in range(cw-1-2,cw-1+3):
    for j in range(ch-1-2,ch-1+3):
      if 0<=i<w and 0<=j<h:
        if maze[j][i]=="." and (ans[j][i]>ans[ch][cw]+1 or ans[j][i]==-1):
          ans[j][i]=(ans[ch][cw]+1)
          qu.append([i,j])
  while qu:
    zahyou=qu.popleft()
    x=zahyou[0]
    y=zahyou[1]
    if x==dw-1 and y==dh-1:
      break
    count=0
    for i in range(4):
      if 0 <= x+dx[i] < w and 0 <= y+dy[i] < h and maze[y+dy[i]][x+dx[i]]=="." and (ans[y+dy[i]][x+dx[i]]>ans[y][x] or ans[y+dy[i]][x+dx[i]]==-1):

        qu.append([x+dx[i],y+dy[i]])
        ans[y+dy[i]][x+dx[i]] = ans[y][x]
        if i==0:
          for j in range(-2,3):
            if 0 <= x+dx[i]+j < w and 0 <= y+dy[i]+2 < h and maze[y+dy[i]][x+dx[i]+j]=="." and (ans[y+dy[i]+2][x+dx[i]+j]>ans[y][x]+1 or ans[y+dy[i]+2][x+dx[i]+j]==-1):
              qu.append([x+dx[i]+j,y+dy[i]+2])
              ans[y+dy[i]+2][x+dx[i]+j] = ans[y][x]+1
        elif i==2:
          for j in range(-2,3):
            if 0 <= x+dx[i]+j < w and 0 <= y+dy[i]-2 < h and maze[y+dy[i]][x+dx[i]+j]=="." and (ans[y+dy[i]-2][x+dx[i]+j]>ans[y][x]+1 or ans[y+dy[i]-2][x+dx[i]+j]==-1):
              qu.append([x+dx[i]+j,y+dy[i]-2])
              ans[y+dy[i]-2][x+dx[i]+j] = ans[y][x]+1
              
        elif i==1:
          for j in range(-2,3):
            if 0 <= x+dx[i]+2 < w and 0 <= y+dy[i]+j < h and maze[y+dy[i]][x+dx[i]+j]=="." and (ans[y+dy[i]+2][x+dx[i]+j]>ans[y][x]+1 or ans[y+dy[i]+2][x+dx[i]+j]==-1):
              qu.append([x+dx[i]+2,y+dy[i]+j])
              ans[y+dy[i]+j][x+dx[i]+2] = ans[y][x]+1
        else:
          for j in range(-2,3):
            if 0 <= x+dx[i]-2 < w and 0 <= y+dy[i]+j < h and maze[y+dy[i]+j][x+dx[i]]=="." and (ans[y+dy[i]-2][x+dx[i]+j]>ans[y][x]+1 or ans[y+dy[i]+j-2][x+dx[i]+j]==-1):
              qu.append([x+dx[i]-2,y+dy[i]+j])
              ans[y+dy[i]+j][x+dx[i]-2] = ans[y][x]+1


bfs(ans)
print(ans[dh-1][dw-1])
  

### FIXED CODE ###

from collections import deque
h,w=map(int,input().split())
ch,cw=map(int,input().split())
dh,dw=map(int,input().split())

maze = [list(input()) for i in range(h)]
ans = [[-1]*w for i in range(h)]

dx=[0,1,0,-1]
dy=[1,0,-1,0]

ans[ch-1][cw-1]=0
def bfs(ans):
  qu = deque([])
  qu.append([cw-1,ch-1])
  while qu:
    zahyou=qu.popleft()
    x=zahyou[0]
    y=zahyou[1]
    count=0
    for i in range(4):
      if 0 <= x+dx[i] < w and 0 <= y+dy[i] < h:
        if maze[y+dy[i]][x+dx[i]]=="." and (ans[y+dy[i]][x+dx[i]]>ans[y][x] or ans[y+dy[i]][x+dx[i]]==-1):
          qu.appendleft([x+dx[i],y+dy[i]])
          ans[y+dy[i]][x+dx[i]] = ans[y][x]
          count+=1
    if count!=4:
      for i in range(x-2,x+3):
        for j in range(y-2,y+3):
          if 0<=i<w and 0<=j<h:
            if maze[j][i]=="." and (ans[j][i]>ans[y][x]+1 or ans[j][i]==-1):
              ans[j][i]=(ans[y][x]+1)
              qu.append([i,j])

bfs(ans)
print(ans[dh-1][dw-1])