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;

const long long INF = 1LL << 60;

bool foot_visited[3010][3010];
bool magic_visited[3010][3010];

long long magic[3010][3010];
vector<string> field;

int H, W;

// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};


// void foot_bfs(int h, int w, long long magic_cnt) {
//     foot_visited[h][w] = true;
//     magic[h][w] = min(magic[h][w], magic_cnt);

    

//     // 四方向を探索
//     for (int dir = 0; dir < 4; ++dir) {
//         int nh = h + dx[dir];
//         int nw = w + dy[dir];

//         // 場外アウトしたり、移動先が壁の場合はスルー
//         if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
//         if (field[nh][nw] == '#') continue;

//         // 移動先が探索済みの場合
//         if (foot_visited[nh][nw]) continue;

//         magic_dfs()

//         // 再帰的に探索
//         dfs(nh, nw, magic_cnt1);
//     }

// }


int main(){
    cin >> H >> W;
    int CH, CW; cin >> CH >> CW;
    CH--; CW--;
    int DH, DW; cin >> DH >> DW;
    DH--; DW--;

    //init
    for(int i=0; i<H; ++i){
        for(int j=0; j<W; ++j){
            magic[i][j] = INF;
        }
    }

    field.resize(H);
    for(int h=0; h<H; ++h) cin >> field[h];

    int magic_cnt = 0;

    //BFS
    queue<pair<pair<int, int>, long long>> que;
    magic[CH][CW] = 0;

    que.push({{CH, CW}, 0});
    while(!que.empty()){
        int h = que.front().first.first;
        int w = que.front().first.second;
        long long cnt = que.front().second;

        foot_visited[h][w] = true;
        que.pop();
        //歩く場合
        for (int dir = 0; dir < 4; ++dir) {
            magic[h][w] = min(magic[h][w], cnt);
            int nh = h + dx[dir];
            int nw = w + dy[dir];

            // 場外アウトしたり、移動先が壁の場合はスルー
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == '#') continue;

            // 移動先が探索済みの場合
            if (foot_visited[nh][nw]) continue;
            que.push({{nh, nw}, cnt});
        }

        //魔法の場合
        for(int dx = -2; dx <= 2; ++dx){
            for(int dy = -2; dy <= 2; ++dy){
                magic_visited[h][w] = true;
                magic[h][w] = min(magic[h][w], cnt);
                int nh = h + dx;
                int nw = w + dy;
                long long new_cnt = cnt +1;

                // 場外アウトしたり、移動先が壁の場合はスルー
                if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
                if (field[nh][nw] == '#') continue;

                // 移動先が探索済みの場合
                if (magic_visited[nh][nw]) continue;
                if (foot_visited[nh][nw]) continue;
                que.push({{nh, nw}, new_cnt});
            }
        }

    }


    // // 頂点 s をスタートとした探索
    // vector<int> dist(N, -1);
    // queue<int> que;
    // dist[s] = 0, que.push(s);
    // while (!que.empty()) {
    //     int v = que.front(); que.pop();
    //     for (auto nv : G[v]) {
    //         if (dist[nv] == -1) {
    //             dist[nv] = dist[v] + 1;
    //             que.push(nv);
    //         }
    //     }
    // }

    long long ans = magic[DH][DW];
    if(ans == INF){
        cout << -1 << endl;
    }else{
        cout << ans << endl;
    }

}