#!/usr/bin/env python3
from collections import deque
visited = deque()
# inputで盤面と、start,goalをいれる
r,c = map(int,input().split())
s = tuple(map(int,input().split()))
g = tuple(map(int,input().split()))
s = (s[0]-1,s[1]-1)
g = (g[0]-1,g[1]-1) 
visited.append(s)
maze = []
for x in range(r):
	maze.append(input())
cost = [[-1]*c for _ in range(r)]
cost[s[0]][s[1]] = 0
flag = 0
while visited and flag == 0:
    start = visited.popleft()
    #print(start)
    for dx,dy in ((1,0),(0,-1),(0,1),(-1,0)):
        tmp = (start[0]+dx,start[1]+dy)
        if maze[start[0]+dx][start[1]+dy] == ".":
            if cost[start[0]+dx][start[1]+dy] == -1:
                cost[start[0]+dx][start[1]+dy] = cost[start[0]][start[1]] + 1
                visited.append(tmp)
                if tmp == g:
                    flag = cost[start[0]+dx][start[1]+dy]
print(flag)

	
	