#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct T {
	int v, x, y;
	T() {
	}
	T(int d, int x, int y) :
			v(d), x(x), y(y) {
	}
};

int h, w, ax, ay, bx, by, d[1000][1000];
bool b[1000][1000], seen[1000][1000];

auto comp = [](T &x, T &y) {
	return x.v > y.v;
};
priority_queue<T, vector<T>, decltype(comp)> q(comp);

int main() {
	cin >> h >> w >> ax >> ay >> bx >> by;
	--ax;
	--ay;
	--bx;
	--by;
	for (int i = 0; i < h; ++i) {
		string s;
		cin >> s;
		for (int j = 0; j < w; ++j)
			if (s[j] == '.')
				b[i][j] = 1;
	}

	q.emplace(0, ax, ay);
	while (!q.empty()) {
		int v, x, y;
		v = q.top().v;
		x = q.top().x;
		y = q.top().y;
		q.pop();

		if (seen[x][y])
			continue;
		seen[x][y] = 1;
		d[x][y] = v;
		if (x == bx && y == by) {
			{
				cout << v << endl;
				return 0;
			}
		}
		for (int i = -2; i < 3; ++i)
			for (int j = -2; j < 3; ++j) {
				int nx = x + i;
				int ny = y + j;
				if (nx < 0 || nx >= h || ny < 0 || ny >= w || !b[nx][ny]
						|| seen[nx][ny])
					continue;
				int nd = abs(i) + abs(j) > 1;
				q.emplace(v + nd, nx, ny);
			}
	}

	cout << -1 << endl;
}
