function main(lines) {
  const [H, W] = na();
  const [Ch, Cw] = na();
  const [Dh, Dw] = na();
  const maze = lines.slice(3).map(l => l.split(''));
  
  let points = new Set([`${Ch-1} ${Cw-1}`]);
  let warpCount = 0;
  let arrived, ans = -1;
  while (points.size) {
    arrived = search(maze, points, Dh-1, Dw-1);
    if (arrived) {
      ans = warpCount;
      break;
    }
    warpCount++;
    points = warp(maze, points);
  }
  
  return ans;
}

function search(graph, points, dh, dw) {
  for (const pStr of points) {
    const [h, w] = pStr.split(' ').map(Number);
    if (graph[h][w] != '.') continue;
    const queue = [[h, w]];
    while (queue.length) {
      let [h, w] = queue.shift();
      if (h == dh && w == dw) return true;
      graph[h][w] = true;
      points.add(`${h} ${w}`);
      if (graph[h-1] && graph[h-1][w] == '.') queue.push([h-1, w]);
      if (graph[h+1] && graph[h+1][w] == '.') queue.push([h+1, w]);
      if (graph[h][w-1] == '.') queue.push([h, w-1]);
      if (graph[h][w+1] == '.') queue.push([h, w+1]);
    }
  }
}

function warp(graph, fromPoints) {
  const points = new Set();
  for (const pStr of fromPoints) {
    const [h, w] = pStr.split(' ').map(Number);
    for (let _h = h-2; _h <= h+2; _h++) {
      for (let _w = w-2; _w <= w+2; _w++) {
        if (graph[_h] && graph[_h][_w] == '.') points.add(`${_h} ${_w}`);
      }
    }
  }
  return points;
}



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 ans = main(lines);
console.log( ans );