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(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1900000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vs = vector<string>;
using vc = vector<char>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vvp = vector<vector<pair<int,int>>>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}


int H,W;
ve<ve<char>> G;
ve<ve<int>> telpote;

ve<int> dh = {0,-1,1,0};
ve<int> dw = {1,0,0,-1};

void bfs(int h, int w){
    
    
    G[h][w] = '-';
    
    reps(i,-2,3) reps(j,-2,3){
        if(i == 0 && j == 0) continue;
        int nh = h + i;
        int nw = w + j;
        
        if(nh >= 0 && nh < H && nw >= 0 && nw < W){
            if(G[nh][nw] == '.') chmin(telpote[nh][nw],telpote[h][w]+1);
        }
    }
    
    rep(kkk,4){
        int nh = h + dh[kkk];
        int nw = w + dw[kkk];
        
        if(nh >= 0 && nh < H && nw >= 0 && nw < W){
            if(G[nh][nw] == '#') continue;
            if(telpote[nh][nw] == telpote[h][w]) continue;
            telpote[nh][nw] = telpote[h][w];
            bfs(nh,nw);
            
        }
        
    }
    
    
}


int main(){
    
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> H >> W;
    G.resize(H,ve<char>(W));
    int Ch,Cw,Dh,Dw;
    cin >> Ch >> Cw >> Dh >> Dw;
    Ch--,Cw--,Dh--,Dw--;
    
    telpote.assign(H,vi(W,infi));
    telpote[Ch][Cw] = 0;
    
    rep(i,H) rep(j,W) cin >> G[i][j];
    
    int a = 0;
    while (1) {
        int ff = 0;
        rep(i,H) rep(j,W){
            if(G[i][j] == '.' && telpote[i][j] == a) {
                bfs(i,j);
                ff++;
            }
        }
        if(ff == 0) break;
        a++;
    }
    
    if(telpote[Dh][Dw] == infi) cout << -1 << endl;
    else cout << telpote[Dh][Dw] << endl;

    return 0;
}