function Main(input) {
  input=input.split("\n");
  const H=input[0].split(' ')[0]-0;
  const W=input[0].split(' ')[1]-0;
  const CH=input[1].split(' ')[0]-0;
  const CW=input[1].split(' ')[1]-0;
  const DH=input[2].split(' ')[0]-0;
  const DW=input[2].split(' ')[1]-0;
  var map=[];
  map=new Array(H+1);
  map[0]=new Array(W+1);
  map[0].fill(false);
  for(var i=1;i<=H;i++){
    map[i]=[false].concat(input[i+2].split('').map(e=>{
      return e==='.'?Infinity:false;
    }));
  }
  //console.log(map);
  var answer=map.concat([]);
  var newRoad=[[CH,CW]];
  answer[CH][CW]=0;
  var next=[];
  var e;
  do{
    next=[];
    e=newRoad[0];
      [-1,0,1].forEach(a=>{
        [-1,0,1].forEach(b=>{
          if(0<e[0]+a&&e[0]+a<=H&&0<e[1]+b&&e[1]+b<=W&&answer[e[0]+a][e[1]+b]!==false&&Math.abs(a)+Math.abs(b)===1){
            if(answer[e[0]][e[1]]<answer[e[0]+a][e[1]+b]){
              next.push([e[0]+a,e[1]+b]);
              answer[e[0]+a][e[1]+b]=answer[e[0]][e[1]];
            }
          }
        });
      });
      [-2,-1,0,1,2].forEach(a=>{
        [-2,-1,0,1,2].forEach(b=>{
          if(0<e[0]+a&&e[0]+a<=H&&0<e[1]+b&&e[1]+b<=W&&answer[e[0]+a][e[1]+b]!==false){
            if(answer[e[0]][e[1]]+1<answer[e[0]+a][e[1]+b]){
              next.push([e[0]+a,e[1]+b]);
              answer[e[0]+a][e[1]+b]=answer[e[0]][e[1]]+1;
            }
          }
        });
      });
    newRoad.shift();
    
    newRoad=newRoad.concat(next.filter(f=>{
      return !newRoad.includes[f];
    }));
  }while(newRoad.length>0);
  console.log(answer[DH][DW]===Infinity?-1:answer[DH][DW]);
  
}


//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
  
  
