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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
#define f(i,a,b) for (ll i=(a); i<(b);i++)
#define frr(i,a,b) for(ll i=(a-1);i>=(b);i--)
#define pb push_back
#define ppb pop_back()
#define pf push_front
#define ppf pop_front()
#define F first
#define S second
#define pii pair<int,int> 
#define rev(a) reverse(a.begin(),a.end())
#define srt(a) sort(a.begin(),a.end())
#define rsrt(a) sort(a.begin(),a.end(),greater<int>())
#define mem(a,n) memset(a,n,sizeof a)
#define ln(a) a.length()
#define sz(a) (ll)a.size()
#define endl "\n"
#define pi 3.14159265358979

const ll M=1e9+7;
const int P=31;
const int MaxM=200005;
/*__________________________________________________________________________________________________________________*/
int n,m;
char s[1005][1005];
int vis[1005][1005];
int cnt[1005][1005];
vector<pair<int,int> >v[1005];
void dfs(int i,int j,int k)
{
    if(vis[i][j]!=0)return;
    vis[i][j]=k;
    v[k].pb({i,j});
    if(i+1<n && s[i+1][j]=='.')dfs(i+1,j,k);
    if(i-1>=0 && s[i-1][j]=='.')dfs(i-1,j,k);
    if(j-1>=0 && s[i][j-1]=='.')dfs(i,j-1,k);
    if(j+1<m && s[i][j+1]=='.')dfs(i,j+1,k);

    return;
}

void bfs(int i,int j,int val)
{
    int p=vis[i][j];
    for(auto it:v[p])
    {
         cnt[it.F][it.S]=val;
    }
    for(auto it:v[p])
    {
         int x=it.F,y=it.S;
         for(int i1=x-2;i1<=x+2;i1++)
         {
            for(int j1=y-2;j1<=y+2;j1++)
            {
                if(i1>=0 && j1>=0 && i1<n && j1<m && s[i1][j1]=='.' && cnt[i1][j1]==-1)
                    bfs(i1,j1,val+1);
            }
         }

    }
    return;

}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);  
    
     
      
  cin>>n>>m;
  int x1,y1;cin>>x1>>y1;
  x1--;y1--;
  int x2,y2;cin>>x2>>y2;
  x2--;y2--;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
        cin>>s[i][j];
    }
  }

  memset(vis,0,sizeof(vis));
  int k=0;
  for(int i=0;i<n;i++)
  {
    for(int j=0;j<m;j++)
    {
        if(s[i][j]=='.' && vis[i][j]==0)
            {
                k++;
                dfs(i,j,k);
            }
    }
  }

  memset(cnt,-1,sizeof(cnt));

  bfs(x1,y1,0);

  cout<<cnt[x2][y2]<<endl;

}