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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	bool InRange(int t, int l, int r){ return l <= t && t < r;}
	public void Solve(){
		
		int Inf = (int) 1e9;
		Func<int, int, int> enc = (y, x) => y * W + x;
		int[][] min = new int[H][];
		bool[][] used = new bool[H][];
		for(int i=0;i<H;i++){
			min[i] = new int[W];
			used[i] = new bool[W];
			for(int j=0;j<W;j++){
				min[i][j] = Inf;
			}
		}
		
		int[] dy = new int[]{0,1,0,-1};
		int[] dx = new int[]{1,0,-1,0};
		
		var SH = new SkewHeap<Pair>();
		SH.Push(new Pair(enc(Ch, Cw), 0));
		min[Ch][Cw] = 0;
		while(SH.Count > 0){
			var p = SH.Top; SH.Pop();
			int y = p.Pos / W;
			int x = p.Pos % W;
			long cost = p.Cost;
			for(int i=-2;i<=2;i++){
				int ny = y + i;
				if(!InRange(ny, 0, H)) continue;
				for(int j=-2;j<=2;j++){
					int nx = x + j;
					if(!InRange(nx, 0, W)) continue;
					if(S[ny][nx] == '#') continue;
					if(min[ny][nx] > min[y][x] + 1){
						min[ny][nx] = min[y][x] + 1;
						SH.Push(new Pair(enc(ny, nx), min[ny][nx]));
					}
				}
			}
			for(int t=0;t<4;t++){
				int ny = y + dy[t];
				int nx = x + dx[t];
				if(!InRange(ny, 0, H)) continue;
				if(!InRange(nx, 0, W)) continue;
				if(S[ny][nx] == '#') continue;
				if(min[ny][nx] > min[y][x]){
					min[ny][nx] = min[y][x];
					SH.Push(new Pair(enc(ny, nx), min[ny][nx]));
				}
			}
		}
		
		int ans = min[Dh][Dw] == Inf ? -1 : min[Dh][Dw];
		Console.WriteLine(ans);
		
		
		
		
	}
	int H,W;
	int Ch, Cw;
	int Dh, Dw;
	String[] S;
	public Sol(){
		var d = ria();
		H = d[0]; W = d[1];
		d = ria();
		Ch = d[0] - 1; Cw = d[1] - 1;
		d = ria();
		Dh = d[0] - 1; Dw = d[1] - 1;
		S = new String[H];
		for(int i=0;i<H;i++) S[i] = rs();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


class Pair : IComparable<Pair> {
	public int Pos;
	public long Cost;
	public Pair(int p, long c) {
		Pos = p; Cost = c;
	}
	public int CompareTo(Pair t) {
		//return this.Cost > t.Cost ? -1 : this.Cost < t.Cost ? 1 : 0;
		return this.Cost > t.Cost ? 1 : this.Cost < t.Cost ? -1 : 0;
	}
}


class SkewHeap<T> where T:IComparable<T>{
	public int Count{
		get{return cnt;}
		private set{cnt=value;}
	}
	
	public SkewHeap(){
		root=null;
		this.Count=0;
	}
	
	public void Push(T v){
		NodeSH<T> p=new NodeSH<T>(v);
		root=NodeSH<T>.Meld(root,p);
		this.Count++;
	}
	
	public void Pop(){
		if(root==null)return;
		root=NodeSH<T>.Meld(root.L,root.R);
		this.Count--;
	}
	
	public T Top{
		get{return root.Val;}
	}
	
	int cnt;
	NodeSH<T> root;
	
	class NodeSH<S> where S : IComparable<S> {
		public NodeSH<S> L, R;
		public S Val;

		public NodeSH(S v){
			Val = v;
			L = null; R = null;
		}
		public static NodeSH<S> Meld(NodeSH<S> a, NodeSH<S> b){
			if(a == null)return b;
			if (b == null) return a;
			if (a.Val.CompareTo(b.Val) > 0) swap(ref a, ref b);
			a.R = Meld(a.R, b);
			swap(ref a.L, ref a.R);
			return a;
		}

		static void swap<U>(ref U x, ref U y) {
			U t = x; x = y; y = t;
		}
	}
}