import sys, math

from collections import defaultdict, deque, Counter

from bisect import bisect_left, bisect_right

from itertools import combinations, permutations, product

from heapq import heappush, heappop

from functools import lru_cache

input = sys.stdin.readline

rs = lambda: input().strip()

ri = lambda: int(eval(input()))

rl = lambda: list(map(int, input().split()))

narr = lambda d, f: narr(d[:-1], lambda: [f() for _ in range(d[-1])]) if d else f() # narr((2,3), int)

mod = 1000000007

sys.setrecursionlimit(1000000)



H, W = rl()

C = rl()

D = rl()

M = []

for i in range(H):

	s = rs()

	M.append(s)



start = tuple([C[0]-1, C[1]-1])

end = tuple([D[0]-1, D[1]-1])



queue = deque()

queue.append((start, 0))

visited = narr((H, W), lambda: float('inf'))

visited[start[0]][start[1]] = 0



ds = [(1,0),(-1,0),(0,1),(0,-1)]

while queue:

	n, d = queue.popleft()

	if n == end:

		print(d)

		exit()

	i, j = n

	if visited[i][j] != d:

		continue

	nd = d

	for di, dj in ds:

		ni, nj = i+di, j+dj

		if not (0 <= ni < H and 0 <= nj < W): continue

		if M[ni][nj] == '#': continue

		if nd < visited[ni][nj]:

			queue.appendleft(((ni,nj), nd))

			visited[ni][nj] = nd

	nd = d+1

	for di in range(-2, 3):

		for dj in range(-2, 3):

			if (di, dj) == (0,0): continue

			ni, nj = i+di, j+dj

			if not (0 <= ni < H and 0 <= nj < W): continue

			if M[ni][nj] == '#': continue

			if nd < visited[ni][nj]:

				queue.append(((ni,nj), nd))

				visited[ni][nj] = nd

print((-1))

import sys, math

from collections import defaultdict, deque, Counter

from bisect import bisect_left, bisect_right

from itertools import combinations, permutations, product

from heapq import heappush, heappop

from functools import lru_cache

input = sys.stdin.readline

rs = lambda: input().strip()

ri = lambda: int(eval(input()))

rl = lambda: list(map(int, input().split()))

narr = lambda d, f: narr(d[:-1], lambda: [f() for _ in range(d[-1])]) if d else f() # narr((2,3), int)

mod = 1000000007

sys.setrecursionlimit(1000000)



H, W = rl()

C = rl()

D = rl()

M = []

for i in range(H):

	s = rs()

	M.append(s)



start = tuple([C[0]-1, C[1]-1])

end = tuple([D[0]-1, D[1]-1])



queue = deque()

queue.append((start, 0))

#visited = narr((H, W), lambda: float('inf'))

visited = [[float('inf')] * W for _ in range(H)]

visited[start[0]][start[1]] = 0



ds = [(1,0),(-1,0),(0,1),(0,-1)]

while queue:

	n, d = queue.popleft()

	if n == end:

		print(d)

		exit()

	i, j = n

	if visited[i][j] != d:

		continue

	nd = d

	for di, dj in ds:

		ni, nj = i+di, j+dj

		if not (0 <= ni < H and 0 <= nj < W): continue

		if M[ni][nj] == '#': continue

		if nd < visited[ni][nj]:

			queue.appendleft(((ni,nj), nd))

			visited[ni][nj] = nd

	nd = d+1

	for di in range(-2, 3):

		for dj in range(-2, 3):

			if (di, dj) == (0,0): continue

			ni, nj = i+di, j+dj

			if not (0 <= ni < H and 0 <= nj < W): continue

			if M[ni][nj] == '#': continue

			if nd < visited[ni][nj]:

				queue.append(((ni,nj), nd))

				visited[ni][nj] = nd

print((-1))

import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache

input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
narr = (
    lambda d, f: narr(d[:-1], lambda: [f() for _ in range(d[-1])]) if d else f()
)  # narr((2,3), int)
mod = 1000000007
sys.setrecursionlimit(1000000)
H, W = rl()
C = rl()
D = rl()
M = []
for i in range(H):
    s = rs()
    M.append(s)
start = tuple([C[0] - 1, C[1] - 1])
end = tuple([D[0] - 1, D[1] - 1])
queue = deque()
queue.append((start, 0))
visited = narr((H, W), lambda: float("inf"))
visited[start[0]][start[1]] = 0
ds = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while queue:
    n, d = queue.popleft()
    if n == end:
        print(d)
        exit()
    i, j = n
    if visited[i][j] != d:
        continue
    nd = d
    for di, dj in ds:
        ni, nj = i + di, j + dj
        if not (0 <= ni < H and 0 <= nj < W):
            continue
        if M[ni][nj] == "#":
            continue
        if nd < visited[ni][nj]:
            queue.appendleft(((ni, nj), nd))
            visited[ni][nj] = nd
    nd = d + 1
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if (di, dj) == (0, 0):
                continue
            ni, nj = i + di, j + dj
            if not (0 <= ni < H and 0 <= nj < W):
                continue
            if M[ni][nj] == "#":
                continue
            if nd < visited[ni][nj]:
                queue.append(((ni, nj), nd))
                visited[ni][nj] = nd
print((-1))

import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache

input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(eval(input()))
rl = lambda: list(map(int, input().split()))
narr = (
    lambda d, f: narr(d[:-1], lambda: [f() for _ in range(d[-1])]) if d else f()
)  # narr((2,3), int)
mod = 1000000007
sys.setrecursionlimit(1000000)
H, W = rl()
C = rl()
D = rl()
M = []
for i in range(H):
    s = rs()
    M.append(s)
start = tuple([C[0] - 1, C[1] - 1])
end = tuple([D[0] - 1, D[1] - 1])
queue = deque()
queue.append((start, 0))
# visited = narr((H, W), lambda: float('inf'))
visited = [[float("inf")] * W for _ in range(H)]
visited[start[0]][start[1]] = 0
ds = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while queue:
    n, d = queue.popleft()
    if n == end:
        print(d)
        exit()
    i, j = n
    if visited[i][j] != d:
        continue
    nd = d
    for di, dj in ds:
        ni, nj = i + di, j + dj
        if not (0 <= ni < H and 0 <= nj < W):
            continue
        if M[ni][nj] == "#":
            continue
        if nd < visited[ni][nj]:
            queue.appendleft(((ni, nj), nd))
            visited[ni][nj] = nd
    nd = d + 1
    for di in range(-2, 3):
        for dj in range(-2, 3):
            if (di, dj) == (0, 0):
                continue
            ni, nj = i + di, j + dj
            if not (0 <= ni < H and 0 <= nj < W):
                continue
            if M[ni][nj] == "#":
                continue
            if nd < visited[ni][nj]:
                queue.append(((ni, nj), nd))
                visited[ni][nj] = nd
print((-1))

-visited = narr((H, W), lambda: float("inf")) +# visited = narr((H, W), lambda: float('inf')) +visited = [[float("inf")] * W for _ in range(H)]