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 <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <math.h>
#include <cmath>
#include <ctime>
#include <stdlib.h>


using namespace std;

/*
#pragma GCC target("avx2") //AtCoderでは "avx" にする
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
*/

#define int long long
//#define endl "\n"
#define all(v)  v.begin(),v.end()
#define fir first
#define sec second
#define m_p make_pair
#define m_t make_tuple
#define rep(i,n)  for(int i=0; i<(int) (n); i++)
typedef pair<int, int> P;
typedef long double ld;
typedef long long ll;
template<class T> using PQ_inv = priority_queue<T, vector<T>, greater<T>>;

const double pai = 3.1415926535897;
const ll mod = 1000000007;
const int INF = 1000000021;
const ll LINF = 2000000000000000000;
const int MAX = 510000;
const ll MOD = 1000000007;
long long fac[MAX], finv[MAX], inv[MAX];


// テーブルを作る前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}
//n個を1個以上のx組のグループに分ける重複組み合わせはcom(n-1,x-1)
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
ll PER(int n, int k) {
	if (n < 0 || k < 0) return 0;
	if (n < k) return fac[n];
	return fac[n] * finv[n - k] % mod;
}

long long modinv(long long a, long long m) {
	long long b = m, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= m;
	if (u < 0) u += m;
	return u;
}
long long modpow(int a, int n) {
	int res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}


struct edge {
	int to, cost;
};
edge m_e(int xx, int yy) {
	edge ed;
	ed.to = xx;
	ed.cost = yy;
	return ed;
}
//x未満の要素数を返す二分探索関数
int b_s(vector<int>& vec, int xx) {
	return lower_bound(all(vec), xx) - vec.begin();
}
template<class T>void vecout(vector<T>& vec) { for (T t : vec) cout << t << " "; cout << endl; }
template<class T>void vecin(vector<T>& vec) { for (int i = 0; i < vec.size(); i++) cin >> vec[i]; }
bool chmax(int& xx, int yy) {
	if (xx < yy) {
		xx = yy;
		return true;
	}
	return false;
}
bool chmin(int& xx, int yy) {
	if (xx > yy) {
		xx = yy;
		return true;
	}
	return false;
}
double dist(int x, int y) {
	return sqrt(x * x + y * y);
}
double dist(int xx1, int xx2, int yy1, int yy2) {
	return sqrt(abs(xx1 - xx2) * abs(xx1 - xx2) + abs(yy1 - yy2) * abs(yy1 - yy2));
}
ll mypow(int nn, int kk) {
	ll xx = 1;
	rep(i, kk) {
		xx *= nn;
	}
	return xx;
}
int gcd(int xx, int yy) {
	int p = xx;
	int q = yy;
	if (q > p)swap(p, q);
	while (p % q != 0) {
		p %= q;
		swap(p, q);
	}
	return q;
}
int lcm(int xx, int yy) {
	return xx * yy / gcd(xx, yy);
}
bool prime(int xx) {
	if (xx <= 1) {
		return 0;
	}
	for (int i = 2; i * i <= xx; i++) {
		if (xx % i == 0) {
			return 0;
		}
	}
	return 1;
}



signed main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int h, w,cx,cy,dx,dy,c=0;
	cin >> h >> w >> cy >> cx >> dy >> dx;
	cx--;
	cy--;
	dx--;
	dy--;
	vector<vector<char>>vec(h, vector<char>(w,'x'));
	rep(i,h) {
		rep(j,w) {
			cin >> vec[i][j] ;
		}
	}
	{
		queue<P>que;
		que.push(m_p(cy, cx));
		while (!que.empty()) {
			int fi = que.front().first, se=que.front().second;
			que.pop();
			if (fi == dy && se == dx) {
				cout << 0 << endl;
				return 0;
			}
			vec[fi][se] = 'o';
			if (fi>0&&vec[fi - 1][se]=='.') {
				vec[fi - 1][se] = 'o';
				que.push(m_p(fi - 1, se));
			}
			if (fi < h - 1 && vec[fi + 1][se] == '.') {
				vec[fi + 1][se] = 'o';
				que.push(m_p(fi + 1, se));
			}
			if (se > 0 && vec[fi][se - 1] == '.') {
				vec[fi][se - 1] = 'o';
				que.push(m_p(fi, se - 1));
			}
			if (se < w - 1 && vec[fi][se + 1] == '.') {
				vec[fi][se + 1] = 'o';
				que.push(m_p(fi, se + 1));
			}
		}
	}
	/*
	rep(i, h) {
		rep(j, w) {
			cout << vec[i][j];
		}
		cout << endl;
	}
	*/
	while (1) {
		c++;
		queue<P>que;
		rep(i, h) {
			rep(j, w) {
				if (vec[i][j] != '.')continue;
				for (int k = i - 2; k < i + 3; k++) {
					if (k < 0 || k >= h)continue;
					for (int l = j - 2; l < j + 3; l++) {
						if (l < 0 || l >= w)continue;
						if (vec[k][l] == 'o') {
							que.push(m_p(i, j));
						}
					}
				}
			}
		}
		if (que.empty()) {
			cout << -1 << endl;
			return 0;
		}
		while (!que.empty()) {
			int fi = que.front().first, se = que.front().second;
			que.pop();
			if (fi == dy && se == dx) {
				cout << c << endl;
				return 0;
			}
			vec[fi][se] = 'o';
			if (fi > 0 && vec[fi - 1][se] == '.') {
				vec[fi - 1][se] = 'o';
				que.push(m_p(fi - 1, se));
			}
			if (fi < h - 1 && vec[fi + 1][se] == '.') {
				vec[fi + 1][se] = 'o';
				que.push(m_p(fi + 1, se));
			}
			if (se > 0 && vec[fi][se - 1] == '.') {
				vec[fi][se - 1] = 'o';
				que.push(m_p(fi, se - 1));
			}
			if (se < w - 1 && vec[fi][se + 1] == '.') {
				vec[fi][se + 1] = 'o';
				que.push(m_p(fi, se + 1));
			}
		}
	}
}