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: Accepted)

#include <bits/stdc++.h>
#include <algorithm>
#include <unordered_map>
#define ull unsigned long long
#define int long long
#define ll long long
#define fr(i, n) for (int i = 0; i < n; i++)
#define frf(i, j, n) for (int i = j; i <= n; i++)
#define frd(i, n) for (int i = n; i >= 0; i--)
#define mp(i, j) make_pair(i, j)
#define pb(x) push_back(x)
using namespace std;
// typedef vector<vector<ll>> matrix;
// const ll PRIME = 1e9 + 7;
// typedef pair<int,pair<int,int> >  ppi;
int n,m;
string s[1001];
bool isValid(int x,int y)
{
    if(x<0|| x>n-1 || y<0 || y>m-1)return 0;
    if(s[x][y]=='#')return 0;
    return 1;
}
void solve()
{
  
   cin>>n>>m;
   int s1,s2;
   cin>>s1>>s2;

   int d1,d2;
   cin>>d1>>d2;
   s1--,s2--,d1--,d2--;
  
   fr(i,n)
   {
       cin>>s[i];
   }
   int dis[n][m];
   fr(i,n)
   {
       fr(j,m)
       {
           dis[i][j]=1e7;
       }
   }
   dis[s1][s2]=0;
   int dx[]={-1,0,0,1};
    int dy[]={0,1,-1,0};
   deque<int>qx,qy;
   qx.push_front(s1);
   qy.push_front(s2);
   while(!qx.empty())
   {
       int curx=qx.front(),cury=qy.front();
        qx.pop_front(),qy.pop_front();
        
        fr(i,4)
        {
            int newx=curx+dx[i],newy=cury+dy[i];
            //  cout<<newx<<newy<<isValid(newx,newy)<<" ";
            if(!isValid(newx,newy))continue;
           
            if(dis[newx][newy]>dis[curx][cury])
            {dis[newx][newy]=dis[curx][cury];
             
            qx.push_front(newx),qy.push_front(newy);
            }
        }
        frf(i,curx-2,curx+2)
        {
            frf(j,cury-2,cury+2)
            {
                if(!isValid(i,j))continue;
                if(dis[i][j]>dis[curx][cury]+1)
                { 
                    dis[i][j]=dis[curx][cury]+1;
                    qx.push_back(i),qy.push_back(j);
                }
                
            }
        }
   }
   if(dis[d1][d2]==1e7)
   {
       cout<<-1;
   }
   else
   {
       cout<<dis[d1][d2];
   }

// fr(i,n)
// {
//     fr(j,m)
//     {
//         cout<<dis[i][j]<<" ";
//     }
//     cout<<"\n";
// }
}
int32_t main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int t;
    // cin >> t;
    t = 1;
    while (t--)
    {
        solve();
    }
}