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;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
#define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))
#define REPR(i, m, n) for(ll (i) = (m); (i) > (n); --(i))
#define rep(i, n) REP(i, 0, n)
#define R cin>>
#define repr(i, n) REPR(i, n, 0)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define in(a) insert(a)
#define P(p) cout<<(p)<<endl;
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define SORT(a) sort((a).begin(), (a).end())
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long int> vll;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
void sonic(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);}
void setp(const ll n){cout << fixed << setprecision(n);}
const ll INF = 1e9+1;
const ll LINF = 1e18+1;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const ld PI = acos(-1);
const ld EPS = 1e-11;
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
template<typename T> void co(T e){cout << e << "\n";}
template<typename T> void co(const vector<T>& v){for(const auto& e : v)
    { cout << e << " "; } cout << "\n";}

int dp[1000][1000];
char grid[1000][1000];
int h, w;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};


bool search(int sy, int sx){
    deque<pii> q1;
    deque<pii> q2;
    q1.push_back({sy,sx});
    q2.push_back({sy,sx});
    dp[sy][sx]=0;
    set<pii> s;
    while(!q1.empty()|!q2.empty()) {
        while (!q1.empty()) {
            auto pos = q1.front();
            int uy = pos.first;
            int ux = pos.second;
            q1.pop_front();
            rep(k, 4) {
                int i = dx[k];
                int j = dy[k];
                int ny = uy + i, nx = ux + j;
                if (0<=ny&&ny<h&& 0 <= nx && nx < w && grid[ny][nx] == '.' && dp[ny][nx] > dp[uy][ux] + 1) {
                    dp[ny][nx] = dp[uy][ux];
                    q1.push_back({ny, nx});
                    q2.push_back({ny, nx});
                }
            }
        }
        while (!q2.empty()) {
            auto pos = q2.front();
            int uy = pos.first;
            int ux = pos.second;
            q2.pop_front();
            REP(i, -2, 3) REP(j, -2, 3) {
                    int ny = uy + i, nx = ux + j;
                    if (0<=ny&&ny<h&& 0 <= nx && nx < w && grid[ny][nx] == '.' && dp[ny][nx] > dp[uy][ux] + 1) {
                        dp[ny][nx] = dp[uy][ux] + 1;
                        q1.push_back({ny, nx});
                        q2.push_back({ny, nx});
                    }
                }
        }
    }
}

int main() {
    cin >> h>>w;
    int sx,sy,gx,gy;
    cin>>sy>>sx;
    cin>>gy>>gx;
    sx-=1;sy-=1;gy-=1;gx-=1;
    rep(i,h) rep(j,w) dp[i][j]=INF;
    rep(i,h)rep(j,w) cin>>grid[i][j];
    search(sy, sx);
    if(dp[gy][gx]==INF) co(-1);
    else co(dp[gy][gx]);
}