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 <ctype.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#ifdef __cplusplus
#include <bits/stdc++.h>
#endif
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")

#define getchar getchar_unlocked
#define putchar putchar_unlocked
int32_t nextint(void){ char c=getchar(); while(c!='-'&&(c<'0'||'9'<c)) c=getchar(); bool s=false; if(c=='-'){s=true;c=getchar();} uint32_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=getchar(); } return s?-x:x; }
int64_t nextlong(void){ char c=getchar(); while(c!='-'&&(c<'0'||'9'<c)) c=getchar(); int s=0; if(c=='-'){s=1;c=getchar();} uint64_t x=0; while('0'<=c && c<='9'){ x=x*10+c-'0'; c=getchar(); } return s?-x:x; }
uint32_t nextstr(char *s){ char c=getchar(); while(c==' '||c=='\n') c=getchar(); uint32_t len=0; while(c!=' '&&c!='\n'){ *s++=c; len++; c=getchar(); } *s='\0'; return len; }
void rsorta_d32(void *base, const int count, const int size, const int index){
	typedef int32_t rsort_t;
	rsort_t (*a)[size] = (rsort_t(*)[size])base;
	rsort_t (*b)[size] = (rsort_t(*)[size])malloc(count*size*sizeof(rsort_t));
	int c[0x100];
	for(int k=0; k<4; k++){
		memset(c, 0, sizeof(c)); for(int i=0; i<count; i++){ c[(a[i][index]>>(k<<3))&0xFF]++; } for(int i=1; i<0x100; i++) c[i]+=c[i-1]; for(int i=count-1; i>=0; i--){ int to=--c[(a[i][index]>>(k<<3))&0xFF]; for(int j=0; j<size; j++){ b[to][j]=a[i][j]; } }{rsort_t(*t)[size]=a; a=b; b=t;}
	}
	free(b);
}

char m[1000][1001];
int d[1000][1000];
int s[1000000][2];
int main(void){
	int h=nextint();
	int w=nextint();
	int ch=nextint()-1;
	int cw=nextint()-1;
	int dh=nextint()-1;
	int dw=nextint()-1;
	for(int i=0; i<h; i++){
		nextstr(m[i]);
	}
	int k=0;
	int sl=0, sr=0;
	d[ch][cw]=1;
	s[sl][0]=ch, s[sl][1]=cw, sr++;
	while(1){
		k++;
		if(sl==sr) break;
		int tl=sl;
		while(sl<sr){
			int x=s[sl][0], y=s[sl][1]; sl++;
			{int i=x-1, j=y; if(0<=i&&i<h&&0<=j&&j<w && m[i][j]=='.' && d[i][j]==0){ d[i][j]=k; s[sr][0]=i, s[sr][1]=j, sr++;}}
			{int i=x+1, j=y; if(0<=i&&i<h&&0<=j&&j<w && m[i][j]=='.' && d[i][j]==0){ d[i][j]=k; s[sr][0]=i, s[sr][1]=j, sr++;}}
			{int i=x, j=y-1; if(0<=i&&i<h&&0<=j&&j<w && m[i][j]=='.' && d[i][j]==0){ d[i][j]=k; s[sr][0]=i, s[sr][1]=j, sr++;}}
			{int i=x, j=y+1; if(0<=i&&i<h&&0<=j&&j<w && m[i][j]=='.' && d[i][j]==0){ d[i][j]=k; s[sr][0]=i, s[sr][1]=j, sr++;}}
		}
		if(d[dh][dw]!=0){
			printf("%d\n", k-1);
			return 0;
		}
		int tr=sr;
		while(tl<tr){
			int x=s[tl][0], y=s[tl][1]; tl++;
			if(d[x][y]==k){
				for(int i=x-2; i<=x+2; i++){
					for(int j=y-2; j<=y+2; j++){
						if(0<=i&&i<h&&0<=j&&j<w && m[i][j]=='.' && d[i][j]==0){
							d[i][j]=k+1;
							s[sr][0]=i, s[sr][1]=j, sr++;
						}
					}
				}
			}
		}
	}
	printf("%d\n", -1);
}