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)

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
 
#include<bits/stdc++.h>
 
using namespace std;
#define ll long long 
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ull unsigned long long
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0); 
#define nl "\n"
#define sz(a) (int)(a).size() 
#define all(a) (a).begin(),(a).end()
#define fl(a,x) memset(a,x,sizeof(a));
#define pt(a,x) cout << get<x>(a) << " " ;  
#define mt make_tuple 
#define vec vector
 
#define int long long 
 
const int mod = (int) 1e9 + 7 ;
const int MOD = (int) 998244353 ;
const int INF = (int) 1e18 ;
 
typedef vector<int> vii;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<vector<int> > vv;
 
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
 
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	err(++it, args...);
}

const int N = 1e3 + 5 ;
char c[N][N] ;
int mn[N][N] , h,w, X,Y,A,B;
bool vis[N][N] ;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int Dx[] = {-2,-2,-2,-2,-2,-1,-1,0,0,1,1,2,2,2,2,2} ;
int Dy[] = {2,1,0,-1,-2,2,-2,2,-2,2,-2,2,1,0,-1,-2} ;

void solve(int test) {

	cin >> h >> w ;
	cin >> A >> B ;
	cin >> X >> Y ;

	rep(i,1,h+1) rep(j,1,w+1) cin >> c[i][j] , mn[i][j] = INF ; 

	mn[A][B] = 0 ; 
	
	queue<pii> q ; q.push({A,B}) ;
	vis[A][B] = 1 ;

	while(not q.empty()) {
		int x = q.front().ff; int y = q.front().ss; q.pop() ;

		rep(i,0,4) {
			int nx = x + dx[i] , ny = y + dy[i] ;
			if(nx > h or nx < 1 or ny > w or ny < 1 ) continue ;
			if(vis[nx][ny]) continue ;
			if(c[nx][ny] == '#') continue; 
			if(mn[nx][ny]>mn[x][y]) {
				vis[nx][ny] = 1 ;
				mn[nx][ny] = mn[x][y]; 
				q.push({nx,ny}); 
			}
		}

		rep(i,0,16) {
			int nx = x + Dx[i] , ny = y + Dy[i] ;
			if(nx > h or nx < 1 or ny > w or ny < 1 ) continue ;
			if(vis[nx][ny]) continue ;
			if(c[nx][ny] == '#') continue; 
			if(mn[nx][ny] > 1 + mn[x][y]) {
				vis[nx][ny] = 1; 
				mn[nx][ny] = 1 + mn[x][y] ;
				q.push({nx,ny});
			}
		}
	}

	// rep(i,1,h+1) { rep(j,1,w+1) cout << mn[i][j] << " " ; cout << nl ;}

	if(mn[X][Y] == INF) cout << -1 << nl ;
	else cout << mn[X][Y] ;
}
 
int32_t main() {
 

 
    fast;
	int t=1;
	// cin>>t;
	rep(i,1,t+1) {
		solve(i) ;
	}
 
	// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	return 0;
}