import std.conv, std.functional, std.range, std.stdio, std.string; import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons; import core.bitop; class EOFException : Throwable { this() { super("EOF"); } } string[] tokens; string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; } int readInt() { return readToken.to!int; } long readLong() { return readToken.to!long; } real readReal() { return readToken.to!real; } bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } } bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } } int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; } int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); } int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); } enum INF = 10^^9 / 2; int H, W, SX, SY, TX, TY; string[] A; alias Pt = Tuple!(int, "x", int, "y"); Pt[] que; Tuple!(int[][], Pt[][]) bfs(bool needPrev)(int sx, int sy, int ox, int oy) { int qb, qe; auto dist = new int[][](H, W); Pt[][] prev; static if (needPrev) prev = new Pt[][](H, W); foreach (x; 0 .. H) foreach (y; 0 .. W) { dist[x][y] = INF; } dist[sx][sy] = 0; que[qe++] = Pt(sx, sy); for (; qb != qe; ) { const x = que[qb].x; const y = que[qb].y; ++qb; void update(int xx, int yy) { if (A[xx][yy] == '.' && !(xx == ox && yy == oy)) { if (chmin(dist[xx][yy], dist[x][y] + 1)) { static if (needPrev) prev[xx][yy] = Pt(x, y); que[qe++] = Pt(xx, yy); } } } update(x + 1, y); update(x, y + 1); update(x - 1, y); update(x, y - 1); } return tuple(dist, prev); } void main() { try { for (; ; ) { H = readInt(); W = readInt(); SX = readInt() - 1; SY = readInt() - 1; TX = readInt() - 1; TY = readInt() - 1; A = new string[H]; foreach (x; 0 .. H) { A[x] = readToken(); } que = new Pt[H * W]; const res = bfs!true(SX, SY, -1, -1); const dS = res[0]; const prev = res[1]; const dT = bfs!false(TX, TY, -1, -1)[0]; const dST = bfs!false(SX, SY, TX, TY)[0]; const dTS = bfs!false(TX, TY, SX, SY)[0]; int ans = INF; if (dS[TX][TY] < INF) { auto on = new bool[][](H, W); for (int x = TX, y = TY; ; ) { on[x][y] = true; if (x == SX && y == SY) { break; } const p = prev[x][y]; x = p.x; y = p.y; } foreach (x; 1 .. H - 1) foreach (y; 1 .. W - 1) { if (A[x][y] == '.' && !on[x][y]) { chmin(ans, dS[TX][TY] + dST[x][y] + dTS[x][y]); } } foreach (x; 1 .. H - 1) foreach (y; 1 .. W - 1) { if (A[x][y] == '.') { int cnt; if (A[x + 1][y] == '.') ++cnt; if (A[x][y + 1] == '.') ++cnt; if (A[x - 1][y] == '.') ++cnt; if (A[x][y - 1] == '.') ++cnt; if (cnt >= 3) { chmin(ans, 2 * (dS[x][y] + dT[x][y] + 2)); } } } } writeln((ans >= INF) ? -1 : ans); } } catch (EOFException e) { } }