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 ll long long int 
#define mod 1000000007
#define modulo 998244353
#define endl '\n'
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
using namespace std;
vector <int> adj[100001];
 const ll N=1e3+5;
char s[N][N];
ll vis[N][N];
ll dx[] = {-1,1,0,0};
ll dy[] = { 0,0,1,-1};
ll h,w;
ll r=0;
ll fxp(ll a, ll b)
{
    if(b == 0)return 1;
	if(b%2 == 0){
		ll c = fxp(a,b/2);
		return (c*c)%1000000007;
	}
	return (a*fxp(a,b-1))%1000000007;
}
int lcm(int c,int d)
{
    int a=c;
    int b=d;
    int ans;
    if(a>=b)
    {
        while(b!=0)
        { 
            int x=b;
            b=a%b;
            a=x;
        }
        ans = ( c*d)/a;
    }
    else
    {
        while(a!=0)
        {
            int x=a;
            a=b%a;
            b=x;
        }
        ans = ( c*d)/b;  
    }
    return ans;
}
int root_find()
{
    ll x;
    for(int i=1;i<100001;i++)
    {
        if(adj[i].size()==1)
        {
            x = i;
            break;
        }
    }
    return x;
}
bool isValid(int x,int y)
{
    if(x>=0 && x<h)
    {
        if(y>=0 && y<w)
        return true;
        else 
        return false;
    }
    else
    {
        return false;
    }
    
}
void dfs(int x,int y)
{
    vis[x][y]=r;
    for(int i=0;i<4;i++)
    {
        ll a = x + dx[i];
        ll b = y + dy[i];
        if(isValid(a,b) && vis[a][b]==0 && s[a][b]=='.')
        {
            dfs(a,b);
        }
    }
}
/*void bfs(int s)
{
    queue <ll> q;
    q.push(s);
    vis[s]=1;
    while(!q.empty())
    {
        ll u = q.front();
        q.pop();
        for(int i=0;i<adj[u].size();i++)
        {
            if(vis[adj[u][i]]==0)
            {
            vis[adj[u][i]]=1;
            q.push(adj[u][i]);
            }
        }

    }
}*/
bool isPrime(ll n) 
{ 
    if (n <= 1)  return false; 
    if (n <= 3)  return true; 
  
    if (n%2 == 0 || n%3 == 0) return false; 
  
    for (int i=5; i*i<=n; i=i+6) 
        if (n%i == 0 || n%(i+2) == 0) 
           return false; 
  
    return true; 
}
ll smallestDivisor(ll n) 
{ 
    // if divisible by 2 
    if (n % 2 == 0) 
        return n/2; 
  
    // iterate from 3 to sqrt(n) 
    for (int i = 3; i * i <= n; i += 2) { 
        if (n % i == 0) 
            return n/i; 
    } 
  
    return n; 
}  
bool sortbysec(const pair<int,int> &a, 
              const pair<int,int> &b) 
{ 
    return (a.second < b.second); 
} 
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin>>h>>w;
    ll x1,x2;
    cin>>x1>>x2;
    x1--;
    x2--;
    ll y1,y2;
    cin>>y1>>y2;
    y1--;
    y2--;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            cin>>s[i][j];
            vis[i][j]=0;
        }
    }
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if(s[i][j]=='.' && vis[i][j]==0)
            {
                r++;
                dfs(i,j);

            }
        }
    }
    vector <set<ll>> v(r+1);
    ll a = vis[x1][x2];
    ll b = vis[y1][y2];
    ll dxi[] = {-2,2,0,1,2};
    ll dyj[] = {-2,2,0,1,2};
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if(s[i][j]=='.')
            {
                ll ch = vis[i][j];
                for(int p=0;p<5;p++)
                {
                    for(int q=0;q<5;q++)
                    {
                        ll ni = i + dxi[p];
                        ll nj = j + dyj[q];
                        if(ni!=i || nj !=j)
                        {
                            if(isValid(ni,nj))
                            {
                                ll idx = vis[ni][nj];
                                if(idx&& ch!=idx)
                                v[ch].insert(idx);
                            }
                        }
                    }
                }
            }
        }
    }
    queue <ll> q;
    ll seen[r+1]={0};
    vector <ll> lvl(r+1,1e9);
    q.push(a);
    lvl[a]=0;
    while(!q.empty())
    {
        ll z = q.front();
        seen[z]=1;
        q.pop();
        for(auto child:v[z])
        {
            if(seen[child]==0)
            {
                seen[child]=1;
                q.push(child);
                if(lvl[z]+1<lvl[child])
                lvl[child]=lvl[z]+1;
            }
        }
    }
    if(lvl[b]==1e9)
    cout<<-1<<endl;
    else
    {
        cout<<lvl[b]<<endl;
    }
    


    
    
    return 0;
}