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: Runtime Error)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        int h = Integer.parseInt(input[0]);
        int w = Integer.parseInt(input[1]);
        Pair[][] mat = new Pair[h][w];
        input = br.readLine().split(" ");
        int start = convert(Integer.parseInt(input[0])-1, Integer.parseInt(input[1])-1, h);
        input = br.readLine().split(" ");
        int end = convert(Integer.parseInt(input[0])-1, Integer.parseInt(input[1])-1, h);
        for(int i=0; i<h; i++) {
            String s = br.readLine();
            for(int j=0; j<w; j++)
                mat[i][j] = new Pair(convert(i, j, h), s.charAt(j));
        }
        Graph g = new Graph((h+1) * (w+1) + 1);
        for(int i=0; i<h; i++) {
            for(int j=0; j<w; j++) {
                if(mat[i][j].c != '#') {
                    ArrayList<Node> connects = getConnects(i, j, h, w, mat);
                    int val = convert(i, j, h);
                    for (Node node : connects) {
                        g.addEdge(val, node.node, node.cost);
                    }
                }
            }
        }
        System.out.println(g.find(start, end));
        br.close();
    }

    static ArrayList<Node> getConnects(int i, int j, int h, int w, Pair[][] mat) {
        ArrayList<Node> list = new ArrayList<>();
        int m = i - 2, n = j - 2;
        for(int k=m; k<m+5; k++) {
            for(int l=n; l<n+5; l++) {
                if(isValidIndex(k, l, h, w)) {
                    if(i == k && j == l)
                        continue;
                    if(mat[k][l].c != '#') {
                        int diff = Math.abs(i - k);
                        diff += Math.abs(j - l);
                        if(diff == 1)
                            list.add(new Node(convert(k, l, h), 0));
                        else
                            list.add(new Node(convert(k, l, h), 1));
                    }
                }
            }
        }
        return list;
    }

    static boolean isValidIndex(int i, int j, int h, int w) {
        if(i >= h || j >= w || i < 0 || j < 0)
            return false;
        return true;
    }

    static class Graph {
        int V;
        ArrayList<Node> list[];

        Graph(int v) {
            V = v;
            list = new ArrayList[V];
            for(int i=0; i<V; i++) {
                list[i] = new ArrayList<>();
            }
        }

        void addEdge(int u, int v, int cost) {
            list[u].add(new Node(v, cost));
        }

        int find(int start, int end) {
            int[] dist = new int[V];
            Arrays.fill(dist, Integer.MAX_VALUE);
            dist[start] = 0;
            boolean[] visited = new boolean[V];
            dijkstra(start, dist, visited);
            return dist[end] == Integer.MAX_VALUE ? -1 : dist[end];
        }

        void dijkstra(int start, int[] dist, boolean[] visited) {
            PriorityQueue<Node> pq = new PriorityQueue<>(V, new Node());
            pq.add(new Node(start, 0));
            while(!pq.isEmpty()) {
                Node a = pq.poll();
                if(visited[a.node])
                    continue;
                visited[a.node] = true;
                for(int i=0; i<list[a.node].size(); i++) {
                    Node u = list[a.node].get(i);
                    if(dist[u.node] > dist[a.node] + u.cost) {
                        dist[u.node] = dist[a.node] + u.cost;
                    }
                    if(!visited[u.node]) {
                        pq.add(new Node(u.node, dist[u.node]));
                    }
                }
            }
        }
    }

    private static int convert(int i, int j, int h) {
        return h*i+j;
    }

    static class Node implements Comparator<Node> {
        int node, cost;

        Node() {}

        Node(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        public int compare(Node a, Node b) {
            return a.cost - b.cost;
        }
    }

    static class Pair {
        int i;
        char c;

        Pair() {}

        Pair(int i, char c) {
            this.i = i;
            this.c = c;
        }
    }
}