### BUGGY CODE ###

from collections import deque
h, w = map(int, input().split())
ch, cw = map(int, input().split())
dh, dw = map(int, input().split())
maze = [[9]*(w+4)]
maze.append([9]*(w+4))
for i in range(h):
  s = input()
  tmp = [9,9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+4))
maze.append([9]*(w+4))
#print(maze)

pos = deque()
pos.append([ch+1,cw+1,0])
while len(pos)>0:
  x, y, hop = pos.popleft()
  #print(pos)
  #print[x,y,hop)
  if (x,y) == (dh+1,dw+1):
    print(hop)
    break
  maze[x][y] = 2
  
  if maze[x-1][y] <2:
    pos.appendleft([x-1,y,hop])
    
  if maze[x+1][y] <2:
    pos.appendleft([x+1,y,hop])
    
  if maze[x][y-1] <2:
    pos.appendleft([x,y-1,hop])
    
  if maze[x][y+1] <2:
    pos.appendleft([x,y+1,hop])
      
  if maze[x-2][y] <2:
    if [x-2, y, hop + 1] not in pos:
    	pos.append([x-2,y,hop+1])
    
  if maze[x-2][y+1] <2:
    if [x-2, y+1, hop + 1] not in pos:
    	pos.append([x-2,y+1,hop+1])
    
  if maze[x-2][y+2] <2:
    if [x-2, y+2, hop + 1] not in pos:
    	pos.append([x-2,y+2,hop+1])
    
  if maze[x-2][y-1] <2:
    if [x-2, y-1, hop + 1] not in pos:
    	pos.append([x-2,y-1,hop+1])
    
  if maze[x-2][y-2] <2:
    if [x-2, y-2, hop + 1] not in pos:
    	pos.append([x-2,y-2,hop+1])
    
  if maze[x+2][y] <2:
    if [x+2, y, hop + 1] not in pos:
    	pos.append([x+2,y,hop+1])
    
  if maze[x+2][y-1] <2:
    if [x+2, y-1, hop + 1] not in pos:
    	pos.append([x+2,y-1,hop+1])
    
  if maze[x+2][y-2] <2:
    if [x+2, y-2, hop + 1] not in pos:
    	pos.append([x+2,y-2,hop+1])
    
  if maze[x+2][y+1] <2:
    if [x+2, y+1, hop + 1] not in pos:
    	pos.append([x+2,y+1,hop+1])
    
  if maze[x+2][y+2] <2:
    if [x+2, y+2, hop + 1] not in pos:
    	pos.append([x+2,y+2,hop+1])
    
  if maze[x][y-2] <2:
    if [x, y-2, hop + 1] not in pos:
    	pos.append([x,y-2,hop+1])
    
  if maze[x+1][y-2] <2:
    if [x+1, y-2, hop + 1] not in pos:
    	pos.append([x+1,y-2,hop+1])
    
  if maze[x-1][y-2] <2:
    if [x-1, y-2, hop + 1] not in pos:
    	pos.append([x-1,y-2,hop+1])
    
  if maze[x][y+2] <2:
    if [x, y+2, hop + 1] not in pos:
    	pos.append([x,y+2,hop+1])
    
  if maze[x+1][y+2] <2:
    if [x+1, y+2, hop + 1] not in pos:
    	pos.append([x+1,y+2,hop+1])
    
  if maze[x-1][y+2] <2:
    if [x-1, y+2, hop + 1] not in pos:
    	pos.append([x-1,y+2,hop+1])
    
  if maze[x-1][y-1] <2:
    if [x-1, y-1, hop + 1] not in pos:
    	pos.append([x-1,y-1,hop+1])
    
  if maze[x-1][y+1] <2:
    if [x-1, y+1, hop + 1] not in pos:
    	pos.append([x-1,y+1,hop+1])
    
  if maze[x+1][y-1] <2:
    if [x+1, y-1, hop + 1] not in pos:
      pos.append([x+1,y-1,hop+1])
    
  if maze[x+1][y+1] <2:
    if [x+1, y+1, hop + 1] not in pos:
      pos.append([x+1,y+1,hop+1])  
    
else:
  print(-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 = [[9]*(w+4)]
maze.append([9]*(w+4))
for i in range(h):
  s = input()
  tmp = [9,9]
  for i in s:
    if i == "#":
      tmp.append(9)
    else:
      tmp.append(0)
  tmp.append(9)
  tmp.append(9)
  maze.append(tmp)
maze.append([9]*(w+4))
maze.append([9]*(w+4))
#print(maze)

pos = deque()
pos.append([ch+1,cw+1,0])
while len(pos)>0:
  x, y, hop = pos.popleft()
  #print(pos)
  #print[x,y,hop)
  if (x,y) == (dh+1,dw+1):
    print(hop)
    break
  maze[x][y] = 3
  
  if maze[x-1][y] <3:
    pos.appendleft([x-1,y,hop])
    maze[x-1][y] = 3  
    
  if maze[x+1][y] <3:
    pos.appendleft([x+1,y,hop])
    maze[x+1][y] = 3
    
  if maze[x][y-1] <3:
    pos.appendleft([x,y-1,hop])
    maze[x][y-1] = 3
    
  if maze[x][y+1] <3:
    pos.appendleft([x,y+1,hop])
    maze[x][y+1] = 3

  move =[(-2,0),(-2,1),(-2,2),(-2,-1),(-2,-2),(2,0),(2,1),(2,2),(2,-1),(2,-2),(0,-2),(-1,-2),(1,-2),(0,2),(-1,2),(1,2),(-1,1),(-1,-1),(1,1),(1,-1)]
  for dx,dy in move:
    if maze[x+dx][y+dy] <2:
      pos.append([x+dx,y+dy,hop+1])
      maze[x+dx][y+dy] = 2
  
else:
  print(-1)