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 inf 0x3f3f3f3f
#define mes(a,b) memset((a),(b),sizeof((a)))
#define syio std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0);
#define lson l, mid, rt << 1
#define rson mid+1, r, rt << 1 | 1
typedef long long ll;
typedef long double real;
typedef pair<int, int> pii;

const int oo = ~0u >> 2;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int mxn = 1003;
inline int read(){
    int x = 0,f = 1;char c = getchar();
    while(!isdigit(c))f = c == '-' ? -1 : 1,c = getchar();
    while(isdigit(c))x = x * 10 + c - '0',c = getchar();
    return x * f;
}
int h, w, sx, sy, ex, ey, mf[1003][1003];
char g[1003][1003];
bool v1[1003][1003], v2[1003][1003];
int dx[] = {0 , 1, 0 , -1};
int dy[] = {1 , 0, -1 , 0};
struct node
{
    int x, y;
};
bool check(int x, int y)
{
    if (x >= 0 && x < h && y >= 0 && y < w && g[x][y] == '.')
    return 1;
    return 0;
}
void bfs()
{
    queue<node> q;
    q.push(node{sx, sy}); 
    v1[sx][sy] = 1;  v2[sx][sy] = 1;
    mf[sx][sy] = 0;
    while (!q.empty())
    {
        node t = q.front(); q.pop();
        if (t.x == ex && t.y == ey) return ;
        for (int i = 0; i < 4; ++ i)
        {
            int nx = t.x + dx[i], ny = t.y + dy[i];
            if (check(nx, ny) && !v1[nx][ny])
            {
                mf[nx][ny] = min(mf[nx][ny], mf[t.x][t.y]);
                q.push(node{nx, ny});
                v1[nx][ny] = 1;
            }
        }
        for (int i = t.x - 2; i <= t.x + 2; ++ i)
        for (int j = t.y - 2; j <= t.y + 2; ++ j)
        {
            int nx = i, ny = j;
            if (check(i, j) && !v1[i][j] && !v2[i][j])
            {
                mf[nx][ny] = min(mf[nx][ny], mf[t.x][t.y] + 1);
                q.push(node{nx, ny});
                v2[nx][ny] = 1;
            }
        }
    }
}
int main()
{
    syio;
    cin>>h>>w;
    cin>>sx>>sy>>ex>>ey;
    sx -= 1; sy -= 1; ex -= 1; ey -= 1;
    for (int i = 0; i < h; ++ i) cin>>g[i];
    fill(mf[0], mf[0]+mxn*mxn, 1e9);
    // for (int i)
    bfs();
    if (mf[ex][ey] == inf) cout<<-1<<endl;
    else cout<<mf[ex][ey]<<endl;
    return 0;
}