#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double lld;
ll MOD = (1e9)+7;
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int kx[] = {+1, +2, -1, -2, +1, +2, -1, -2};
const int ky[] = {+2, +1, +2, +1, -2, -1, -2, -1};
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
inline void fastIO(){
  ios_base::sync_with_stdio(0);cin.tie(0);
}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }

#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int MAXN=1001;
const int MAXCOMP=1010101;
int current_color=1;
vector<vector<int>>adj(MAXCOMP);
map<pair<int,int>,int>graph;
map<pair<int,int>,int>connections;
int h,w;
int ch,cw;
int dh,dw;
int wizard_cc;
int final_cc;
int coloring[MAXN][MAXN];
char grid[MAXN][MAXN];
bool vis[MAXN][MAXN];
bool isPossible=false;
bool is_valid(int y,int x){
  if(x<0||x>=w)return false;
  if(y<0||y>=h)return false;
  if(grid[y][x]=='#')return false;
  return true;
}
void dfs_build(int y,int x){
  graph[make_pair(y,x)]=current_color;
  coloring[y][x]=current_color;
  vis[y][x]=true;
  for(int i=0;i<4;i++){
    if(is_valid(y+dr[i],x+dc[i])){
      if(!vis[y+dr[i]][x+dc[i]]){
        dfs_build(y+dr[i],x+dc[i]);
      }
    }
  }
}
void connection(int y,int x){
  for(int incY=-2;incY<=2;incY++){
    for(int incX=-2;incX<=2;incX++){
      bool cond1=connections[make_pair(coloring[y][x],coloring[y+incY][x+incX])]==0;
      cond1=cond1&&connections[make_pair(coloring[y+incY][x+incX],coloring[y][x])]==0;
      bool cond2=coloring[y][x]!=coloring[y+incY][x+incX];
      if(is_valid(y+incY,x+incX)){
        if(cond1&&cond2){
          connections[make_pair(coloring[y][x],coloring[y+incY][x+incX])]++;
          adj[coloring[y][x]].push_back(coloring[y+incY][x+incX]);
          adj[coloring[y+incY][x+incX]].push_back(coloring[y][x]);
        }
      }
    }
  }
}
int main(){
  fastIO();
  cin>>h>>w>>ch>>cw>>dh>>dw;
  ch--,cw--,dh--,dw--;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      cin>>grid[i][j];
      vis[i][j]=false;
      coloring[i][j]=0;
    }
  }
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(!vis[i][j]&&grid[i][j]!='#'){
        dfs_build(i,j);
        current_color++;
      }
    }
  }
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(grid[i][j]!='#'){
        connection(i,j);
      }
    }
  }
  wizard_cc=graph[make_pair(ch,cw)];
  final_cc=graph[make_pair(dh,dw)];
  queue<pair<int,int>>q;
  vector<bool>bfs_vis(MAXCOMP,false);
  vector<int>dist(MAXCOMP,MAXCOMP);
  q.push(make_pair(wizard_cc,0));
  dist[wizard_cc]=0;
  while(!q.empty()){
    pair<int,int>next=q.front();
    int color=next.first,depth=next.second;
    bfs_vis[color]=true;
    if(final_cc==color){
      cout<<depth<<endl;
      return 0;
    }
    q.pop();
    for(int i=0;i<adj[color].size();i++){
      int node=adj[color][i];
      if(!bfs_vis[node]){
        if(dist[node]>dist[color]+1){
          dist[node]=dist[color]+1;
          q.push(make_pair(node,depth+1));
        }
      }
    }
  }
  cout<<-1<<endl;
  return 0;
}


Measured memory_bytes: 107072