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>
using namespace std;
#define REP(i,x,n) for(int i=x;i<(int)n;i++)
#define rep(i,n) REP(i,0,n)
#define sp(p) cout<<setprecision(16)<<fixed<<p<<endl;
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define SORT(a) sort(all(a))
#define RSORT(a) sort(rall(a))
#define UNIQ(a) unique(all(a))
#define UNIQNUM(a) UNIQ(a)-a.begin()
#define UNIQIT(a) a.erase(UNIQ(a), a.end());
#define VOUT(v,i) rep(i,v.size())cout<<v[i]<<(i==v.size()-1?"\n":" ");
#define vout(v) VOUT(v,z);
#define vdbg(v,i) cout<<#v<<": ";for(int i=0;i<(int)v.size();i++){cout<<v[i]<<" ";}cout<<"\n";
#define vmin(v) *min_element(all(v))
#define vmax(v) *max_element(all(v))
#define vsum(v) accumulate(all(v), 0LL)
#define MOUT(m,r,c) rep(i,r){rep(j,c){cout<<m[i][j]<<" ";}cout<<endl;}
#define mout(m) MOUT(m,m.size(),m[0].size())
#define debg(a) cout<<#a<<": "<<a<<endl;
#define show(a) for(cont &y:a){for(cont &x:y){cout<<x<<" ";}cout<<endl;}
#define digit(a) to_string(a).length();
template<class T>inline int out(const T &t){ print(t); putchar('\n'); return 0; }
// template<class T>inline T gcd(T a,T b){if(b==0)return a; return(gcd(b,a%b));}
// template<class T>inline T lcm(T a,T b){return a/gcd(a,b)*b;}
bool is_palindrome(string s){return s == string(s.rbegin(),s.rend());}
#define popcount __builtin_popcount
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef vector<ll> V;
typedef vector<vector<ll>> VV;
// const long long MOD=1000000007;
const long long INF = 1e18;
#define EPS (1e-7)
#define PI (acos(-1))
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) { a = b; return true; }
    return false;
}
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) { a = b; return true; }
    return false;
}

const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int wx[20] = {1,-1,-1, 1, 2, 2, 2, 2, 2, 1, 1, 0, 0,-1,-1,-2,-2,-2,-2,-2};
const int wy[20] = {1, 1,-1,-1, 2, 1, 0,-1,-2, 2,-2, 2,-2, 2,-2, 2, 1, 0,-1,-2};

ll h, w;
bool isRange(ll y, ll x){return(0<=y&&y<h&&0<=x&&x<w);}

int main(){
    // ll h, w;
    cin >> h >> w;
    ll ch, cw, dh, dw;
    cin >> ch >> cw >> dh >> dw;
    vector<vector<char>> s(h, vector<char>(w));
    for (ll i = 0; i < h; i++) {
        for (ll j = 0; j < w; j++) {
            cin>>s[i][j];
        }
    }

    // mout(s);
    vector<vector<ll>> n(h, vector<ll>(w, -1));

    queue<P> q;
    queue<P> wq;
    P p;
    ch--,cw--;
    dh--,dw--;
    q.push(P(ch,cw));
    wq.push(P(ch,cw));
    n[ch][cw]=0;

    while(q.size()){
        while(q.size()){
            p=q.front();
            q.pop();
            // for (long long i = 0; i < 20; i++) {
            //     ll ny=p.first+wy[i], nx=p.second+wx[i];
            //     if(isRange(ny,nx) && s[ny][nx]=='.' && n[ny][nx]==-1){
            //         n[ny][nx]=n[p.first][p.second]+1;
            //         q.push(P(ny,nx));
            //     }
            // }
            for (long long i = 0; i < 4; i++) {
                ll ny=p.first+dy[i], nx=p.second+dx[i];
                if(isRange(ny,nx) && s[ny][nx]=='.' && (n[ny][nx]>n[p.first][p.second] || n[ny][nx]==-1)){
                    n[ny][nx]=n[p.first][p.second];
                    if(n[ny][nx]!=-1){
                        chmin(n[ny][nx],n[p.first][p.second]);
                        chmin(n[p.first][p.second],n[ny][nx]);
                    }
                    q.push(P(ny,nx));
                    wq.push(P(ny,nx));
                }
            }
            // if(ch==0&&cw==0) {
            //     mout(n);
            //     cout << endl;
            // }
        }
        // mout(n);

        while(wq.size()){
            p=wq.front();
            wq.pop();
            for (long long i = 0; i < 20; i++) {
                ll ny=p.first+wy[i], nx=p.second+wx[i];
                if(isRange(ny,nx) && s[ny][nx]=='.' && n[ny][nx]==-1){
                    n[ny][nx]=n[p.first][p.second]+1;
                    q.push(P(ny,nx));
                }
            }
        }
    }
    // mout(n);

    cout << n[dh][dw] << endl;

    return 0;
}