#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const ll INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;

const ll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const ll dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

inline bool inside(ll y, ll x, ll H, ll W) {
    return (y >= 0 && x >= 0 && y < H && x < W);
}

char G[1005][1005];
int cost[1005][1005];

int visited[1005][1005];

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  
  int h,w; cin>>h>>w;
  int sy,sx,gy,gx; cin>>sy>>sx>>gy>>gx;
  sy--,sx--,gy--,gx--;
  rep(i,h) rep(j,w) cin>>G[i][j];
  
  int id=1;
  
  rep(i,h){
    rep(j,w){
      if(G[i][j]!='#' && visited[i][j]==0){
        queue<pll> q;
        q.emplace(i,j);
        visited[i][j]=id;
        id++;
        while(!q.empty()){
          auto p= q.front(); q.pop();
          ll y=p.first, x=p.second;
          rep(k,4){
            ll ni=y+dy[k], nj=x+dx[k];
            if(!inside(ni,nj,h,w)) continue;
            if(G[ni][nj]!='#' && visited[ni][nj]==0){
              visited[ni][nj]=id;
              q.emplace(ni,nj);
            }
          }
        }
      }
    }
  }
  
  set<int> ids;
  rep(i,h) rep(j,w) cost[i][j]=INF;
  priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> q;
  q.emplace(0,pll(sy,sx));
  cost[sy][sx]=0;
  ids.insert(visited[sy][sx]);
  while(!q.empty()){
    auto p=q.top(); q.pop();
    ll c=p.first, y=p.second.first, x=p.second.second;
    if(c>cost[y][x]) continue;
    rep(i,4){
      ll ny=y+dy[i], nx=x+dx[i];
      if(!inside(ny,nx,h,w)) continue;
      if(G[ny][nx]!='#' && cost[ny][nx]>cost[y][x]){
        cost[ny][nx]=cost[y][x];
        q.emplace(cost[ny][nx], pll(ny,nx));
      }
    }
    for(ll i=-2; i<=2; i++){
      for(ll j=-2; j<=2; j++){
        ll ny=y+i, nx=x+j;
        if(!inside(ny,nx,h,w)) continue;
        if(G[ny][nx]!='#' && ids.count(G[ny][nx])==0){
          if(cost[ny][nx]>cost[y][x]+1){
            cost[ny][nx]=cost[y][x]+1;
            q.emplace(cost[ny][nx], pll(ny,nx));
          }
        }
      }
    }
  }
  
  ll ans=cost[gy][gx];
  if(ans==INF) ans=-1;
  cout<<ans<<endl;
  
}


Measured memory_bytes: 12576