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 <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef  long long int ll;
typedef long double db;
#define pb push_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define PI 3.1415926535
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define rep(i,n) for( ll i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll M = 1e9 + 7;
ll INF = 1e18;

ll n, m;
v<v<char>>s;
v<v<ll>> used;
v<v<ll>> warp;
v<v<ll>> con;
void dfs(ll i, ll j) {
	used[i][j] = 1;
	warp[i][j] = 0;
	v<pair<ll, ll>> move = {{1, 0}, {0, 1}, { -1, 0}, {0, -1}};
	for (auto mv : move) {
		ll x = i + mv.fi;
		ll y = j + mv.se;
		if (x >= 0 && x < n && y >= 0 && y < m) {
			if (s[x][y] == '.' && used[x][y] == 0) {
				dfs(x, y);
			}
		}
	}
}
void dfs2(ll i, ll j, ll l, ll r) {
	con[i - (l - 2)][j - (r - 2)] = 1;
	v<pair<ll, ll>> move = {{1, 0}, {0, 1}, { -1, 0}, {0, -1}};
	for (auto mv : move) {
		ll x = i + mv.fi;
		ll y = j + mv.se;
		if (x >= l - 2 && x >= 0 && x < n && y >= 0 && y < m && x <= l + 2 && y >= r - 2 && y <= r + 2) {
			if (s[x][y] == '.' && con[x - (l - 2)][y - (r - 2)] == 0) {
				dfs2(x, y, l, r);
			}
		}
	}
}
ll recur(ll i, ll j) {
	used[i][j] = 2;
	ll x, y;
	if (warp[i][j] == -1) {
		ll mn = INF;
		con.assign(5, v<ll>(5, 0));
		dfs2(i, j, i, j);
		for (x = i - 2; x <= i + 2; x++) {
			for (y = j - 2; y <= j + 2; y++) {
				if (x >= 0 && x < n && y >= 0 && y < m) {
					if (s[x][y] == '.' && used[x][y] != 2 ) {
						if (con[x - (i - 2)][y - (j - 2)] == 1) {
							mn = min(mn, recur(x, y));
						}
						else {
							mn = min(mn, 1LL + recur(x, y));
						}
					}
				}
			}
		}
		if (mn < INF)
			warp[i][j] = mn;
		else
			warp[i][j] = INF;
	}
	return warp[i][j];
}

int main() {
	// your code goes here
	IOS;

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	ll i, j, t, k, x, y, z,  N;
	cin >> n >> m;
	ll x1, x2, y1, y2;
	cin >> x1 >> y1;
	cin >> x2 >> y2;
	warp.assign(n, v<ll>(m, -1));
	s.assign(n, v<char>(m, '$'));
	used.assign(n, v<ll>(m, 0));
	rep(i, n) {
		rep(j, m) {
			cin >> s[i][j];
		}
	}
	dfs(x1 - 1, y1 - 1);
	x = recur(x2 - 1, y2 - 1);
	if (x == INF)
		cout << -1;
	else {
		cout << x;
	}




	return 0;
}