#include <bits/stdc++.h>
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()

vector<vector<ll>> note;

vector<string>  vec;

queue<pair<ll,ll>> Q;


vector<vector<bool>> rem;


void dfs(ll H,ll W) {
  
  if(vec[H+1][W]=='.'&&note[H+1][W]==0) {
note[H+1][W]=note[H][W];
  dfs(H+1,W);
 
  }
  if(vec[H-1][W]=='.'&&note[H-1][W]==0) {
note[H-1][W]=note[H][W];
  dfs(H-1,W);

  }
  
   if(vec[H][W+1]=='.'&&note[H][W+1]==0) {
  
note[H][W+1]=note[H][W];
  dfs(H,W+1);
  
  }
  
   if(vec[H][W-1]=='.'&&note[H][W-1]==0) {


note[H][W-1]=note[H][W];
  dfs(H,W-1);
  
  }
 
 
   return ;
      }
      
void worp(ll H,ll W) {
  rem[H][W]=true;
  
  if(vec[H+2][W+2]=='.'&&note[H+2][W+2]==0) {
note[H+2][W+2]=note[H][W]+1;
    Q.push(make_pair(H+2,W+2));
  dfs(H+2,W+2);
 }
  
  
   if(vec[H+1][W+1]=='.'&&note[H+1][W+1]==0) {
note[H+1][W+1]=note[H][W]+1;
     Q.push(make_pair(H+1,W+1));
     
  dfs(H+1,W+1);
}
 
  
  
  if(vec[H-1][W-1]=='.'&&note[H-1][W-1]==0) {
note[H-1][W-1]=note[H][W]+1;
    Q.push(make_pair(H-1,W-1));
  dfs(H-1,W-1);

 }
  
   if(vec[H-2][W-2]=='.'&&note[H-2][W-2]==0) {
note[H-2][W-2]=note[H][W]+1;
     Q.push(make_pair(H-2,W-2));
  dfs(H-2,W-2);

 }
  
  
  if(vec[H-1][W+1]=='.'&&note[H-1][W+1]==0) {
note[H-1][W+1]=note[H][W]+1;
    Q.push(make_pair(H-1,W+1));
  dfs(H-1,W+1);

 }
  
    if(vec[H-2][W+2]=='.'&&note[H-2][W+2]==0) {
note[H-2][W+2]=note[H][W]+1;
      Q.push(make_pair(H-2,W+2));
  dfs(H-2,W+2);
}
 
  
   if(vec[H+2][W-2]=='.'&&note[H+2][W-2]==0) {
note[H+2][W-2]=note[H][W]+1;
     Q.push(make_pair(H+2,W-2));
  dfs(H+2,W-2);
     

 }
  
  if(vec[H+1][W-1]=='.'&&note[H+1][W-1]==0) {
note[H+1][W-1]=note[H][W]+1;
    Q.push(make_pair(H+1,W-1));
  dfs(H+1,W-1);
}
 
  
  
 if(vec[H+2][W]=='.'&&note[H+2][W]==0) {
note[H+2][W]=note[H][W]+1;
   Q.push(make_pair(H+2,W));
  dfs(H+2,W);
}
 
  
   if(vec[H-2][W]=='.'&&note[H-2][W]==0) {
note[H-2][W]=note[H][W]+1;
     Q.push(make_pair(H-2,W));    
  dfs(H-2,W);
}
 
  if(vec[H-2][W+1]=='.'&&note[H-2][W+1]==0) {
note[H-2][W+1]=note[H][W]+1;
    Q.push(make_pair(H-2,W+1));
  dfs(H-2,W+1);
}
 
  
    if(vec[H-2][W-1]=='.'&&note[H-2][W-1]==0) {
note[H-2][W-1]=note[H][W]+1;
      Q.push(make_pair(H-2,W-1));
  dfs(H-2,W-1);

 }

if(vec[H+2][W+1]=='.'&&note[H+2][W+1]==0) {
note[H+2][W+1]=note[H][W]+1;
  Q.push(make_pair(H+2,W+1));
  dfs(H+2,W+1);
 }
  
  
  if(vec[H+2][W-1]=='.'&&note[H+2][W-1]==0) {
note[H+2][W-1]=note[H][W]+1;
    Q.push(make_pair(H+2,W-1));
  dfs(H+2,W-1);
 }


  if(vec[H][W+2]=='.'&&note[H][W+2]==0) {
note[H][W+2]=note[H][W]+1;
    Q.push(make_pair(H,W+2));
  dfs(H,W+2);

 }


 if(vec[H][W-2]=='.'&&note[H][W-2]==0) {
note[H][W-2]=note[H][W]+1;
   Q.push(make_pair(H,W-2));
  dfs(H,W-2);
}
 
   if(vec[H+1][W-2]=='.'&&note[H+1][W-2]==0) {
note[H+1][W-2]=note[H][W]+1;
     Q.push(make_pair(H+1,W-2));
  dfs(H+1,W-2);

 }
   
   if(vec[H-1][W-2]=='.'&&note[H-1][W-2]==0) {
note[H-1][W-2]=note[H][W]+1;
     Q.push(make_pair(H-1,W-2));
  dfs(H-1,W-2);

 }
  
  if(vec[H+1][W+2]=='.'&&note[H+1][W+2]==0) {

note[H+1][W+2]=note[H][W]+1;
    Q.push(make_pair(H+1,W+2));
  dfs(H+1,W+2);
}
 
  
   if(vec[H-1][W+2]=='.'&&note[H-1][W+2]==0) {
note[H-1][W+2]=note[H][W]+1;
     Q.push(make_pair(H-1,W+2));
  dfs(H-1,W+2);
 }
  
  if(vec[H][W+1]=='.'&&!rem[H][W+1]) {
worp(H,W+1);
  }
  
  if(vec[H][W-1]=='.'&&!rem[H][W-1]) {
worp(H,W-1);
  }
  
  if(vec[H+1][W]=='.'&&!rem[H+1][W]) {
worp(H+1,W);
  }
  
   if(vec[H-1][W]=='.'&&!rem[H-1][W]) {
worp(H-1,W);
  }
  
  return ;
}
  
  

  
  
  
int main() {
 ll H,W;
  cin>>H>>W;
  ll Ch,Cw;
  cin>>Ch>>Cw;
  Ch+=1;
  Cw+=1;
  ll Dh,Dw;
  cin>>Dh>>Dw;
  Dh+=1;
  Dw+=1;
  note=vector<vector<ll>>(H+4,vector<ll>(W+4));
  rem=vector<vector<bool>>(H+4,vector<bool>(W+4));
  
  vec=vector<string> (H+4);
  string S;
  for(ll i=0;i<W+4;i++) {
S+='#';
  }
  vec[0]=S;
  vec[1]=S;
  vec[H+2]=S;
  vec.back()=S;
  for(ll i=2;i<=H+1;i++) {
  string T;
    cin>>T;
    vec[i]="##"+T+"##";
  }
  note[Ch][Cw]=1;
  dfs(Ch,Cw);
  if(note[Dh][Dw]==1) {
cout<<0<<endl;
    return 0;
  }

  Q.push(make_pair(Ch,Cw));
  

  while(!Q.empty()) {
 
  pair<ll,ll> p=Q.front();
    Q.pop();
    worp(p.first,p.second);
  }
  
  if(note[Dh][Dw]==0) {
cout<<-1<<endl;
    return 0;
  }
  
  else {
cout<<note[Dh][Dw]-1<<endl;
  }
}
    
    
    


  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  


Measured memory_bytes: 13736