### BUGGY CODE ###

from collections import deque
from itertools import product
h, w = map(int, input().split())
si, sj = map(lambda x: int(x)-1, input().split())
gi, gj = map(lambda x: int(x)-1, input().split())
fld = "".join([input() for _ in range(h)])

start = si*w+sj
goal = gi*w+gj


def generate_v2(v):
    i, j = divmod(v, w)
    if 0 < i:
        yield v-w
    if i < h-1:
        yield v+w
    if 0 < j:
        yield v-1
    if j < w-1:
        yield v+1


def bfs(i, j):
    v = i*w+j
    next_v = deque([v])
    members = set()
    while next_v:
        v = next_v.popleft()
        visited[v] = 1
        members.add(v)
        for v2 in generate_v2(v):
            if fld[v2] == '#':
                continue
            if visited[v2]:
                continue
            next_v.append(v2)
    return members


visited = [0]*(h*w)
islands = []
for i in range(h):
    for j in range(w):
        if visited[i*w+j]:
            continue
        if fld[i*w+j] == '#':
            continue
        islands.append(bfs(i, j))


n = len(islands)
edges = [[]for _ in range(n)]


def generate_v2_X(v):
    i, j = divmod(v, w)
    it = [-2, -1, 0, 1, 2]
    for di, dj in product(it, repeat=2):
        x, y = i+di, j+dj
        if x < 0 or h <= x or y < 0 or w <= y:
            continue
        yield x*w+y


# ここN**2だけど大丈夫？
for i in range(n-1):
    for j in range(i+1, n):
        # i→jは可能？
        skip = 0
        for v in islands[i]:
            if skip:
                break
            for v2 in generate_v2_X(v):
                if v2 in islands[j]:
                    edges[i].append(j)
                    edges[j].append(i)
                    skip = 1
                    break


for i in range(n):
    if start in islands[i]:
        start_island = i
    if goal in islands[i]:
        goal_island = i

INF = 10**18


def bfs(init_v):
    next_v = deque([init_v])
    dist = [INF]*n
    dist[init_v] = 0
    while next_v:
        v = next_v.popleft()
        for v2 in edges[v]:
            if dist[v2] <= dist[v]+1:
                continue
            dist[v2] = dist[v]+1
            next_v.append(v2)
    return dist


ans = bfs(start_island)[goal_island]
print(-1 if ans == INF else ans)


### FIXED CODE ###

import heapq
from itertools import product
h, w = map(int, input().split())
si, sj = map(lambda x: int(x)-1, input().split())
gi, gj = map(lambda x: int(x)-1, input().split())
fld = ''.join([input()for _ in range(h)])
INF = 10**18


def to_v(i, j):
    return i*w+j


n = h*w
start = to_v(si, sj)
goal = to_v(gi, gj)


def generate_v2(v):
    i, j = divmod(v, w)
    it = [-2, -1, 0, 1, 2]
    for di, dj in product(it, repeat=2):
        x, y = i+di, j+dj
        if x < 0 or h <= x or y < 0 or w <= y:
            continue
        if (di, dj) in [(-1, 0), (1, 0), (0, -1), (0, 1), (0, 0)]:
            d = 0
        else:
            d = 1
        yield d, x*w+y


def dijkstra(init_v):
    next_v = [(0, init_v)]
    dist = [INF]*n
    dist[init_v] = 0
    while next_v:
        d, v = heapq.heappop(next_v)
        if dist[v] < d:
            continue
        for d, v2 in generate_v2(v):
            if fld[v2] == '#':
                continue
            if dist[v2] <= dist[v]+d:
                continue
            dist[v2] = dist[v]+d
            heapq.heappush(next_v, (dist[v2], v2))
    return dist


ans = dijkstra(start)[goal]
print(-1 if ans == INF else ans)