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: Accepted)

#include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout)
#define pii pair<int,int>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T>
void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }
const int maxn = 1e6 + 11;
const int mod=1e9+7;
int n,m,x,y,t;
char g[1100][1100];
int to(int x,int y){
    return (x-1)*m+y;
}
bool check(int x,int y){
    if(x<1||x>n||y<1||y>m||g[x][y]=='#') return false;
    return true;
}

bool vis[maxn];
LL dis[maxn];
int tot,head[maxn];
struct Edge{
    int to,nxt,w;
}e[maxn*100];
void add(int u,int v,int w){
//    cout<<"add "<<u<<" "<<v<<" "<<w<<endl;
    e[++tot]={v,head[u],w};head[u]=tot;
}
struct HeapNode{
    LL dis,u;
    bool operator<(const HeapNode b)const{
        return dis>b.dis;
    }
};
void dijkstra(int s){
    me0(vis);
    memset(dis,0x3f,sizeof(dis));
    dis[s]=0;
    priority_queue<HeapNode>q;
    q.push({0,s});
    while(!q.empty()){
        HeapNode tp = q.top();q.pop();
        int u = tp.u;
        if(vis[u]) continue;
        vis[u]=1;
        for(int i =head[u];i;i=e[i].nxt){
            int to=e[i].to;
            if(dis[to]>dis[u]+e[i].w){
                dis[to]=dis[u]+e[i].w;
                q.push({dis[to],to});
            }
        }
    }
}

int main(){
//    op;
    read(n);read(m);
    int cx,cy,dx,dy;
    read(cx);read(cy);read(dx);read(dy);
    fo(i,1,n) scanf("%s",g[i]+1);
    fo(i,1,n){
        fo(j,1,m){
            int nx = i+1,ny = j;
            if(check(nx,ny)) add(to(i,j),to(nx,ny),0);
            nx = i-1,ny = j;
            if(check(nx,ny)) add(to(i,j),to(nx,ny),0);
            nx = i,ny = j+1;
            if(check(nx,ny)) add(to(i,j),to(nx,ny),0);
            nx = i,ny = j-1;
            if(check(nx,ny)) add(to(i,j),to(nx,ny),0);
            fo(a,-2,2){
                fo(b,-2,2){
                    if(a==0&&b==0) continue;
                    nx = i+a,ny = j+b;
                    if(check(nx,ny)) add(to(i,j),to(nx,ny),1);
                }
            }
        }
    }

    dijkstra(to(cx,cy));

    if(dis[to(dx,dy)]>=INF) puts("-1");
    else printf("%d\n",dis[to(dx,dy)]);

    return 0;
}