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: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;
 
typedef long long int ll;
typedef unsigned long long int ull;
 
typedef vector <int> vi;
typedef vector <ll> vll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
 
typedef vector < pii > vpii;
typedef vector < pll > vpll;
 
typedef vector <string> vs;
typedef vector < vi > vvi;
typedef vector < vll > vvll;
 
#define fl(i, a, b)    for (int i(a); i <= (b); i ++)
#define rep(i, n)        fl (i, 1, (n))
#define loop(i, n)        fl (i, 0, (n) - 1)
#define rfl(i, a, b)    for (int i(a); i >= (b); i --)
#define rrep(i, n)        rfl (i, (n), 1)
 
#define all(v)     (v).begin(), (v).end()
#define srt(v)    sort (all (v))
 
#define pb push_back
#define mp make_pair
 
#define sz(x) ((int) (x).size())
#define fill (x, y)    memset(x, y, sizeof(x))
#define clr(a)    fill(a, 0)
 
#define PI 3.14159265358979323
 
const int Maxn = 1e5 + 1, Mod = 1e9 + 7;
#define trace1(x1)                    cerr << #x1 << ": " << x1 << endl;
#define trace2(x1, x2)                cerr << #x1 << ": " << x1 << " | " << #x2 << ": " << x2 << endl;
#define trace3(x1, x2, x3)            cerr << #x1 << ": " << x1 << " | " << #x2 << ": " << x2 << " | " << #x3 << ": " << x3 << endl;
 
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
 
const ll MOD = 1000000007LL;
const ll MAX = 100010LL;

// #define endl '\n'
 
// template <typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b);}
// template <typename T> T power(T x, T y, ll m = MOD) { T ans = 1; x %= m; while (y > 0) { if (y & 1ll) ans = (ans * x) % m; y >>= 1ll; x = (x * x)%m; } return ans%m;}
 
// template<typename T, typename T1>
// T mod(T x, T1 p) {
//     x %= p;
//     if (x < 0)
//         x += p;
//     return x;
// }
 
// int inv[Maxn], ifact[Maxn];
// template<typename T>
// T inverse(T x, T p) 
// {
//     x = mod(x, p);
//     if (x == 1)
//         return x;
//     return mod((1LL * (-p / x) * (inv[p % x] % p)) , p); 
//     // Since inverse of p % x is already calculated.
// }
 
// ifact[0] = 1;
// for(int i = 1; i < Maxn; i++) 
// {
//     inv[i] = inverse(i, Mod);
//     ifact[i] = (1LL * ifact[i - 1] * inv[i]) % Mod;
// }
 
// int fact[Maxn];
// fact[0] = 1;
// for(int i = 1; i < Maxn; i++) 
// {
//     fact[i] = 1LL * fact[i - 1] * i % Mod;
// }
 
// int n, t[4*MAXN];
// void build(int a[], int v, int tl, int tr) {
//     if (tl == tr) {
//         t[v] = a[tl];
//     } else {
//         int tm = (tl + tr) / 2;
//         build(a, v*2, tl, tm);
//         build(a, v*2+1, tm+1, tr);
//         t[v] = t[v*2] + t[v*2+1];
//     }
// }
// int sum(int v, int tl, int tr, int l, int r) {
//     if (l > r) 
//         return 0;
//     if (l == tl && r == tr) {
//         return t[v];
//     }
//     int tm = (tl + tr) / 2;
//     return sum(v*2, tl, tm, l, min(r, tm))
//            + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
// }
// void update(int v, int tl, int tr, int pos, int new_val) {
//     if (tl == tr) {
//         t[v] = new_val;
//     } else {
//         int tm = (tl + tr) / 2;
//         if (pos <= tm)
//             update(v*2, tl, tm, pos, new_val);
//         else
//             update(v*2+1, tm+1, tr, pos, new_val);
//         t[v] = t[v*2] + t[v*2+1];
//     }
// }
 
// int dif(string a, string b)
// {
// 	int cnt = 0;
// 	for(int i = 0 ; i < a.length() ; i++)
// 	{
// 		if(a[i]!=b[i])
// 			cnt++;
// 		if(cnt>1)
// 			return 0;
// 	}
// 	return 1;
// }

// void rv1(vector<int> &vec, int n) {
// 	vec = vector<int> (n);
// 	fl(i, 0, n-1) cin >> vec[i];
// 	return;
// }

// void pv1(vector<int> &vec) {
// 	int n = vec.size();
// 	if (n == 0) return;
// 	loop (i, n) cout << vec[i] << " ";
// 	return;
// }

// void rv2(vector<vector<int>> &vec, int n, int m) {
// 	vec = vector<vector<int>> (n, vector<int> (m));
// 	fl(i, 0, n-1) {
// 		fl(j, 0, m-1) cin >> vec[i][j];
// 	}
// 	return;
// }

// void pv2(vector<vector<int>> &vec) {
// 	int n = vec.size();
// 	if (n == 0) return;
// 	int m = vec[0].size();
// 	if (m == 0) return;
// 	loop (i, n) {
// 		loop (j, m) cout << vec[i][j] << " ";
// 		cout << endl;
// 	}
// 	return;
// }

int n, m;

bool valid(int x, int y) {
 return (x >= 0 && x < n && y >= 0 && y < m);
}

int main() {
	FAST_IO;

	int T = 1;

	for (int t = 1; t <= T; t++) {
		cin >> n >> m;
		vvi vals(n, vi (m, INT_MAX));
		vector<vector<bool>> vis(n, vector<bool> (m, false));
		int c1, c2, d1, d2;
		cin >> c1 >> c2;
		cin >> d1 >> d2;
		c1--, d1--, c2--, d2--;
		vector<vector<char>> g(n, vector<char> (m));
		fl (i, 0, n-1) {
			fl (j, 0, m-1) {
				cin >> g[i][j];
			}
		}
		vis[d1][d2] = true;
		vals[d1][d2] = 0;
		queue<pii> q;
		q.push(mp(d1, d2));
		pii tmp;
		int xc, yc;
		while (!q.empty()) {
			tmp = q.front();
			xc = tmp.first;
			yc = tmp.second;
			q.pop();
			if (valid(xc+1, yc)) {
				if (g[xc+1][yc] == '.') {
					if (vals[xc+1][yc] > vals[xc][yc]) {
						vals[xc+1][yc] = vals[xc][yc];
						q.push(mp(xc+1, yc));
					}
				}
			} 

			if (valid(xc-1, yc)) {
				if (g[xc-1][yc] == '.') {
					if (vals[xc-1][yc] > vals[xc][yc]) {
						vals[xc-1][yc] = vals[xc][yc];
						q.push(mp(xc-1, yc));
					}
				}
			} 

			if (valid(xc, yc+1)) {
				if (g[xc][yc+1] == '.') {
					if (vals[xc][yc+1] > vals[xc][yc]) {
						vals[xc][yc+1] = vals[xc][yc];
						q.push(mp(xc, yc+1));
					}
				}
			} 

			if (valid(xc, yc-1)) {
				if (g[xc][yc-1] == '.') {
					if (vals[xc][yc-1] > (vals[xc][yc])) {
						vals[xc][yc-1] = vals[xc][yc];
						q.push(mp(xc, yc-1));
					}
				}
			} 
			fl (i, max(0, tmp.first-2), min(n-1, tmp.first+2)) {
				fl (j, max(0, tmp.second-2), min(m-1, tmp.second+2)) {
					if (g[i][j] == '.') {
						if (vals[i][j] > (vals[tmp.first][tmp.second] + 1)) {
							vals[i][j] = vals[tmp.first][tmp.second] + 1;
							q.push(mp(i, j));
						}
					}
				}
			}
		}

		if (vals[c1][c2] == INT_MAX) cout << -1 << endl;
		else cout << vals[c1][c2] << endl;
	}

	return 0;
}