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>
#define F first 
#define S second 
#define endl "\n"
#define Endl "\n"
#define emdl "\n"
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
using namespace std;
// bool sortBy(const pair<int,pair<int,int>> &a,const pair<int,pair<int,int>> &b){
// 	if(a.S.F==b.S.F)
// 		return a.F<b.F;
// 	return a.S.F<b.S.F;
// }

// #define MAXN 1001
// int pf[MAXN]; 
// void sieve(){
// 	long long int i,j; 
//     pf[1] = 1; 
//     for (i=2; i<MAXN; i++) 
//     pf[i] = i;   
//     for (i=2; i*i<MAXN; i++){ 
//         if (pf[i] == i){
//             for (j=i*i; j<MAXN; j+=i) 
//                 if (pf[j]==j) 
//                     pf[j] = i; 
//         } 
//     } 
// }
// vector<int> factors(int x){ 
//     vector<int> ret; 
//     while (x != 1){ 
// 		int fact=pf[x];
//         ret.push_back(pf[x]); 
// 		while(fac!=1&&x%fact==0)
//         x/=fact; 
//     } 
//     return ret; 
// } 
// set<long long int>poww;
// void init(){
// 	long long int temp=1;
// 	for(int i=1;i<43;i++){
// 		poww.insert(temp);
// 		temp*=2;
// 	}
// }  
int a,b,x,y;
int n,m;
vector<vector<int>>vis,fin;
vector<pair<int,int>>sum;
int dfs(int i,int j){
    //cout<<i<<" "<<j<<endl;
    vis[i][j]=3;
    int ans=-1;
    int u,v;
    if(i==x&&j==y){
        vis[i][j]=1;
        fin[i][j]=0;
        return fin[i][j];
    }
    for(auto temp:sum){
        int hld=0;
        u=i+temp.first;
        v=j+temp.second;
        int flag;
        if(abs(temp.first)+abs(temp.second)>1)
        hld=1;
        if(u<n&&u>=0&&v<m&&v>=0){
            if(vis[u][v]==0){
                flag=dfs(u,v);
                if(flag==-1){
                    
                }else{
                    if(ans==-1){
                        ans=flag+hld;
                    }else{
                        ans=min(ans,flag+hld);
                    }
                }
            }else if(vis[u][v]>=2){

            }else{
                if(fin[u][v]!=-1){
                    if(ans!=-1){
                        ans=min(ans,fin[u][v]+hld);
                    }else{
                        ans=fin[u][v]+hld;
                    }
                }
            }
        }
    }
    vis[i][j]=1;
    fin[i][j]=ans;
    return ans;
}

int main() {		
	ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
	int qwe=1;
	//cin>>qwe;
	//init();
	// sieve();
	while(qwe--){
       
        cin>>n>>m;
        cin>>a>>b>>x>>y;
        string s[n];
        vis.resize(n);fin.resize(n);
        for(int i=0;i<n;i++){
            cin>>s[i];
            vis[i].resize(m);
            fin[i].resize(m);
        }
        for(int i=-2;i<=2;i++){
            for(int j=-2;j<=2;j++){
                if(i==0&&j==0){

                }
                else sum.push_back({i,j});
            }
        }
        a--;b--;x--;y--;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                fin[i][j]=-1;
                if(s[i][j]=='.'){
                    vis[i][j]=0;
                }else{
                    vis[i][j]=2;
                }
            }
        }
        cout<<dfs(a,b);
        // for(int i=0;i<n;i++){
        //     for(int j=0;j<m;j++){
        //         cout<<vis[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }

	}

	return 0;
}