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 1.8.0) (judge verdict: Accepted)

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
    private static final int[] dr = { -1, 0, 0, 1, };
    private static final int[] dc = { 0, -1, 1, 0, };

    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in)) {
            int H = sc.nextInt();
            int W = sc.nextInt();
            int CH = sc.nextInt() - 1;
            int CW = sc.nextInt() - 1;
            int DH = sc.nextInt() - 1;
            int DW = sc.nextInt() - 1;
            boolean[][] S = new boolean[H][W];
            for (int h = 0; h < H; h++) {
                String line = sc.next();
                for (int w = 0; w < W; w++) {
                    S[h][w] = line.charAt(w) == '#';
                }
            }

            int n = search(H, W, CH, CW, DH, DW, S);

            System.out.println(n);
        }
    }

    private static int search(int h, int w, int cH, int cW, int dH, int dW, boolean[][] isWall) {
        Comparator<Node> comparator = new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if (o1.n < o2.n) {
                    return -1;
                }
                if (o1.n > o2.n) {
                    return 1;
                }
                return 0;
            }
        };
        PriorityQueue<Node> queue = new PriorityQueue<>(comparator);
        int[][] used = new int[h][w];
        for (int r = 0; r < h; r++) {
            Arrays.fill(used[r], (int) 1e9);
        }
        {
            used[cH][cW] = 0;
            queue.add(new Node(cH, cW, 0));
        }
        int min = (int) 1e9;
        for (; !queue.isEmpty();) {
            Node node = queue.poll();
//            Utils.debug(node.r, node.c, node.n);

            if (node.n > used[node.r][node.c]) {
                continue;
            }

            if (node.r == dH && node.c == dW) {
                if (node.n < min) {
                    min = node.n;
                }
                continue;
            }

            for (int d = 0; d < dr.length; d++) {
                int nr = node.r + dr[d];
                int nc = node.c + dc[d];
                if (nr < 0 || nr >= h || nc < 0 || nc >= w) {
                    continue;
                }
                if (isWall[nr][nc]) {
                    continue;
                }
                int nn = node.n;
                if (nn >= used[nr][nc]) {
                    continue;
                }
                used[nr][nc] = nn;
                queue.add(new Node(nr, nc, nn));
            }
            for (int dr2 = -2; dr2 <= 2; dr2++) {
                for (int dc2 = -2; dc2 <= 2; dc2++) {
                    int nr = node.r + dr2;
                    int nc = node.c + dc2;
                    if (nr < 0 || nr >= h || nc < 0 || nc >= w) {
                        continue;
                    }
                    if (isWall[nr][nc]) {
                        continue;
                    }
                    int nn = node.n + 1;
                    if (nn >= used[nr][nc]) {
                        continue;
                    }
                    used[nr][nc] = nn;
                    queue.add(new Node(nr, nc, nn));
                }
            }
        }

        return min >= (int) 1e9 ? -1 : min;
    }
}

class Node {
    int r;
    int c;
    int n;

    public Node(int r, int c, int n) {
        super();
        this.r = r;
        this.c = c;
        this.n = n;
    }

}