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;
	}

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

	void visit(int warpCount, int x, int y) {
		if (!valid(x,y)) {
			return;
		}
		if (maze[y][x] == '#') {
			return;
		}
		if (warps[y][x] <= warpCount) {
			return;
		}

		warps[y][x] = warpCount;

		for (int i = y - 2;i <= y + 2;++i) {
			for (int j = x - 2;j <= x + 2;++j) {
				if (valid(j, i)) {
					visit(warpCount + 1, j, i);
				}
			}
		}

		visit(warpCount, x, y - 1);
		visit(warpCount, x, y + 1);
		visit(warpCount, x - 1, y);
		visit(warpCount, x + 1, y);
	}

	visit(0, cx, cy);

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