Problem:
Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2

Solution in PyPy2 (7.3.0) (judge verdict: Accepted)

from __future__ import  print_function
import sys

if sys.subversion[0] == "PyPy":
    import io, atexit

    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))

    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()
RS = raw_input
RA = lambda x=int: map(x, RS().split())
RN = lambda x=int: x(RS())


def solve():
    num = 0
    visited = set()
    stack = []
    for i in range(h):
        for j in range(w):
            if (not s[i][j] == "#") and (not i*h + j in visited):
                visited.add(i*h + j)
                who[i][j] = num
                stack.append((i,j))
                while stack:
                    cur = stack.pop()
                    who[cur[0]][cur[1]] = num
                    for nb in getnb(cur[0], cur[1]):
                        ii = nb[0]
                        jj = nb[1]
                        if (not s[ii][jj] == "#") and (not ii*h + jj in visited):
                            stack.append(nb)
                            visited.add(ii*h + jj)
                num += 1
    #
    # for row in who:
    #     print(*row)
    edges = [set() for _ in range(num)]
    for i in range(h):
        for j in range(w):
            cg = who[i][j]
            if not cg == -1:
                for a in range(0, 3):
                    for b in range(-2, 3):
                        if 0 <= i+a < h and 0<= j+b < w:
                            ng = who[i+a][j+b]
                            if ng not in (-1, cg) and (ng not in edges[cg]):
                                edges[cg].add(ng)
                                edges[ng].add(cg)
    # final bfs
    start = who[ch-1][cw-1]
    goal = who[dh-1][dw-1]
    count = 0
    stack = [start]
    visited = set()
    while stack:
        new_stack = []
        for el in stack:
            if not el in visited:
                visited.add(el)
                if el == goal:
                    print(count)
                    exit()
                new_stack.extend(list(edges[el]))
        stack = new_stack
        count += 1
    print(-1)
    exit()

    return


def getnb(i, j):
    nb = []
    if i > 0:
        nb.append((i-1, j))
    if i < h-1:
        nb.append((i+1, j))
    if j > 0:
        nb.append((i, j -1))
    if j < w-1:
        nb.append((i, j +1))
    return nb








h, w = RA()
ch, cw = RA()
dh, dw = RA()
s = [list(raw_input()) for _ in range(h)]
who = [[-1 for _ in range(w)] for _ in range(h)]
solve()