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: Time Limit Exceeded)

#include<bits/stdc++.h>
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
#define ios; ios::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define ls(x) (x<<1)
#define rs(x) (ls(x)|1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
const int M = 1e3 + 5;
inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; }
inline ll lcm(ll a,ll b){ return a*b/gcd(a,b); }
inline ll ksm(ll x,ll y){ ll res=1; while(y){if(y&1) res=res*x%mod;
x=x*x%mod; y>>=1;} return res; }
int n, m, k;
int sx,sy,tx,ty;
int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char s[N][N];
int vis[N][N];
int pre[N*(N+1)], mp[N*(N+1)];
vector<int> g[N*(N+1)];
int a[N*(N+1)], cnt;
set<pii> st;
int Find(int x){
    return x==pre[x]?x:pre[x]=Find(pre[x]);
}
void join(int x,int y){
    int fx=Find(x), fy=Find(y);
    if(fx!=fy){
        pre[fy]=fx;
    }
}
bool ok(int x,int y){
    if(x<1||y<1||x>n||y>m||s[x][y]=='#') return 0;
    return 1;
}
void dfs(int x,int y){
    vis[x][y]=1;
    rep(i,0,3){
        int dx=x+d[i][0];
        int dy=y+d[i][1];
        if(!ok(dx,dy)||vis[dx][dy]) continue;
        dfs(dx,dy);
        join(y+x*m,dy+dx*m);
    }
}
int bfs(int S,int T){
    queue<pii> que;
    que.push(mk(S,0));
    while(!que.empty()){
        int u=que.front().fi, t=que.front().se;
        que.pop();
        if(u==T) return t;
        for(int v:g[u]){
            que.push({v,t+1});
        }
    }
    return -1;
}

int main(){
    ios;
    rep(i,1,1000000) pre[i]=i;
    cin>>n>>m;
    cin>>sx>>sy;
    cin>>tx>>ty;
    rep(i,1,n) cin>>s[i]+1;
    rep(i,1,n) rep(j,1,m) if(!vis[i][j]&&s[i][j]=='.'){
        dfs(i,j);
    }
    rep(i,1,n) rep(j,1,m){
        rep(p,-2,2) rep(q,-2,2){
            int dx=i+p, dy=j+q;
            if(!ok(dx,dy)||s[i][j]=='#'||s[dx][dy]=='#') continue;
            int s1=j+i*m, s2=dy+dx*m;
            int fs1=Find(s1), fs2=Find(s2);
            if(fs1!=fs2&&st.count(mk(fs1,fs2))==0){
                if(mp[fs1]==0){
                    mp[fs1]=++cnt;
                    a[cnt]=fs1;
                }
                if(mp[fs2]==0){
                    mp[fs2]=++cnt;
                    a[cnt]=fs2;
                }
                g[fs1].pb(fs2);
                g[fs2].pb(fs1);
                st.insert(mk(fs1,fs2));
                st.insert(mk(fs2,fs1));
            }
        }
    }
    int S=Find(sy+sx*m), T=Find(ty+tx*m);
    cout<<bfs(S,T);
    return 0;
}