#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <map>
#include <deque>
#include <queue>
#include <tuple>
using namespace std;

#define prec(i) cout << fixed << setprecision(i)

void output_yn(bool isYes) {
	if (isYes)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}

const long long MOD = 1000000007;

long long fact(long long k) {
	long long sum = 1;
	for (long long i = 2; i <= k; i++)
		sum *= i;

	return sum;
}

long long fact_mod(long long k) {
	long long sum = 1;
	for (long long i = 2; i <= k; i++)
		sum = (sum * i) % MOD;

	return sum;
}

int ctoi(char c) {
	if (c >= '0' && c <= '9')
		return c - '0';
	else
		return -1;
}

long long pow_mod(int a, int b) {
	long long ans = 1;
	for (int i = 0; i < b; i++)
		ans = (ans * a) % MOD;

	return ans;
}

long long powll(long long a, long long  b) {
	long long ans = 1;
	for (long long i = 0; i < b; i++)
		ans = ans * a;

	return ans;
}

long long modinv(long long a) {

	long long b = MOD, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	u %= MOD;
	if (u < 0) u += MOD;

	return u;
}

long long _gcd(long long a, long long b)
{
	if (a < b)
		swap(a, b);

	if (a % b == 0)
		return b;
	else
		return _gcd(b, a % b);
}

long long _lcm(long long a, long long b)
{
	return (((a * b) % MOD) * modinv(_gcd(a, b))) % MOD;
}

long long ncr(long long n, long long r) {
	long long sum = 1;
	for (long long i = r + 1; i <= n; i++)
		sum *= i;

	return sum;
}


int main()
{
	int h, w;
	cin >> h >> w;

	pair<int, int> c, d;
	int _h, _w;
	cin >> _h >> _w;
	c = make_pair(_h - 1, _w - 1);
	cin >> _h >> _w;
	d = make_pair(_h - 1, _w - 1);

	vector<vector<bool>> a(h);
	vector<vector<int>> dist(h);

	for (int i = 0; i < h; i++) {
		vector<bool> _a(w);
		a[i] = _a;
		vector<int> _dist(w);
		dist[i] = _dist;
		
		for (int j = 0; j < w; j++) {
			char c;
			cin >> c;
			if (c == '#')
				a[i][j] = true;
			else
				a[i][j] = false;

			dist[i][j] = -1;
		}
	}

	vector<pair<int, int>> dir = {
		make_pair(-1, 0),
		make_pair(1, 0),
		make_pair(0, -1),
		make_pair(0, 1)
	};
	priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> que;
	que.push(make_pair(0, c));
	dist[c.first][c.second] = 0;

	pair<int, int> pos = c;
	while (!que.empty()) {

		pair<int, int> pos = que.top().second;
		que.pop();
		if (pos == d)
			break;
		for (int i = -2; i < 3; i++) {


			int _h = pos.first + i;

			if (_h < 0 || _h >= h)
				continue;

			for (int j = -2; j < 3; j++) {

				if (i == 0 && j == 0)
					continue;

				int _w = pos.second + j;

				if (_w < 0 || _w >= w)
					continue;

				if (a[_h][_w])
					continue;

				int newDist;
				if (abs(i) + abs(j) == 1)
					newDist = dist[pos.first][pos.second];
				else
					newDist = dist[pos.first][pos.second] + 1;

				if (dist[_h][_w] != -1 && dist[_h][_w] <= newDist)
					continue;

				dist[_h][_w] = newDist;
				que.push(make_pair(newDist, make_pair(_h, _w)));
			}
		}
	}

	cout << dist[d.first][d.second] << endl;

	return 0;
}

Measured memory_bytes: 7720