### BUGGY CODE ###

from collections import deque

H, W = [int(x) for x in input().split()]
Ch, Cw = [int(x) - 1 for x in input().split()]
Dh, Dw = [int(x) - 1 for x in input().split()]

maze = []
warp_count = []
for _ in range(H):
    line = list([x for x in input()])
    maze.append(line)
    warp_count.append([10**9] * W)

queue = deque()
queue.appendleft((Ch, Cw, 0))

answer = 10**9
while queue:
    now_h, now_w, use_magic = queue.pop()
    warp_count[now_h][now_w] = use_magic

    if now_h == Dh and now_w == Dw:
        if answer > use_magic:
            answer = use_magic

        continue

    # 壁じゃない場合
    if not maze[now_h][now_w] == '#':

        # 上下左右に移動
        if now_h + 1 < H:
            if warp_count[now_h + 1][now_w] > use_magic:
                queue.appendleft((now_h+1, now_w, use_magic))
        if now_h - 1 >= 0:
            if warp_count[now_h - 1][now_w] > use_magic:
                queue.appendleft((now_h-1, now_w, use_magic))
        if now_w + 1 < W:
            if warp_count[now_h][now_w + 1] > use_magic:
                queue.appendleft((now_h, now_w + 1, use_magic))
        if now_w - 1 >= 0:
            if warp_count[now_h][now_w - 1] > use_magic:
                queue.appendleft((now_h, now_w - 1, use_magic))

        for h in range(-2, 3):
            for w in range(-2, 3):
                warp_h = now_h + h
                warp_w = now_w + w

                if warp_h >= 0 and warp_h < H:
                    if warp_w >= 0 and warp_w < W:
                        if warp_count[warp_h][warp_w] > use_magic + 1:
                            queue.append((warp_h, warp_w, use_magic + 1))
                else:
                    break


if answer == 10**9:
    print(-1)
else:
    print(answer)


### FIXED CODE ###


from collections import deque


H, W = [int(x) for x in input().split()]
Ch, Cw = [int(x) - 1 for x in input().split()]
Dh, Dw = [int(x) - 1 for x in input().split()]

warp_list = [(-2, -2), (-2, -1), (-2, 0), (-2, 1), (-2, 2),
             (-1, -2), (-1, -1), (-1, 1), (-1, 2),
             (0, -2), (0, 2),
             (1, -2), (1, -1), (1, 1), (1, 2),
             (2, -2), (2, -1), (2, 0), (2, 1), (2, 2)]

maze = []
warp_count = []
for _ in range(H):
    line = list([x for x in input()])
    maze.append(line)
    warp_count.append([-1] * W)

queue = deque()
warp_queue = deque()

queue.appendleft((Ch, Cw, 0))
warp_count[Ch][Cw] = 0

answer = -1
warp_set = set()
while queue:
    def maze_check(h, w, use_magic):
        if h < 0 or H <= h:
            return False
        if w < 0 or W <= w:
            return False
        if warp_count[h][w] == -1:
            return True
        if warp_count[h][w] <= use_magic:
            return False

    def is_wall(h, w):
        if maze[h][w] == '#':
            return True
        return False

    now_h, now_w, use_magic = queue.pop()

    if now_h == Dh and now_w == Dw:
        if answer == -1:
            answer = use_magic

        elif answer > use_magic:
            answer = use_magic

        continue

    # 上下左右に移動
    for y, x in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        if maze_check(now_h + y, now_w + x, use_magic):

            # 上下左右、いずれかに壁がある場合にはワープ先に行く可能性がある
            if is_wall(now_h + y, now_w + x):
                if not (now_h, now_w) in warp_set:
                    warp_queue.appendleft((now_h, now_w, use_magic))
                    warp_set.add((now_h, now_w))
            else:
                queue.appendleft((now_h + y, now_w + x, use_magic))
                warp_count[now_h + y][now_w + x] = use_magic

    if not queue:
        while warp_queue:
            now_h, now_w, use_magic = warp_queue.pop()

            for y, x in warp_list:
                warp_h = now_h + y
                warp_w = now_w + x

                if maze_check(warp_h, warp_w, use_magic+1):
                    if not is_wall(warp_h, warp_w):
                        queue.append((warp_h, warp_w, use_magic+1))
                        warp_count[warp_h][warp_w] = use_magic + 1
        warp_set = set()

print(answer)