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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	static readonly int[][] di1=new int[][]{new int[]{-1,0},new int[]{1,0},new int[]{0,-1},new int[]{0,1}};
	static string[] bm;
	static int[][] b;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		int h=s[0],w=s[1];
		char c='#';
		var t=sc.Ia;
		var g=sc.Ia;
		int sy=t[0],sx=t[1];
		bm=new string[h+2];
		b=new int[h+2][];
		for(int i = 1;i<=h;i++) {
			bm[i]=c+sc.S+c;
			b[i]=new int[w+2];
		}
		bm[0]=bm[h+1]=new string(c,w+2);
		b[0]=b[h+1]=new int[w+2];
		var qu=new Qu2<ValueTuple<int,int,int>>();
		qu.Enqueue0(0,ValueTuple.Create(sy,sx,1));
		b[sy][sx]=1;
		while(qu.cnt>0){
			var e=qu.Dequeue;
			if(e.d.Item1==g[0]&&e.d.Item2==g[1]){break;}
			Fdi(e.d.Item1,e.d.Item2,(y,x)=>{
				if(bm[y][x]=='.'&&(b[y][x]==0||b[y][x]>e.d.Item3)){
					qu.Enqueue0(0,ValueTuple.Create(y,x,e.d.Item3));
					b[y][x]=e.d.Item3;
				}
			});
			for(int i = -2;i<=2;i++) {
				for(int j = -2;j<=2;j++) {
					int y=e.d.Item1+i,x=e.d.Item2+j;
					if(y>0&&x>0&&y<=s[0]&&x<=s[1]&&bm[y][x]=='.'&&b[y][x]==0){
						qu.Enqueue0(1,ValueTuple.Create(y,x,e.d.Item3+1));
						b[y][x]=e.d.Item3+1;
					}
				}
			}
		}
		Console.WriteLine("{0}",b[g[0]][g[1]]-1);
	}
	static void Fdi(int y,int x,Action<int,int> f){for(int i=0;i<4;i++){f(y+di1[i][0],x+di1[i][1]);}}
}
public class Qu2<T>{
	public class Dt{
		public int n;
		public T d;
		public Dt(int n,T d){this.n=n;this.d=d;}
		public override string ToString()=>"n:"+n.ToString()+" d:"+d.ToString();
	}
	private Queue<Dt> qu0=new Queue<Dt>(),qu1=new Queue<Dt>();
	public void Enqueue(bool b,int n,T d){
		if(b){qu0.Enqueue(new Dt(n,d));}
		else{qu1.Enqueue(new Dt(n+1,d));}
	}
	public void Enqueue0(int n,T d){qu0.Enqueue(new Dt(n,d));}
	public void Enqueue1(int n,T d){qu1.Enqueue(new Dt(n,d));}
	public Dt Dequeue{get{return qu0.Count>0?qu0.Dequeue():qu1.Dequeue();}}
	public Dt Peek{get{return qu0.Count>0?qu0.Peek():qu1.Peek();}}
	public int cnt{get{return qu0.Count+qu1.Count;}}
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}