#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<vvll> vvvll;
typedef vector<ld> vld;
typedef vector<string> vstr;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef priority_queue<ll, vector<ll>, greater<ll>> spqll;  // 小さい順に取り出し
typedef priority_queue<ll, vector<ll>, less<ll>> bpqll;     // 大きい順に取り出し

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define IREP(i, v) for (auto i = (v).begin(); i != (v).end(); ++i)
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"

ll MOD = 1000000007;
ll INF = 1e18;
ld EPS = 1e-9;
ld PI = M_PI;
vll dx = {1, 0, -1, 0, 1, -1, -1, 1};
vll dy = {0, 1, 0, -1, 1, 1, -1, -1};
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;} //最大公約数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b;}     //最小公倍数

void yes(){ cout << "Yes" << endl;}
void no(){ cout << "No" << endl;}

//-----------------------------------------

//-----------------------------------------

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  ll h, w;
  cin >> h >> w;
  ll sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--; sy--; gx--; gy--;

  vstr str(h);
  REP(i,h) cin >> str[i];

  vvll seen(h,vll(w,0));
  vvll color(h,vll(w,-1));

  ll col = -1;
  queue<pll> que;

  for(ll i = 0; i < h; i++){
    for(ll j = 0; j < w; j++){
      if(seen[i][j] == 0 && str[i][j] == '.'){
        col++;
        que.push(make_pair(i,j));
        seen[i][j] = 1;
        color[i][j] = col;

        while(!que.empty()){
          ll x, y;
          x = que.front().first;
          y = que.front().second;
          que.pop();
          for(int k=0;k<4;k++){
            ll nx = x +dx[k] ;
            ll ny = y +dy[k] ;
            if(nx>=0 && nx<h && ny>=0 && ny<w && seen[nx][ny]==0){
              if(str[nx][ny]=='.'){
                seen[nx][ny] = 1 ;
                que.push(make_pair(nx,ny));
                color[nx][ny] = col;
              }
            }
          }
        }
      }
    }
  }
  
  
  /*REP(i,h){
    REP(j,w){
      if(j) cout << " ";
      cout << color[i][j];
    }
    cout << endl;
  }*/
  

  vvll graph(col+1);
  vll dis(col+1,INF);
  vll see(col+1,0);

  ll s = color[sx][sy];
  ll g = color[gx][gy];

  //cout << s << " " << g <<endl;
  

  REP(i,h){
    REP(j,w){
      if(str[i][j] == '#') continue;
      ll r = color[i][j];
      for(ll I = -2; I <= 2; I++){
        for(ll J = -2; J <= 2; J++){
          ll NI = i + I;
          ll NJ = j + J;
          if(0 <= NI && NI < h && 0 <= NJ && NJ < w && str[NI][NJ] == '.'){
            ll R = color[NI][NJ];
            if(r != R){
              graph[r].push_back(R);
              graph[R].push_back(r);
            }
          }
        }
      }
    }
  }
  

  queue<ll> que2;
  see[s] = 1;
  dis[s] = 0;
  que2.push(s);
  

  while(!que2.empty()){
    int k = que2.front() ;
    que2.pop() ;
    for(int i=0;i<graph[k].size();i++){
      ll u = graph[k][i];
      if(see[u]==0){
        que2.push(u);
        see[u] = 1;
        dis[u] = dis[k] + 1;
      }
    }
  }

  if(dis[g] == INF) cout << -1 << endl;
  else cout << dis[g] <<endl;
  





  return 0;
}



Measured memory_bytes: 65436