### BUGGY CODE ###

import sys
import re
import queue
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def isIn(H,W,x,y):
	if 0<= x and x <= H-1 and 0<=y and y<=W-1:
		return True
	else:
		return False

def main():
	H,W = i_map()

	C_h,C_w = i_map()
	D_h,D_w = i_map()

	S = []
	for i in range(0,H):
		tmp = s_input()
		tmp2 = []
		for j in range(0,W):
			tmp2.append(tmp[j])
		S.append(tmp2)

	start = [C_h-1,C_w-1]
	exit = [D_h-1,D_w-1]

	rel_matrix = [([[] for i in range(W)]) for i in range(H)]
	dir = [[-1,0],[1,0],[0,-1],[0,1]]
	for i in range(0,H):
		for j in range(0,W):
			for v_dir in dir:
				if isIn(H,W,i+v_dir[0],j+v_dir[1]):
					if(S[i][j] == '.' and S[i+v_dir[0]][j+v_dir[1]] == '.'):
						rel_matrix[i][j].append((i+v_dir[0],j+v_dir[1]))

	color = [['white' for i in range(W)] for j in range(H)]
	cost = [[-1 for i in range(W)] for j in range(H)]
	
	q =  queue.Queue()
	cost[start[0]][start[1]] = 0
	color[start[0]][start[1]]='gray'
	q.put(start)
	while(not q.empty()):
		u = q.get()
		for rel in rel_matrix[u[0]][u[1]]:
			if color[rel[0]][rel[1]] == 'white':
				cost[rel[0]][rel[1]] = 0
				color[rel[0]][rel[1]]  = 'gray'
				q.put(rel)
		color[u[0]][u[1]] = 'black'

	current_cost = 0
	while True:
		currentList = []
		for i in range(0,H):
			for j in range(0,W):
				if(cost[i][j] == current_cost):
					currentList.append((i,j))
		if(len(currentList)==0):
			break

		nextList  = []
		for kouho in currentList:
			for i in range(-2,3):
				for j in range(-2,3):
					if(kouho[0]+i >= 0 and kouho[0]+i <= H-1 and kouho[1] + j >= 0 and kouho[1] + j <= W-1):
						if(S[kouho[0]+i][kouho[1]+j]=='.' and color[kouho[0]+i][kouho[1]+j] == 'white'):
							nextList.append((kouho[0]+i,kouho[1]+j))
		nextList = set(nextList)
		for next in nextList:
			if(color[next[0]][next[1]]=='white'):
				q2 = queue.Queue()
				cost[next[0]][next[1]] =current_cost+1
				color[next[0]][next[1]] = 'gray'
				q2.put(next)
				while(not q2.empty()):
					u = q2.get()
					for rel in rel_matrix[u[0]][u[1]]:
						if color[rel[0]][rel[1]] == 'white':
							cost[rel[0]][rel[1]] = current_cost+1
							color[rel[0]][rel[1]]  = 'gray'
							q2.put(rel)
					color[u[0]][u[1]] = 'black'
		current_cost += 1


	print(cost[D_h-1][D_w-1])



if __name__ == '__main__':
	main()

### FIXED CODE ###

import sys
import re
import queue
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    H, W = i_map()
    Ch, Cw = i_map()
    Dh, Dw = i_map()
    Ch,Cw = Ch-1,Cw-1
    Dh,Dw = Dh-1,Dw-1
    maze = [input() for _ in range(H)]

    INF = 10 ** 12
    path = [[INF] * W for _ in range(H)]  # path: 各マスへの移動コスト

    # walk:移動A, warp:移動B
    walk = [(0, 1), (0, -1), (-1, 0), (1, 0)]
    warp = [(i, j) for i in range(-2, 3) for j in range(-2, 3) if (i, j) not in [(0, 0)] + walk]

    q = deque()
    path[Ch][Cw] = 0
    q.append((Ch, Cw, 0))

    while q:
        x, y, s = q.popleft()
        for dx, dy in walk:  # 移動A
            nx = x + dx
            ny = y + dy
            if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s:
                path[nx][ny] = s
                q.appendleft((nx, ny, s))  # 移動Aの場合はコスト0. キューの先頭に追加する.
        for dx, dy in warp:  # 移動B
            nx = x + dx
            ny = y + dy
            if 0 <= nx < H and 0 <= ny < W and maze[nx][ny] == '.' and path[nx][ny] > s + 1:
                path[nx][ny] = s + 1
                q.append((nx, ny, s + 1))  # 移動Bの場合はコスト1. キューの最後尾に追加する.
    
    ans = path[Dh][Dw] if path[Dh][Dw] < INF else '-1'
    print(ans)


if __name__ == '__main__':
	main()