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 main() {
	enum Inf = int.max / 2 - 1;

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

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

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

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


	auto warpTable = new int[][](h, w);
	foreach (line;warpTable) {
		line[] = Inf;
	}

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

	struct Position {
		int y, x;
	}

	bool walk(int y, int x) {
		auto warpCount = warpTable[y][x];
		auto queue = new Position[4];
		queue[0] = Position(y - 1, x);
		queue[1] = Position(y + 1, x);
		queue[2] = Position(y, x - 1);
		queue[3] = Position(y, x + 1);
		bool walked;

		while (!queue.empty) {
			auto pos = queue.front;
			queue.popFront;

			if (!isValid(pos.y, pos.x)) {
				continue;
			}
			if (maze[pos.y][pos.x] != '.') {
				continue;
			}
			if (warpTable[pos.y][pos.x] <= warpCount) {
				continue;
			}

			warpTable[pos.y][pos.x] = warpCount;
			walked = true;

			queue ~= Position(pos.y - 1, pos.x);
			queue ~= Position(pos.y + 1, pos.x);
			queue ~= Position(pos.y, pos.x - 1);
			queue ~= Position(pos.y, pos.x + 1);
		}

		return walked;
	}

	bool warp(int y, int x) {
		auto warpCount = warpTable[y][x] + 1;
		bool warped;

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

		return warped;
	}

	warpTable[cy][cx] = 0;

	int warpCount = 0;
	while (true) {
		bool walked = false;
		foreach (i;0..h) {
			foreach (j;0..w) {
				if (warpCount == warpTable[i][j]) {
					walked |= walk(i, j);
				}
			}
		}

		bool warped = false;
		foreach (i;0..h) {
			foreach (j;0..w) {
				if (warpCount == warpTable[i][j]) {
					warped |= warp(i, j);
				}
			}
		}
		if (!walked && !warped) {
			break;
		}

		++warpCount;
	}

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