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

#define FOR(i,a,b) for (int i = a; i < b; ++i)
#define FORR(i,a,b) for (int i = b - 1; i >= a; --i)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) FORR(i,0,n)

//#define DEBUG_MODE

int H, W, Ch, Cw, Dh, Dw;
vector<vector<bool>> road, track;

void showRoad() {
	cout << "===== Road =====" << endl;
	REP(i, H) {
		REP(j, W) {
			if (road[i][j]) cout << " ";
			else cout << "#";
		}
		cout << endl;
	}
}

void showScore(vector<vector<int>>& score) {
	cout << "===== Score =====" << endl;
	REP(i, H) {
		REP(j, W) {
			cout << score[i][j] << "\t";
		}
		cout << endl;
	}
}

bool checkPosition(int h, int w) {
	if (h < 0 | H <= h | w < 0 | W <= w) return false;
	if (!road[h][w]) return false;
	return true;
}

int main() {
	// get input
	cin >> H >> W;
	cin >> Ch >> Cw;
	cin >> Dh >> Dw;

	road = vector<vector<bool>>(H, vector<bool>(W, true));
	REP(i, H) {
		string buf;
		cin >> buf;
		REP(j, W) {
			if (buf[j] == '#') road[i][j] = false;
		}
	}

#ifdef DEBUG_MODE
	showRoad();
#endif

	// align to 0-indexed
	Ch--;
	Cw--;
	Dh--;
	Dw--;

	// data initialization
	track = vector<vector<bool>>(H, vector<bool>(W, false));
	vector<vector<int>> score(H, vector<int>(W, numeric_limits<int>::max()));
	queue<pair<int, int>> con, mag;
	pair<int, int> adj[4] = {
		make_pair(0, -1),
		make_pair(0, 1),
		make_pair(-1, 0),
		make_pair(1, 0),
	};

	// init
	mag.push(make_pair(Dh, Dw));
	score[Dh][Dw] = 0;
	pair<int, int> s;		// subject to search

	// search
	while(mag.size() != 0) {
#ifdef DEBUG_MODE
		showScore(score);
#endif
		s = mag.front();
		mag.pop();

		if (track[s.first][s.second]) continue;	// already come here
		track[s.first][s.second] = true;


		// add magic position
#ifdef DEBUG_MODE
		cout << "Add magic position" << endl;
#endif
		for (int i = -2; i <= 2; ++i) {
			for (int j = -2; j <= 2; ++j) {
				int h = s.first + i;
				int w = s.second + j;
				if (i == 0 & j == 0) continue;	// current position
				if (!checkPosition(h, w)) continue;	// invalid position
				if (track[h][w]) continue;	// already come here
#ifdef DEBUG_MODE
				cout << "h: " << h << ", w: " << w << endl;
#endif
				score[h][w] = min(score[s.first][s.second] + 1, score[h][w]);
				mag.push(make_pair(h, w));
			}
		}

		// add adjacent position
#ifdef DEBUG_MODE
		cout << "Add adjacent position" << endl;
#endif
		REP(i, 4) {
			int h = s.first + adj[i].first;
			int w = s.second + adj[i].second;
			if (!checkPosition(h, w)) continue;
			if (track[h][w]) continue;
#ifdef debug_mode
			cout << "h: " << h << ", w: " << w << endl;
#endif
			score[h][w] = min(score[s.first][s.second], score[h][w]);
			con.push(make_pair(h, w));
		}

		while (con.size() != 0) {
#ifdef DEBUG_MODE
			showScore(score);
#endif
			s = con.front();
			con.pop();

			if (track[s.first][s.second]) continue;
			track[s.first][s.second] = true;

			// add magic position
#ifdef DEBUG_MODE
			cout << "Add magic position" << endl;
#endif
			for (int i = -2; i <= 2; ++i) {
				for (int j = -2; j <= 2; ++j) {
					int h = s.first + i;
					int w = s.second + j;
					if (i == 0 & j == 0) continue;	// current position
					if (!checkPosition(h, w)) continue;	// invalid position
					if (track[h][w]) continue;	// already come here
#ifdef debug_mode
					cout << "h: " << h << ", w: " << w << endl;
#endif
					score[h][w] = min(score[s.first][s.second] + 1, score[h][w]);
					mag.push(make_pair(h, w));
				}
			}

			// add adjacent position
#ifdef DEBUG_MODE
			cout << "Add adjacent position" << endl;
#endif
			REP(i, 4) {
				int h = s.first + adj[i].first;
				int w = s.second + adj[i].second;
				if (!checkPosition(h, w)) continue;
				if (track[h][w]) continue;
#ifdef debug_mode
				cout << "h: " << h << ", w: " << w << endl;
#endif
				score[h][w] = min(score[s.first][s.second], score[h][w]);
				con.push(make_pair(h, w));
			}
		}
	}

	if (score[Ch][Cw] == numeric_limits<int>::max()) cout << -1 << endl;
	else cout << score[Ch][Cw] << endl;

	return 0;
}

Measured memory_bytes: 8292