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.util.*;
import java.io.*;

  class Main {
    
    static char[][] mat;
    
    public static boolean check(int x, int y) { return (x >= 0 && x < mat.length && y>=0 && y<mat[0].length); }
    
    public static void print(int[][] dis){
        for(int i = 0; i < dis.length; i++) System.out.println(Arrays.toString(dis[i]));
    }
    
   
    
    public static void solve(int x, int y, int end_x, int end_y){
        Comparator<Node> c = Comparator.comparing(Node :: getCost);
       
        PriorityQueue<Node> q = new PriorityQueue(c);
        
        int[][] dis = new int[mat.length][mat[0].length];
        
        q.add(new Node(x,y,0));
        
        for(int i = 0; i < dis.length; i++)
            Arrays.fill(dis[i],Integer.MAX_VALUE);
    
        dis[x][y] = 0;
        
        boolean[][] visited = new boolean[mat.length][mat[0].length];
    
        
        while(!q.isEmpty()){
            Node pop = q.poll();
            visited[pop.x][pop.y] = true;
          //  System.out.println(pop.x + " " + pop.y + " -> ");
            int[] dx = {1,-1,0,0};
            int dy[] = {0,0,1,-1};
            
            // put neighbours
            for(int i = 0; i < dx.length; i++){
                int newX = pop.x + dx[i];
                int newY = pop.y + dy[i];
                if(check(newX,newY) && mat[newX][newY] == '.' && !visited[newX][newY]){
                    // add it with 0 cost
                    int newDis = 0;
                    if(dis[newX][newY] > dis[pop.x][pop.y] + 0){
                        dis[newX][newY] = dis[pop.x][pop.y] ;
                        q.add(new Node(newX,newY,dis[pop.x][pop.y]));
                    }
                }
                
            }
            
            
            /// add magic wala scene
            int[] dxm = {1,2,-1,-2,0,0,0,0,-1,-2,-1,-2,1,2,1,2};
            int[] dym = {0,0,0,0,-1,-2,1,2,-1,-2,1,2,-1,-2,1,2};
             for(int i = 0; i < dxm.length; i++){
                int newX = pop.x + dxm[i];
                int newY = pop.y + dym[i];
                if(check(newX,newY) && mat[newX][newY] == '.' && !visited[newX][newY]){
                    // add it with 0 cost
                    int newDis = 0;
                    if(dis[newX][newY] > dis[pop.x][pop.y] + 1){
                        dis[newX][newY] = dis[pop.x][pop.y] + 1;
                        q.add(new Node(newX,newY,dis[pop.x][pop.y] + 1));
                    }
                }
                
            }
            
            
            
               
        }
        if(dis[end_x][end_y] == Integer.MAX_VALUE)
            System.out.println(-1);
        else
           System.out.println(dis[end_x][end_y]);
        //print(dis);
    }

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] in = br.readLine().trim().split(" ");
        int row = Integer.parseInt(in[0]);
        int col = Integer.parseInt(in[1]);
        
        
        in = br.readLine().trim().split(" ");
        int start_x = Integer.parseInt(in[0]);
        int start_y = Integer.parseInt(in[1]);
        
        in = br.readLine().trim().split(" ");
        
        int end_x = (Integer.parseInt(in[0]));
        int end_y = (Integer.parseInt(in[1]));
      
        mat = new char[row][col];
        
        
        for(int i = 0; i < row; i++){
            mat[i] = br.readLine().trim().toCharArray();
        }
        
        solve(start_x-1,start_y-1,end_x-1,end_y-1);
    }
}



class Node {
    int x;
    int y;
    int cost;
    Node(int x, int y, int cost){
        this.x = x;
        this.y = y;
        this.cost = cost;
    }
    int getCost() {return this.cost;}
}