function main(lines) {
  const [H, W] = na();
  const [Ch, Cw] = sa().map(v => v-1);
  const [Dh, Dw] = sa().map(v => v-1);
  const maze = getLines(H);
  
  const deque = [[Ch, Cw, 0]];
  const done = Array(H).fill(0).map(_ => Array(W).fill(false));
  while (deque.length) {
    const [h, w, cost] = deque.shift();
    if (h == Dh && w == Dw) return cost;
    done[h][w] = true;
    for (let _h = h-2; _h <= h+2; _h++) {
      if (!maze[_h]) continue;
      for (let _w = w-2; _w <= w+2; _w++) {
        if (maze[_h][_w] != '.' || done[_h][_w]) continue;
        if (Math.hypot(_h-h, _w-w) <= 1) {
          deque.unshift([_h, _w, cost]);
        } else {
          deque.push([_h, _w, cost+1]);
        }
      }
    }
  }
  return -1;
}



const lines = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n");

let lineCount = 0;
const s = () => lines[lineCount++];
const n = () => Number(s());
const b = () => BigInt(s());
const sa = () => s().split(' ');
const na = () => sa().map(Number);
const ba = () => sa().map(BigInt);
const getLines = (n) => lines.slice(lineCount, lineCount += n);

const ans = main(lines);
console.log( ans );