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 Java (OpenJDK 11.0.6) (judge verdict: Wrong Answer)

import java.io.*;
import java.util.*;
class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder();
        String s[]=bu.readLine().split(" ");
        int h=Integer.parseInt(s[0]),w=Integer.parseInt(s[1]),i,j;
        s=bu.readLine().split(" ");
        int sx=Integer.parseInt(s[0])-1,sy=Integer.parseInt(s[1])-1;
        s=bu.readLine().split(" ");
        int ex=Integer.parseInt(s[0])-1,ey=Integer.parseInt(s[1])-1;
        char g[][]=new char[h][w]; boolean v[][]=new boolean[h][w],v2[][]=new boolean[h][w];
        for(i=0;i<h;i++)
        {
            String st=bu.readLine();
            for(j=0;j<w;j++)
            g[i][j]=st.charAt(j);
        }

        Queue<Point> q=new LinkedList<>();
        q.add(new Point(sx,sy));
        v[sx][sy]=true;
        int mvs[][]={{-1,0},{0,-1},{0,1},{1,0}};
        while(!q.isEmpty())
        {
            Point p=q.poll();
            for(int[] mv:mvs)
            {
                if(!in(h,w,p.x+mv[0],p.y+mv[1])) continue;
                int xx=p.x+mv[0],yy=p.y+mv[1];
                if(g[xx][yy]=='.' && !v[xx][yy])
                {
                    v[xx][yy]=true;
                    q.add(new Point(xx,yy));
                }
            }
        }
        if(v[ex][ey]) {System.out.print("0"); return;}

        q.add(new Point(ex,ey));
        v2[ex][ey]=true;
        while(!q.isEmpty())
        {
            Point p=q.poll();
            for(int[] mv:mvs)
            {
                if(!in(h,w,p.x+mv[0],p.y+mv[1])) continue;
                int xx=p.x+mv[0],yy=p.y+mv[1];
                if(g[xx][yy]=='.' && !v2[xx][yy])
                {
                    v2[xx][yy]=true;
                    q.add(new Point(xx,yy));
                }
            }
        }

        int d[][]=new int[h][w];
        for(i=0;i<h;i++)
        for(j=0;j<w;j++)
        if(v2[i][j]) q.add(new Point(i,j));

        int ans=Integer.MAX_VALUE;
        while(!q.isEmpty())
        {
            Point p=q.poll();
            if(v[p.x][p.y]) ans=Math.min(ans,d[p.x][p.y]);
            for(i=p.x-2;i<=p.x+2;i++)
            for(j=p.y-2;j<=p.y+2;j++)
            if(in(h,w,i,j) && !v2[i][j] && g[i][j]=='.')
            {
                q.add(new Point(i,j));
                v2[i][j]=true;
                d[i][j]=d[p.x][p.y]+1;
            }
        }
        if(ans==Integer.MAX_VALUE) ans=-1;
        System.out.print(ans);
    }

    static boolean in(int n,int m,int x,int y)
    {
        if(x>=0 && x<n && y>=0 && y<m) return true;
        return false;
    }

    static class Point
    {
        int x,y;
        Point(int a,int b)
        {
            x=a;
            y=b;
        }
    }
}