//FIRST THINK THEN CODE.

  #include <bits/stdc++.h>


  using namespace std;

  typedef long long ll;

  #define rep(i,a,b) for(ll i=a;i<b;++i)
  #define rrep(i,a,b) for(ll i=a;i>b;--i)
  #define FOR(i,n)  for(ll i=0;i<n;i++)
  #define vi vector<int>
  #define vl vector<ll>
  #define ld long double
  #define vld vector<ld>
  #define vvi vector<vector<int>>
  #define vvl vector<vector<long long>>
  #define vvld vector<vector<ld>>
  #define pii pair<int,int>
  #define pll pair<long,long>
  #define vpii vector<pii>
  #define vpll vector<pll>
  #define ff first
  #define ss second
  #define pb push_back
  #define pf push_front
  #define mp make_pair
  #define lb lower_bound
  #define ub upper_bound
  #define bs binary_search
  #define d1(x) cout<<(x)<<endl
  #define d2(x,y) cout<<(x)<<" "<<(y)<<endl
  #define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
  #define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
  #define PI 3.1415926535897932384626433832795
  #define fix(f,n) fixed<<setprecision(n)<<f
  #define all(x) x.begin(),x.end()
  #define rev(p) reverse(p.begin(),p.end());
  #define endl "\n"
  #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  #define popcount(x) __builtin_popcountll(x)
  #define sz(x) ((ll)x.size())
  const ll M=1000000007;
  const ll MM=998244353;
  ll begtime = clock();
  #define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
  //#define _Find_next 
  //#define _Find_first 



  ll newmod(ll a,ll b)
  {
  return ((a%b)+b)%b;
  }


  ll powM(ll a,ll b,ll m )
  { 
  if(a<=0)return 0;
  a%=m;
  if(!b)return 1;
  ll ans=1LL;
  while(b)
  {
  if(b&1)ans=ans*a%m;
  a=a*a%m;
  b>>=1;
  }

  return ans;
  }



  ll poww(ll a,ll b)
  { 

  ll ans=1;
  while(b)
  {
  if(b&1)ans=ans*a;
  a=a*a;
  b>>=1;
  }

  return ans;

  }

  template<typename T,typename F>
  void chmax( T &a,F b){
  if(b>a)a=b;
  }

  template<typename T,typename F>
  void chmin( T &a,F b){
  if(b<a)a=b;
  }



  const ll N=1e3+5;

char ch[N][N];

ll vis[N][N];

ll h,w;

ll region;

bool inside(ll x,ll y){
  if(x>=0&&x<h&&y>=0&&y<w)return true;
  return false;
}

bool valid(ll x,ll y){
  if(inside(x,y)&&!vis[x][y]&&ch[x][y]!='#')return true;

  return false;
}

vl dirx={-1,1,0,0};
vl diry={0,0,1,-1};

void dfs(ll x,ll y){
  vis[x][y]=region;
        FOR(i,4){
          ll nx=x+dirx[i];
          ll ny=y+diry[i];
          if(valid(nx,ny)){
            dfs(nx,ny);
          }
        }
}


  int main()
  { 

  IOS;


cin>>h>>w;

ll CH,CW;
cin>>CH>>CW;

CH--;
CW--;


ll dh,dw;
cin>>dh>>dw;

dh--;
dw--;

FOR(i,h)FOR(j,w)cin>>ch[i][j];



FOR(i,h){
  FOR(j,w){
    if(ch[i][j]!='#'&&!vis[i][j]){
      region++;
      dfs(i,j);
    }
  }
}



vector<set<ll>>arr(region+1);

vl dx={-2,-1,0,1,2};
vl dy={-2,-1,0,1,2};

FOR(i,h){
  FOR(j,w){  
    if(ch[i][j]!='#'){
      ll self=vis[i][j];
    for(auto p:dx){
      for(auto q:dy){
        ll ni=i+p,nj=j+q;
        if(i!=ni||j!=nj){
         if(inside(ni,nj)){
          
          ll idx=vis[ni][nj];
          //d3(ni,nj,idx);
          if(idx&&self!=idx)arr[self].insert(idx);
         }
        }
      }
    }
  }
  }
}

ll st=vis[CH][CW];
ll en=vis[dh][dw];
/*
if(st==en){
  d1(0);
  return 0;
}*/

//d2(st,en);
/*
for(int i=1;i<=region;i++){
  for(auto x:arr[i])cout<<x<<" ";
    cout<<endl;
}*/

/*
FOR(i,h){
  FOR(j,w){
    cout<<vis[i][j];
  }
  cout<<endl;
}*/



// now find shortest distance 
// between st and en.

queue<ll>q;

vl seen(region+1);

vl level(region+1,1e9);

q.push(st);

ll ans=1e9;

level[st]=0;

//ll turn=0;

while(!q.empty()){
  ll f=q.front();
q.pop(); 
 seen[f]=1;
//turn++;



for(auto child:arr[f]){
  if(!seen[child]){
    seen[child]=1;
    q.push(child);
    chmin(level[child],level[f]+1);
  }
}

}


if(level[en]==1e9){
  d1(-1);
}
else d1(level[en]);



  return 0;

  }

Measured memory_bytes: 38416