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++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
 
 


//c++ templte for help............................................................................................................................................



typedef long long ll;

template<typename... T>
void print1(T... t){
	((cout<<t<<" "),...);
}

template<typename... T>
void print2(T... t){
		vector<vector<ll>> ans={t...};
		for(ll i=0;i<(ll)ans.size();i++){
			if(i!=0) cout<<"\t\t";
			else cout<<"\t";
			for(ll j=0;j<(ll)ans[i].size();j++){
				cout<<ans[i][j]<<" ";
			}
			if(i!=(ll)ans.size()-1)
			cout<<endl;
		}
}	
#define deb(...) cout<<" [ "#__VA_ARGS__<<" : "; print1(__VA_ARGS__); cout<<"]"<<endl; 
#define vdeb(...) cout<<" [ "#__VA_ARGS__<<" : "; print2(__VA_ARGS__); cout<<"]"<<endl;





	
 
//for loop
#define fo(i,n) for(int i=0;i<(ll)n;i++)
#define Fo(i,k,n) for(int i=k;i<n;i++)
 
 
//to print vector element 
template<typename T>
void printV(T vec){
	fo(i,vec.size()) cout<<vec[i]<<" "<<flush;
	cout<<endl;
	}
	
 
#define M int(1e9+7)
const int INF=int(1e9+5);
 
//iterate map
template<typename T>
void itR(T map){
	 for(auto itr=map.begin();itr!=map.end();itr++){
				deb(itr->first,itr->second);
			}
	cout<<endl;
}


 

 
//c++ template for help...........................................................................................................................................

bool is_valid(ll a,ll b,vector<string> &vec){
	if(a<0 or b<0 or a>=(ll)vec.size() or b>=(ll)vec[0].size()) return 0;
	if(vec[a][b]=='#') return 0;
	return 1;
}

class comparator{
	public:
	bool operator()(pair<pair<ll,ll>,ll> a,pair<pair<ll,ll>,ll> b){
		return a.second>b.second;
	}
};

ll solve(){
	ll row,col;
	cin>>row>>col;
	
	pair<ll,ll> start;
	pair<ll,ll> end;
	cin>>start.first>>start.second>>end.first>>end.second;
	
	start.first--;
	start.second--;
	end.first--;
	end.second--;
	
	vector<string> vec(row);
	for(int i=0;i<row;i++){
		cin>>vec[i];
	}
		
	vector<pair<ll,ll>> zero={{0,1},{1,0},{0,-1},{-1,0}};
	vector<pair<ll,ll>> one={{-2,0},{0,2},{0,-2},{2,0},
							{-2,1},{-1,1},{1,1},{2,1},
							{-2,2},{-1,2},{1,2},{2,2},
							{-2,-1},{-1,-1},{1,-1},{2,-1},
							{-2,-2},{-1,-2},{1,-2},{2,-2}};
	
	set<pair<ll,ll>> visited;
	set<pair<ll,ll>> visited1;
	priority_queue<pair<pair<ll,ll>,ll>,vector<pair<pair<ll,ll>,ll>>,comparator> pq;// node and dist	
	pq.push({{start.first,start.second},0});
	visited.insert({start.first,start.second});
	visited1.insert({start.first,start.second});
	
	while(!pq.empty()){
		ll x=pq.top().first.first;
		ll y=pq.top().first.second;
		ll dist=pq.top().second;
		//deb(x,y,dist);
		if(x==end.first and y==end.second) return dist;
		pq.pop();
		x=pq.top().first.first;
		y=pq.top().first.second;
		//deb(x,y);
		for(ll i=0;i<4;i++){
			if(is_valid(x+zero[i].first,y+zero[i].second,vec) and visited.count({x+zero[i].first,y+zero[i].second})==0){
				//deb(x+zero[i].first,y+zero[i].second);
				pq.push({{x+zero[i].first,y+zero[i].second},dist});
				visited.insert({x+zero[i].first,y+zero[i].second});
			}
		}
		for(ll i=0;i<20;i++){
			if(is_valid(x+one[i].first,y+one[i].second,vec) and visited1.count({x+one[i].first,y+one[i].second})==0){
				pq.push({{x+one[i].first,y+one[i].second},dist+1});
				visited1.insert({x+one[i].first,y+one[i].second});
			}
		}
	}
	return -1;
}

int main() {
	//stringstream ss;
	//ss<<"akhil";
	//cout<<ss.str()<<endl;
	cout<<setprecision(10);
	ios::sync_with_stdio(0);
	cin.tie(0);
	//ofstream myfile;
	//myfile.open("output.txt");
	

	//ll testCase;
	//cin>>testCase;
	//for(ll tt=0;tt<testCase;tt++){
		//solve();
		ll ans=solve();
		cout<<ans<<endl;
	//}
	//myfile.close();
	return 0;
}