console.log((args=>{
  const [HW,CC,DD,...S] = args.trim().split`\n`;
  const [H,W] = HW.split` `.map(v=>v|0);
  const C = CC.split` `.map(v=>v-1);
  const D = DD.split` `.map(v=>v-1);
  const map = Array.from({length:H},_=>Array(W).fill(0));
  const done= Array.from({length:H},_=>Array(W).fill(false));
  let stack = [C];
  let _stack =  [], step = 1;
  while (stack.length) {
    const [px,py] = stack.pop();
    if ( px === D[0] && py === D[1] ) return step-1;
    if ( !done[px][py] ) {
      done[px][py] = true;
      map[px][py] = step;
      for ( let x = px-2; x <= px+2; x++ ) {
        if ( x < 0 || H <= x ) continue;
        for ( let y = py-2; y <= py+2; y++ ) {
          if ( y < 0 || W <= y || S[x][y] === '#' || done[x][y] ) continue;
          if (Math.hypot(x-px,y-py)<=1) {
            map[x][y] = step;
            stack.push([x,y]);
          } else if ( !map[x][y] ) {
            map[x][y] = step+1;
            _stack.push([x,y]);
          }
        }
      }
    }
    if ( stack.length === 0 ) {
      [stack,_stack] = [_stack,[]];
      step++;
    }
  }
  return -1;
})(require('fs').readFileSync('/dev/stdin','utf8')));