//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007; // 998244353;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const ld PI = acos((ld)-1);
const vector<ll> dx={-1,1,0,0},dy={0,0,-1,1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}

template<typename T,typename Y> inline T mpow(T a, Y n) {
  T res = 1;
  for(;n;n>>=1) {
    if (n & 1) res = res * a;
    a = a * a;
  }
  return res;
}

template<typename T,typename Y>
ostream& operator<<(ostream& os,const pair<T,Y>& p){
  return os<<"{"<<p.fs<<","<<p.sc<<"}";
}
template<typename T> ostream& operator<<(ostream& os,const V<T>& v){
  os<<"{";
  for(auto e:v)os<<e<<",";
  return os<<"}";
}
template<typename ...Args>
void debug(Args&... args){
  for(auto const& x:{args...}){
    cerr<<x<<' ';
  }
  cerr<<ENDL;
}

template <typename T,typename IND>
vector<T> dijkstra(int s,vector<vector<pair<IND, T> > > & G){     
  using Pa = pair<T, IND>;
  vector<T> D(G.size(),INF);//----------------
  priority_queue<Pa,vector<Pa>,greater<Pa> > q;
  D[s]=0;
  q.emplace(D[s],s);//最短距離、ゴール頂点番号

  while(!q.empty()){
    T spath=q.top().first;
    int v=q.top().second;
    q.pop();

    if(D[v]<spath) continue;
    for(auto& e:G[v]){
      int u=e.first;//頂点番号
      T c=e.second;//最短距離
      if(D[u]>D[v]+c){
        D[u]=D[v]+c;
        q.emplace(D[u],u);
      }
    }
  }
  return D;
}

ll h,w;

ll toid(int g,int r){
  ll ret=(w)*g+r;
  assert(ret<h*w);
  return ret;
}

bool check(int g,int r){
  if(g<0)return false;
  if(g>=h)return false;
  if(r<0)return false;
  if(r>=w)return false;
  return true;
}

signed main(){
  cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(20);
  cin>>h>>w;
  ll sg,sr,gg,gr;cin>>sg>>sr>>gg>>gr;
  sg--;sr--;gg--;gr--;
  V<string> v(h);
  rep(i,0,h)cin>>v[i];

  VV<Pi> e(h*w,V<Pi>(0));

  rep(i,0,h)rep(j,0,w){
    if(v[i][j]=='#')continue;
    rep(k,0,4){
      if(!check(i+dx[k],j+dy[k]))continue;
      if(v[i+dx[k]][j+dy[k]]=='#')continue;

      e[toid(i,j)].eb(toid(i+dx[k],j+dy[k]),0);
    }
  }


  rep(i,0,h)rep(j,0,w){
    rep(k,-2,3)rep(l,-2,3){
      if(!check(i+k,j+l))continue;
      if(v[i+k][j+l]=='#')continue;

      e[toid(i,j)].eb(toid(i+k,j+l),1);
    }
  }

  V<int> ans=dijkstra(toid(sg,sr),e);

  ll res=ans[toid(gg,gr)];
  if(res>=INF)cout<<-1<<ENDL;
  else cout<<res<<ENDL;
  
}
//! ( . _ . ) ! 
//CHECK overflow,vector_size,what to output?

Measured memory_bytes: 190864