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 DBG

using namespace std;
using ll = long long;
int H, W;
int Ch, Cw, Dh, Dw;
char b[1000][1000];
ll memo[1000][1000];
vector<ll> A;

struct Corr {
    ll y;
    ll x;
    ll warpcnt;
    Corr(ll in_y, ll in_x){
        warpcnt = 0;
        y = in_y;
        x = in_x;
    }
    Corr(ll in_y, ll in_x, ll in_warpcnt){
        warpcnt = in_warpcnt;
        y = in_y;
        x = in_x;
    }
};

void printboard(){
        for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            cout << memo[i][j] << ',' << flush;
        }
        cout << endl;
    }

}

ll minwarp = LLONG_MAX;

int main(){
    cin >> H >> W;
    cin.ignore();
    cin >> Ch >> Cw;
    --Ch;
    --Cw;
    cin.ignore();
    cin >> Dh >> Dw;
    --Dh;
    --Dw;
    cin.ignore();
 
    for(int i = 0; i < 1000; i++){
        for(int j = 0; j < 1000; j++){
            memo[i][j] = LLONG_MAX;
        }
    }
 
    for(int i = 0; i < H; i++){
        string s;
        cin >> s;
        for(int j = 0; j < W; j++){
            b[i][j] = s[j];
        }
        cin.ignore();
    }
 
    queue<Corr> q;
    q.push(Corr(Ch, Cw));
    while(!q.empty()){
        Corr now = q.front();
        q.pop();
        // マスにスコアつけ
        if(memo[now.y][now.x] <= now.warpcnt && memo[now.y][now.x] != LLONG_MAX) continue; // コスト大or訪問済みなら省略
        memo[now.y][now.x] = now.warpcnt;
        if(now.y == Dh && now.x == Dw) continue; // 目的地なら終了
        // -Y
        if(now.y > 0){
            if(b[now.y - 1][now.x] == '.' && (memo[now.y - 1][now.x] > now.warpcnt)) q.push(Corr(now.y - 1, now.x, now.warpcnt));
        }
        // -X
        if(now.x > 0){
            if(b[now.y][now.x - 1] == '.' && memo[now.y][now.x - 1] > now.warpcnt) q.push(Corr(now.y, now.x - 1, now.warpcnt));
        }
        // +Y
        if(now.y < H - 1){
            if(b[now.y + 1][now.x] == '.' && memo[now.y + 1][now.x] > now.warpcnt) q.push(Corr(now.y + 1, now.x, now.warpcnt));
        }
        // +X
        if(now.x < W - 1){
            if(b[now.y][now.x + 1] == '.' && memo[now.y][now.x + 1] > now.warpcnt) q.push(Corr(now.y, now.x + 1, now.warpcnt));
        }
 
        // ワープあり
        ll low_X = (now.x - 2 < 0) ? 0 : now.x - 2;
        ll up_X = (now.x + 2 > W - 1) ? W - 1 : now.x + 2;
        ll low_Y = (now.y - 2 < 0) ? 0 : now.y - 2;
        ll up_Y = (now.y + 2 > H - 1) ? H - 1 : now.y + 2;
        for(ll i = low_Y; i <= up_Y; i++){
            if(i < 0) continue;
            if(i > H - 1) continue;
            if(b[i][low_X] == '.' && memo[i][low_X] > now.warpcnt + 1) q.push(Corr(i, low_X, now.warpcnt + 1));
            if(b[i][up_X] == '.' && memo[i][up_X] > now.warpcnt + 1) q.push(Corr(i, up_X, now.warpcnt + 1));
        }
        for(ll i = low_X; i <= up_X; i++){
            if(i < 0) continue;
            if(i > W - 1) continue;
            if(b[low_Y][i] == '.' && memo[low_Y][i] > now.warpcnt + 1) q.push(Corr(low_Y, i, now.warpcnt + 1));
            if(b[up_Y][i] == '.' && memo[up_Y][i] > now.warpcnt + 1) q.push(Corr(up_Y, i, now  .warpcnt + 1));
        }
        for(ll i = low_Y; i <= up_Y; i++){
            if(i < 0) continue;
            if(i > H - 1) continue;
            for(ll j = low_X; j <= up_X; j++){
                if(b[i][j] == '.' && memo[i][low_X] == LLONG_MAX) q.push(Corr(i, j, now.warpcnt + 1));
                if(b[i][j] == '.' && memo[i][up_X] == LLONG_MAX) q.push(Corr(i, j, now.warpcnt + 1));
            }
        }
        //if(low_Y <= Dh && Dh <= up_Y && low_X <= Dw && Dw <= up_X){
        //    if(b[Dh][Dw] == '.' && memo[Dh][Dw] > now.warpcnt + 1) q.push(Corr(Dh, Dw, now.warpcnt + 1));
        //}
    }
 
    if(memo[Dh][Dw] == LLONG_MAX){
        cout << -1 << endl;
    } else {
        cout << memo[Dh][Dw] << endl;
    }
 
#ifdef DBG
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            cout << memo[i][j] << ',' << flush;
        }
        cout << endl;
    }
#endif
    
    return 0;
}