#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#include<stdio.h>
#include<functional>
#include<queue>

using ll = long long;

using namespace std;

ll mod = 1e9 + 7;
//ll mod = 998244353;

ll n_pow(ll a, ll n) {
	if (n == 0)return 1;
	ll t = n_pow(a, n / 2);
	t *= t;
	//t %= mod;
	if (n % 2 == 1)t *= a;
	return t /*% mod*/;
}

const int INF = 1001001001;
//const ll INF = 1e18;

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

ll cal_gcd(ll a, ll b) {
	ll m = max(a, b);
	ll n = min(a, b);
	while (m%n != 0) {
		ll b = m % n;
		m = n;
		n = b;
	}
	return n;
}

vector<pair<ll, ll>> prime_factorize(ll N) {
	vector<pair<ll, ll>> res;
	for (long long a = 2; a * a <= N; ++a) {
		if (N % a != 0) continue;
		long long ex = 0;
		while (N % a == 0) {
			++ex;
			N /= a;
		}
		res.push_back({ a, ex });
	}
	if (N != 1) res.push_back({ N, 1 });
	return res;
}


int main() {
	int h, w;
	cin >> h >> w;
	int sx, sy, gx, gy;
	cin >> sx >> sy >> gx >> gy;
	vector<vector<pair<bool,int>>>path(h, vector<pair<bool,int>>(w));
	int ans = 0;
	sx--; sy--; gx--; gy--;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			char c;
			cin >> c;
			if (c == '.') {
				path[i][j].first = true;
			}
			else path[i][j].first = false;
			path[i][j].second = INF;
		}
	}
	queue<pair<pair<int, int>, int>>todo;
	todo.push(pair<pair<int, int>, int>(pair<int, int>(sx, sy), 0));
	path[sx][sy].second = 0;
	while (todo.empty() != 1) {
		auto tmp = todo.front();
		todo.pop();
		int x = tmp.first.first;
		int y = tmp.first.second;
		int woop = tmp.second;
		if (woop > path[x][y].second)continue;
		queue<pair<pair<int, int>, int>>todo2;
		todo2.push(tmp);
		queue<pair<int, int>>island;
		while (todo2.empty() != 1) {
			auto tmp = todo2.front();
			todo2.pop();
			int x = tmp.first.first;
			int y = tmp.first.second;
			island.push(tmp.first);
			for (int dx = -1; dx <= 1; dx++) {
				for (int dy = -1; dy <= 1; dy++) {
					if (dx == 0 && dy == 0) {
						continue;
					}
					int nx = x + dx, ny = y + dy;
					if (nx < 0 || ny < 0 || nx >= h || ny >= w)continue;
					if (path[nx][ny].first) {
						if ((dx == 0 && abs(dy) == 1) || (abs(dx) == 1 && dy == 0)) {
							if (woop < path[nx][ny].second) {
								todo2.push(pair<pair<int, int>, int>(pair<int, int>(nx, ny), woop));
								path[nx][ny].second = woop;
							}

						}
					}
				}
			}
		}
		while (island.empty() != 1) {
			auto foo = island.front();
			island.pop();
			x = foo.first, y = foo.second;
			for (int dx = -2; dx <= 2; dx++) {
				for (int dy = -2; dy <= 2; dy++) {
					if (dx == 0 && dy == 0) {
						continue;
					}
					int nx = x + dx, ny = y + dy;
					if (nx < 0 || ny < 0 || nx >= h || ny >= w)continue;
					if (path[nx][ny].first) {
						if ((dx == 0 && abs(dy) == 1) || (abs(dx) == 1 && dy == 0)) {
							continue;
						}
						else {
							if (woop + 1 < path[nx][ny].second) {
								todo.push(pair<pair<int, int>, int>(pair<int, int>(nx, ny), woop + 1));
								path[nx][ny].second = woop + 1;
							}
						}
					}
				}
			}
		}
	}
	if (path[gx][gy].second == INF) {
		cout << -1 << endl;
	}
	else {
		cout << path[gx][gy].second << endl;
	}
	return 0;
}


Measured memory_bytes: 11496