#include <bits/stdc++.h>
using namespace std;

int d[1000][1000],dy[4]={0,0,-1,1},dx[4]={1,-1,0,0};
struct R2{int y,x;};
#define REP(i,s,n) for(int i=s;i<n;i++)
const int INF=1e9;

signed main(){
  int h,w;cin>>h>>w;
  int sy,sx,gy,gx;cin>>sy>>sx>>gy>>gx;sy--;sx--;gy--;gx--;
  vector<string> s(h);
  REP(i,0,h)cin>>s[i];
  REP(i,0,h)REP(j,0,w)d[i][j]=INF;
  deque<R2> que;
  que.push_front({sy,sx});
  d[sy][sx]=0;
  while(que.size()){
    R2 p=que.front();que.pop_front();
    REP(i,0,4){
      int Y=p.y+dy[i],X=p.x+dx[i];
      if(Y<0||Y>=h||X<0||X>=w)continue;
      if(d[Y][X]<=d[p.y][p.x]||s[Y][X]=='#')continue;
      d[Y][X]=d[p.y][p.x];
      que.push_front({Y,X});
    }
    REP(Y,max(p.y-2,0),min(h,p.y+3)){
      REP(X,max(p.x-2,0),min(w,p.x+3)){
        if(d[Y][X]<=d[p.y][p.x]+1||s[Y][X]=='#')continue;
        d[Y][X]=d[p.y][p.x]+1;
        que.push_back({Y,X});
      }
    }
  }
  if(d[gy][gx]==INF)cout<<-1<<endl;
  else cout<<d[gy][gx]<<endl;
}
