import std.container;
import std.range;
import std.algorithm;
import std.array;
import std.string;
import std.conv;
import std.stdio;
import std.container;

void chmax(T)(ref T a, in T b) {
	if (a < b) {
		a = b;
	}
}

void chmin(T)(ref T a, in T b) {
	if (a > b) {
		a = b;
	}
}

void main() {
	int h, w;
	readf(" %s %s\n", &h, &w);

	int cx, cy;
	readf(" %s %s\n", &cy, &cx);
	--cx;
	--cy;

	int goalX, goalY;
	readf(" %s %s\n", &goalY, &goalX);
	--goalX;
	--goalY;

	auto maze = new string[h];
	foreach (i;0..h) {
		maze[i] = readln.chomp;
	}

	enum Inf = int.max / 2 - 1;
	auto warps = new int[][](h, w);
	foreach (ref line;warps) {
		line[] = Inf;
	}

	struct Position {
		int x, y;
	}

	bool isValid(int x, int y) {
		return y >= 0 && y < h && x >= 0 && x < w;
	}

	auto queue = new Position[1];
	queue.length = 0;

	void forcePush(int x, int y) {
			queue ~= Position(x, y);
	}
	void push(int warpCount, int x, int y) {
		if (!isValid(x, y)) {
			return;
		}

		if (maze[y][x] == '#') {
			return;
		}

		if (warps[y][x] <= warpCount) {
			return;
		}

		queue ~= Position(x, y);
	}

	Position pop() {
		auto r = queue.front;
		queue.popFront;
		return r;
	}

	void walk(int x, int y) {
		auto warpCount = warps[y][x];
		forcePush(x, y);

		while (!queue.empty) {
			auto m = pop();
			chmin(warps[m.y][m.x], warpCount);

			push(warpCount, m.x, m.y - 1);
			push(warpCount, m.x, m.y + 1);
			push(warpCount, m.x - 1, m.y);
			push(warpCount, m.x + 1, m.y);
		}
	}

	bool warp(int warpCount, int x, int y) {
		auto warped = false;

		for (int i = y - 2;i <= y + 2;++i) {
			for (int j = x - 2;j <= x + 2;++j) {
				if (isValid(j, i) && maze[i][j] == '.' && warps[i][j] > warpCount) {
					warps[i][j] = warpCount;
					warped = true;
					walk(j, i);
				}
			}
		}

		return warped;
	}

	auto warpCount = 0;
	warps[cy][cx] = 0;
	walk(cx, cy);
	for (;;) {
		auto warped = false;

		foreach (int i;0..h) {
			foreach (int j;0..w) {
				if (warps[i][j] == warpCount) {
					if (warp(warpCount + 1, j, i)) {
						warped = true;
					}
				}
			}
		}

		if (!warped) {
			break;
		}
		++warpCount;
	}

	if (warps[goalY][goalX] == Inf) {
		writeln(-1);
	} else {
		writeln(warps[goalY][goalX]);
	}
}
