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++ (Clang 10.0.0) (judge verdict: Runtime Error)

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include<sstream>
#include<numeric>
#include<queue>
#include<cstdio>
#include<iterator>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb(x) push_back(x)
ll mod = 1e9 + 7;
ll inf = 1e9 + 7;
template<typename T, typename U>
T binpow(T a, U n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1)
        return (binpow(a, n - 1) * a) % mod;
    else {
        ll b = binpow(a, n / 2) % mod;
        return (b * b) % mod;
    }
}
template<typename T>
T gcd(T a, T b) {
    if (b == 0)
        return a;
    else
        return gcd(b, a % b);
}
template<typename T>
istream& operator>>(istream& str, vector<T>& vec) {
    for (auto& w : vec) str >> w;
    return str;
}
template<typename T>
ostream& operator<<(ostream& str, vector<T>& vec) {
    for (auto& w : vec) str << w << ' ';
    return str;
}





int main() {
    ll h, w, ch, cw, dh, dw;
    cin >> h >> w >> ch >> cw >> dh >> dw; ch--; cw--; dh--; dw--;
    vector<vector<ll>>vec(h, vector<ll>(w, inf));
    vector<vector<bool>> mapp(h, vector<bool>(w));
    for (int i = 0; i < h; ++i) {
        for (int j = 0; j < w; ++j) {
            char c;
            cin >> c;
            if (c == '.')mapp[i][j] = 1;
        }
    }
    vec[ch][cw] = 0;
    for (int i = 0; i < max(h, w); ++i) {
        ll u = ch - i, d = ch + i, l = cw - i, r = cw + i;
        for (int ud = max(0ll, u); ud <= min(d, h - 1); ud++) {
            if (l < w && l >= 0) {
                if (ud + 1 < h && ud + 1 >= 0 && mapp[ud + 1][l] == 1) vec[ud + 1][l] = min(vec[ud + 1][l], vec[ud][l]);
                if (ud - 1 < h && ud - 1 >= 0 && mapp[ud - 1][l] == 1) vec[ud - 1][l] = min(vec[ud - 1][l], vec[ud][l]);
                if (l + 1 < w && l + 1 >= 0 && mapp[ud][l + 1] == 1) vec[ud][l + 1] = min(vec[ud][l + 1], vec[ud][l]);
                if (l - 1 < w && l - 1 >= 0 && mapp[ud][l - 1] == 1) vec[ud][l - 1] = min(vec[ud][l - 1], vec[ud][l]);
                for (int q = -2; q <= 2; ++q) {
                    for (int g = -2; g <= 2; ++g) {
                        if (ud + q < h && ud + q >= 0 
                            && l + g < w && l + g >= 0 
                            && mapp[ud + q][l + g] == 1) 
                            vec[ud + q][l + g] = min(vec[ud + q][l + g], vec[ud][l] + 1);
                    }
                }
            }
            if (r < w && r >= 0) {
                if (ud + 1 < h && ud + 1 >= 0 && mapp[ud + 1][r] == 1) vec[ud + 1][r] = min(vec[ud + 1][r], vec[ud][r]);
                if (ud - 1 < h && ud - 1 >= 0 && mapp[ud - 1][r] == 1) vec[ud - 1][r] = min(vec[ud - 1][r], vec[ud][r]);
                if (r + 1 < w && r + 1 >= 0 && mapp[ud][r + 1] == 1) vec[ud][r + 1] = min(vec[ud][r + 1], vec[ud][r]);
                if (r - 1 < w && r - 1 >= 0 && mapp[ud][r - 1] == 1) vec[ud][r - 1] = min(vec[ud][r - 1], vec[ud][r]);
                for (int q = -2; q <= 2; ++q) {
                    for (int g = -2; g <= 2; ++g) {
                        if (ud + q < h && ud + q >= 0
                            && r + g < w && r + g >= 0
                            && mapp[ud + q][r + g] == 1)
                            vec[ud + q][r + g] = min(vec[ud + q][r + g], vec[ud][r] + 1);
                    }
                }
            }
        }
        for (int lr = max(l, 0ll); lr <= min(r, w - 1); lr++) {
            if (u < h && u >= 0) {
                if (u + 1 < w && u + 1 >= 0 && mapp[u + 1][lr] == 1) vec[u + 1][lr] = min(vec[u + 1][lr], vec[u][lr]);
                if (u - 1 < h && u - 1 >= 0 && mapp[u - 1][l] == 1) vec[u - 1][lr] = min(vec[u - 1][lr], vec[u][lr]);
                if (lr + 1 < w && lr + 1 >= 0 && mapp[u][lr + 1] == 1) vec[u][lr + 1] = min(vec[u][lr + 1], vec[u][lr]);
                if (lr - 1 < w && lr - 1 >= 0 && mapp[u][lr - 1] == 1) vec[u][lr - 1] = min(vec[u][lr - 1], vec[u][lr]);
                for (int q = -2; q <= 2; ++q) {
                    for (int g = -2; g <= 2; ++g) {
                        if (u + q < h && u + q >= 0
                            && lr + g < w && lr + g >= 0
                            && mapp[u + q][lr + g] == 1)
                            vec[u + q][lr + g] = min(vec[u + q][lr + g], vec[u][lr] + 1);
                    }
                }
            }
            if (d < h && d >= 0) {
                if (d + 1 < h && d + 1 >= 0 && mapp[d + 1][lr] == 1) vec[d + 1][lr] = min(vec[d + 1][lr], vec[d][lr]);
                if (d - 1 < h && d - 1 >= 0 && mapp[d - 1][lr] == 1) vec[d - 1][lr] = min(vec[d - 1][lr], vec[d][lr]);
                if (lr + 1 < w && lr + 1 >= 0 && mapp[d][lr + 1] == 1) vec[d][lr + 1] = min(vec[d][lr + 1], vec[d][lr]);
                if (lr - 1 < w && lr - 1 >= 0 && mapp[d][lr - 1] == 1) vec[d][lr - 1] = min(vec[d][lr - 1], vec[d][lr]);
                for (int q = -2; q <= 2; ++q) {
                    for (int g = -2; g <= 2; ++g) {
                        if (d + q < h && d + q >= 0
                            && lr + g < w && lr + g >= 0
                            && mapp[d + q][lr + g] == 1)
                            vec[d + q][lr + g] = min(vec[d + q][lr + g], vec[d][lr] + 1);
                    }
                }
            }
        }
        
    }
    if (vec[dh][dw] >= inf) {
        cout << -1;
    }
    else {
        cout << vec[dh][dw];
    }
}