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 Python (3.8.2) (judge verdict: Time Limit Exceeded)

H,W=list(map(int,input().split()))
Ch,Cw=list(map(int,input().split()))
Dh,Dw=list(map(int,input().split()))

S=list()
G=list()
for h in range(H):
    S.append(input())

    G.append([-2 for w in range(W)])
    for w in range(W):
        if S[h][w] == '.':
            G[h][w] = -1

igraph=0
members=list()

def get_members(i,h,w):
    global members
    global G

    G[h][w] = i
    members[i].append((h,w))
    if h>0:
        if G[h-1][w] == -1:
            get_members(i,h-1,w)
    if h<H-1:
        if G[h+1][w] == -1:
            get_members(i,h+1,w)
    if w>0:
        if G[h][w-1] == -1:
            get_members(i,h,w-1)
    if w<W-1:
        if G[h][w+1] == -1:
            get_members(i,h,w+1)


for h in range(H):
    for w in range(W):
        if G[h][w] == -1:
            members.append([])
            get_members(igraph, h, w)
            igraph += 1

ngraph=igraph
# print(ngraph)
# for h in range(H):
#     print(G[h])

if ngraph == 1:
    ans = 0
    print(ans)
    exit(0)

Cgraph = G[Ch-1][Cw-1]
Dgraph = G[Dh-1][Dw-1]
if Cgraph == Dgraph:
    ans = 0
    print(ans)
    exit(0)

def check_can_warp(igraph, jgraph):
    global members

    for hi,wi in members[igraph]:
        for hj,wj in members[jgraph]:
            if abs(hi-hj) <= 2 and abs(wi-wj) <=2 :
                return 1
    return 0

conn=list()
distance=[None for i in range(ngraph)]
for igraph in range(ngraph):
    conn.append([0 for i in range(ngraph)])
    conn[igraph][igraph] = 1


for igraph in range(ngraph-1):
    for jgraph in range(ngraph):
        conn[igraph][jgraph] = check_can_warp(igraph,jgraph)
        conn[jgraph][igraph] = conn[igraph][jgraph]     

# print("conn=", conn)

def make_distance(i,d):
    global conn
    global distance
    global ngraph

    distance[i] = d

    for j in range(ngraph):
        if j != i and conn[i][j] == 1:
            if distance[j] is None or distance[j]>d+1:
                make_distance(j,d+1)




make_distance(Cgraph,0)

if distance[Dgraph] is not None:
    print(distance[Dgraph])
else:
    print(-1)