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: Accepted)

import java.util.*;

public class Main {
    static int INF = 1001001009;
    
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int w = sc.nextInt();
        
        // ans: 迷路上を下記の2種類の移動でs -> gへ行く最短コスト
        // A: 上下左右に+0で行ける
        // B: 周囲25マスに+1で行ける
        int ans = 0;
        
        // start, goal
        int sh = sc.nextInt()-1;
        int sw = sc.nextInt()-1;
        int gh = sc.nextInt()-1;
        int gw = sc.nextInt()-1;
        
        // board[i][j]: 迷路
        char[][] board = new char[h][w];
        for (int i = 0; i < h; i++) {
            String str = sc.next();
            for (int j = 0; j < w; j++) {
                board[i][j] = str.charAt(j);
            }
        }
        
        // 上下左右
        int[] dx = new int[]{1, 0, -1, 0};
        int[] dy = new int[]{0, 1, 0, -1};
        
        // visited[i][j]: 当該マスを既に通ったか否か
        boolean[][] visited = new boolean[h][w];
        
        // dist: startからの最短距離
        int[][] dist = new int[h][w];
        for (int[] aaaa : dist) {
            Arrays.fill(aaaa, INF);
        }
        dist[sh][sw] = 0;
        
        // ダイクストラ法: BFSで距離を配る。
        // (キューの先頭の点はそう来る他最短経路がないので最短距離が確定する)
        PriorityQueue<Point> que = new PriorityQueue<>();
        que.add(new Point(sh, sw, 0));
        while (!que.isEmpty()) {
            Point cur = que.poll();
            int x = cur.x;
            int y = cur.y;
            int depth = cur.dist;
            visited[x][y] = true;
            
            // 移動A
            // 移動可能ならそのマスに深さ+0を配ってキューに入れる
            for (int k = 0; k < 4; k++) {
                int nx = x + dx[k];
                int ny = y + dy[k];
                int ndepth = depth + 0;
                
                // 最短距離が更新される場合だけキューに入れる
                if (0 <= nx && nx < h && 0 <= ny && ny < w &&
                    board[nx][ny] == '.' && !visited[nx][ny] &&
                    ndepth < dist[nx][ny]) {
                    dist[nx][ny] = ndepth;
                    que.add(new Point(nx, ny, ndepth));
                }
            }
            
            // 移動B
            // 移動可能ならそのマスに深さ+1を配ってキューに入れる
            for (int i = -2; i <= 2; i++) {
                for (int j = -2; j <= 2; j++) {
                    int nx = x + i;
                    int ny = y + j;
                    int ndepth = depth + 1;
                    
                    // 最短距離が更新される場合だけキューに入れる
                    if (0 <= nx && nx < h && 0 <= ny && ny < w &&
                        board[nx][ny] == '.' && !visited[nx][ny] &&
                        ndepth < dist[nx][ny]) {
                        dist[nx][ny] = ndepth;
                        que.add(new Point(nx, ny, ndepth));
                    }
                }
            }
        }
        
        ans = dist[gh][gw];
        if (ans == INF) ans = -1;
        System.out.println(ans);
    }
}

class Point implements Comparable<Point> {
    int x;
    int y;
    int dist;
     
    Point(int x, int y, int dist) {
        this.x = x;
        this.y = y;
        this.dist = dist;
    }
    
    @Override
    public int compareTo(Point o) { // longでもint
        //重みの小さい順
        if (this.dist < o.dist) {
            return -1;
        } 
        return 1;
    }
}