"use strict";
var input=require("fs").readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cid=0;
function next(){return +cin[cid++];}
function nextstr(){return cin[cid++];}
function nextbig(){return BigInt(cin[cid++]);}
function nexts(n,a){return a?cin.slice(cid,cid+=n):cin.slice(cid,cid+=n).map(a=>+a);}
function nextm(h,w,a){var r=[],i=0;if(a)for(;i<h;i++)r.push(cin.slice(cid,cid+=w));else for(;i<h;i++)r.push(cin.slice(cid,cid+=w).map(a=>+a));return r;}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill().map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill().map(x=>"+r+")";return eval(r);}

var myOut = main();
if(myOut !== undefined)console.log(String(myOut));

function main(){
  var dy = [-1,0,1,0], dx = [0,1,0,-1];
  var h = next(), w = next();
  var [sy,sx] = nexts(2);
  var [gy,gx] = nexts(2);
  sy--,sx--,gy--,gx--;
  var s = nexts(h,1);
  var map = xArray(1e9,h,w);
  map[sy][sx] = 0;
  var q1 = [sy,sx], q2 = [], d = 0;
  while(q1.length){
    while(q1.length){
      var qx = q1.pop(), qy = q1.pop();
      if(map[qy][qx] < d)continue;
      for(var i = 0; i < 4; i++){
        var nx = qx+dx[i], ny = qy+dy[i];
        if(nx < 0 || nx === w || ny < 0 || ny === h)continue;
        if(s[ny][nx] === "#")continue;
        if(map[ny][nx] <= d)continue;
        map[ny][nx] = d;
        q1.push(ny,nx);
      }
      for(var y = -2; y <= 2; y++)for(var x = -2; x <= 2; x++){
        var nx = qx+x, ny = qy+y;
        if(nx < 0 || nx >= w || ny < 0 || ny >= h)continue;
        if(s[ny][nx] === "#")continue;
        if(map[ny][nx] <= d+1)continue;
        map[ny][nx] = d+1;
        q2.push(ny,nx);
      }
    }
    d++, q1 = q2, q2 = [];
  }
  return map[gy][gx] === 1e9 ? -1 : map[gy][gx];
}