#include <algorithm>
#include<iostream>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#include<map>
#include<set>
#include<string>
#include <sstream>
#include<bitset>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>
#define MAX 1000+2
const int INF = 0x7fffffff;
using lll = long long;
using ull = unsigned long long;
using namespace std;
using Graph = vector<vector<int>>;

int mmap[MAX][MAX];
int iland[MAX][MAX];
bool seen[MAX][MAX];
map<int,int> land;
//1.島を探す
void searchN(int x,int y,int n){
  //cout << "x:" << x << "y:" << y << endl;
  seen[y][x] = true;
  iland[y][x] = n;
  //上
  if(mmap[y-1][x] == 0 && seen[y-1][x] == false){
    searchN(x,y-1,n);
  }
  
  //左
  if(mmap[y][x-1] == 0 && seen[y][x-1] == false){
    searchN(x-1,y,n);
  }
  
  //右
  if(mmap[y][x+1] == 0 && seen[y][x+1] == false){
    searchN(x+1,y,n);
  }
  
  //下
  if(mmap[y+1][x] == 0 && seen[y+1][x] == false){
    searchN(x,y+1,n);
  }
}

//2.繋っているグラフを作成
//3.繋っていない場合 -1
//4.繋っている場合最小値を探す


int main(){
  lll ii,jj,kk;
  vector<int> ret;
  int h,w,ch,cw,dh,dw;

  cin >>  h >>  w;
  
  cin >> ch >> cw;
  cin >> dh >> dw;


  string temp;
  memset(mmap,0,sizeof(mmap));
  memset(iland,0,sizeof(iland));
  memset(seen,false,sizeof(seen));
  
  for(ii=0;ii<=h+1;ii++){
    for(jj=0;jj<=w+1;jj++){
      mmap[ii][jj] = 1;
    }
  }
  
  for(ii=1;ii<=h;ii++){
    cin >> temp;
    for(jj=1;jj<=w;jj++){
      if(temp[jj-1] == '.'){
        mmap[ii][jj] = 0;
      }
      else{
        mmap[ii][jj] = 1;
      }
    }
  }

  //島の数を探す
  int n = 0;

  for(ii=1;ii<=h;ii++){
    for(jj=1;jj<=w;jj++){
      if(mmap[ii][jj] == 0 && seen[ii][jj] == false){
        //cout << "searchN" << endl;
        n++;
        searchN(jj,ii,n);

      }
    }
  }

  //cout << "n:" << n << endl;
  Graph g(n+1);
  set<int> s[n+1];

  //for(ii=0;ii<=h+1;ii++){
  //  for(jj=0;jj<=w+1;jj++){
  //    cout << iland[ii][jj];
  //  }
  //  cout << endl;
  //}
  
  int ll,mm;
  int pos,posi;

  //繋っている島を判定
  for(ii=1;ii<=h;ii++){
    for(jj=1;jj<=w;jj++){
      pos = iland[ii][jj];

      for(ll=ii-2;ll<=ii+2;ll++){
        for(mm=jj-2;mm<=jj+2;mm++){

          if(ll>=1 && ll<=h &&
             mm>= 1 && mm<=w){
            posi = iland[ll][mm];
            //cout << "ll:" << ll << "mm:" << mm << "posi:" << posi << endl;
            if(posi != pos){
              s[pos].insert(posi);
            }
          }
          
        }
      }
    }
  }

  //for(ii=1;ii<=n;ii++){
  //  auto it = s[ii].begin();
  //  cout << "ii:" << ii << ":";
  //  while(it != s[ii].end()){
  //    cout << *it << ",";
  //    it++;
  //  }
  //  cout << endl;
  //}
  
  for(ii=1;ii<=n;ii++){
    auto it = s[ii].begin();
    while(it != s[ii].end()){
      g[ii].push_back(*it);
      it++;
    }
  }
  int from,to;
  from = iland[ch][cw];
  to   = iland[dh][dw];

  bool seen2[n+1];
  memset(seen2,false,sizeof(seen2));
  vector<int> todo;
  todo.push_back(from);
  int dist[n+1] ;
  int tempDist;
  dist[from] = 0;
  for(ii=0;ii<=n;ii++){
    dist[ii] = -1;
  }
  dist[from] = 0;
  int v;
  
  
  while(todo.empty() != true){
    v = todo[0];
    todo.erase(todo.begin());
    tempDist = dist[v]+1;
    for(auto w : g[v]){
      if(seen2[w] == true){continue;}
      dist[w] = tempDist;
      seen2[w] = true;
      todo.push_back(w);
    }
  }

  //for(ii=1;ii<=n;ii++){
  //  cout << "dist:" << dist[ii] << endl;;
  //}
  
  cout << dist[to] << endl;
  
  return 0;
}


Measured memory_bytes: 61112