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;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP1(i,n) for(int i=1, i##_len=(n); i<i##_len; ++i)
#define ll long long
static const int MAX = 10000;
static const ll INFTY = 1e12;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }



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

// 入力
// 入力
int H, W;
int sx, sy, gx, gy; 
vector<vector<char>> field;
vector<vector<int>> dist;
queue<pair<int, int>> que, visited;

// 探索
void bfs() {

    pair<int, int> now;
    // 初期条件 (頂点 0 を初期ノードとする)
    dist[sy][sx] = 0;
    now.first = sy; 
    now.second = sx; 
    que.push(now); 

    // 八方向を探索
    while(!que.empty() || !visited.empty()){

      if (!que.empty()) {
        pair<int, int> v = que.front();
        visited.push(v);
        que.pop();

        for (int i = 0; i <= 4; ++i) {
            int nh = v.first + dy[i], nw = v.second + dx[i];

            // 場外アウトしたり、0 だったりはスルー
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (field[nh][nw] == '#') continue;
            if (dist[nh][nw] != -1) continue;

            dist[nh][nw] = dist[v.first][v.second];
            pair<int, int> nv;
            nv.first = nh; nv.second = nw;
            que.push(nv);
        }
      }else{
        pair<int, int> v = visited.front();
        visited.pop();

        for (ll ix = -2; ix <= 2; ++ix) {
            for (ll jy = -2; jy <= 2; ++jy) {
              int nh = v.first + jy, nw = v.second + ix;

              // 場外アウトしたり、0 だったりはスルー
              if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
              if (field[nh][nw] == '#') continue;
              if (dist[nh][nw] != -1) continue;

              dist[nh][nw] = dist[v.first][v.second]+1;
              pair<int, int> nv;
              nv.first = nh; nv.second = nw;
              que.push(nv);
            }
        }
      }
    }    
}


int main() {

    cin >> H >> W;
    cin >> sy >> sx; sx--; sy--;
    cin >> gy >> gx; gx--; gy--;
    // vector<pair<int, int>> gx(N), gy(N);
    field.assign(H, vector<char>(W));
    dist.assign(H, vector<int>(W, -1));
    for (int h = 0; h < H; ++h) {
      for (int w = 0; w < W; ++w) {
        cin >> field[h][w];
        if( field[h][w] == 'S' ) { sy = h; sx = w; } 
        // REP(i, N){
        //     if( field[h][w] == '1' + i ) { gy[i].first = h; gx[i].first = w; }
        // }
      }
    }

    bfs();
    cout << dist[gy][gx] << endl;

    // if(dist[gx][gy] )

    // REP(i, H){
    //   REP(j, W){
    //     cout << dist[i][j] << " ";
    //   }cout << endl;
    // }

}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/