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)

/**
 *    author:  Daredevil666
 *    institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include<bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define REP(i,a,n) for(i=a;i<=n;i++)
#define F(i,a,b) for(i=a;i<b;i++)
#define RF(i,b,a) for(i=b;i>=a;i--)
#define IOS  ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const ll mod=1e9+7;
const ll MAXN=100200;
ll i,j;
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
   freopen("input.txt","r",stdin);
   freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE*/
ll n,m;
cin>>n>>m;
ll x,y;
cin>>x>>y;
ll u,v;
cin>>u>>v;
char a[n+2][m+2];
ll dp[n+2][m+2],vis[n+2][m+2];
memset(vis,0,sizeof(vis));
F(i,0,n)
{
    F(j,0,m)
    {
        cin>>a[i][j];
        dp[i][j]=mod;
    }
}
dp[x-1][y-1]=0;
ll xc[]={1,-1,0,0};
ll yc[]={0,0,1,-1};
queue<pair<ll,ll> > t,q;
t.push(make_pair(x-1,y-1));
q.push(make_pair(x-1,y-1));
vis[x-1][y-1]=1;
while(!t.empty())
{
    pair<ll,ll> p=t.front();
    t.pop();
    F(i,0,4)
    {
        ll r=p.first+xc[i],s=p.second+yc[i];
        if(r<0 || r>=n || s<0 || s>=m)continue;
        if(a[r][s]=='.' && vis[r][s]==0)
        {
            vis[r][s]=1;
            dp[r][s]=0;
            q.push(make_pair(r,s));
           // cout<<r<<" "<<s;nl
             t.push(make_pair(r,s));
        }
    }
}
ll k,g;
while(!q.empty())
{
    pair<ll,ll> p=q.front();
    q.pop();
    F(k,p.first-2,p.first+3)
    {
        F(g,p.second-2,p.second+3)
        {
             if(k<0 || k>=n || g<0 || g>=m || (k==p.first && g==p.second))continue;
             if(a[k][g]=='.')
             {
                 if(vis[k][g]==0)
                 {
                     vis[k][g]=1;
                     //cout<<k<<" "<<g;nl
                     dp[k][g]=min(dp[k][g],dp[p.first][p.second]+1);
                     q.push(make_pair(k,g));
                 }
             }
        }
    }
}
if(dp[u-1][v-1]==mod)
    cout<<-1;
else
    cout<<dp[u-1][v-1];
return 0;
}