function Main(input) {
  const lines = input.split('\n');
  const HW = lines[0].split(' ').map(i => parseInt(i) - 1);
  const S = lines[1].split(' ').map(i => parseInt(i) - 1);
  const G = lines[2].split(' ').map(i => parseInt(i) - 1);
  const M = lines.splice(3, HW[0] + 1).map(r => r.split(''));

  const queue = [];
  const search = (pos, nWarp) => {
    const r = pos[0];
    const c = pos[1];
    if (M[r][c] !== '.') return;
    const currentSocre = M[r][c];
    if (currentSocre === '#') {
      return;
    } else {
      M[r][c] = nWarp;
      for (let h = -1; h <= 1; h++) {
        for (let w = -1; w <= 1; w++) {
          if ((h + w) % 2 === 0) continue;
          const nr = r + h;
          const nc = c + w;
          if (nr < 0 || HW[0] < nr || nc < 0 || HW[1] < nc) continue;
          queue.unshift([[nr, nc], nWarp]);
        }
      }
      for (let h = -2; h < 3; h++) {
        for (let w = -2; w < 3; w++) {
          const nr = r + h;
          const nc = c + w;
          if (nr < 0 || HW[0] < nr || nc < 0 || HW[1] < nc) continue;
          queue.push([[nr, nc], nWarp + 1]);
        }
      }
    }
  }
  queue.push([[S[0], S[1]], 0]);
  while (queue.length !== 0) {
    const item = queue.shift();
    search(item[0], item[1]);
    if (M[G[0]][G[1]] !== '.') break;
  }
  // console.log(M);
  console.log(M[G[0]][G[1]] !== '.'? M[G[0]][G[1]]: -1);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));
