#pragma GCC optimize("Ofast")
  #pragma GCC target("avx,avx2,fma")
  #pragma GCC optimization ("unroll-loops")
  #include <bits/stdc++.h>
  using namespace std;
  //---------------------------------//
  void __print(int x) {cerr << x;}
  void __print(long x) {cerr << x;}
  void __print(long long x) {cerr << x;}
  void __print(unsigned x) {cerr << x;}
  void __print(unsigned long x) {cerr << x;}
  void __print(unsigned long long x) {cerr << x;}
  void __print(float x) {cerr << x;}
  void __print(double x) {cerr << x;}
  void __print(long double x) {cerr << x;}
  void __print(char x) {cerr << '\'' << x << '\'';}
  void __print(const char *x) {cerr << '\"' << x << '\"';}
  void __print(const string &x) {cerr << '\"' << x << '\"';}
  void __print(bool x) {cerr << (x ? "true" : "false");}

  template<typename T, typename V>
  void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  template<typename T>
  void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  void _print() {cerr << "]\n";}
  template <typename T, typename... V>
  void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  #ifndef ONLINE_JUDGE
  #define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
  #else
  #define deb(x...)
  #endif
 //------------------------------//

  typedef long long int ll;
  typedef pair<int,int> pi;
  typedef pair<ll,ll> pll;
  typedef vector<int> vi;
  typedef vector<ll> vll;
  typedef vector<pi> vpi;
  typedef vector<pll> vpll;

  #define  ff           first
  #define  ss           second
  #define  pb           push_back
  #define  IOS          ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  #define  all(x)       (x).begin(), (x).end()
  #define  ps(x,y)      fixed<<setprecision(y)<<x
  #define  rep(i,a,b)   for(ll i=a ; i<b ; ++i)

  const ll MOD = 1e9 + 7;
  const ll INF = 1e18;
  const ll maxn = 1e6 + 4;
  	
	
  void solve(){
 	  int h,w;
 	  cin >> h >> w;
 	  int sh,sw,eh,ew;
 	  cin >> sh >> sw >> eh >> ew;
 	  sh--,sw--,eh--,ew--;
 	  vector<string>grid(h);
 	  for(int i=0;i<h;i++){
	   cin >> grid[i];
 	  }
 	  vector<vi>dis(h,vi (w,INT_MAX));
 	  vector<vi>vis(h,vi (w,0));
 	  deque<pair<int,int>> q;
 	  q.push_front({sh,sw});
 	  dis[sh][sw]=0;
 	  while(!q.empty()){
 	  	 pi k = q.front();
 	  	 q.pop_front();
 	  	 if(vis[k.ff][k.ss])continue;
 	  	 vis[k.ff][k.ss]=1;
 	  	 for(int i=max(0,k.ff-2);i<min(k.ff+3,h);i++){
 	  	 	 for(int j=max(0,k.ss-2);j<min(k.ss+3,w);j++){
 	  	 	 	 if(grid[i][j]=='#')continue;
 	  	 	 	 if((abs(i-k.ff)==1 && abs(j-k.ss)==0)||(abs(i-k.ff)==0 && abs(j-k.ss)==1)){
 	  	 	 	 	 dis[i][j] = min(dis[i][j],dis[k.ff][k.ss]);
 	  	 	 	 	 q.push_front({i,j});
 	  	 	 	 }
 	  	 	 	 else{
 	  	 	 	 	 dis[i][j] = min(dis[i][j],dis[k.ff][k.ss]+1);
 	  	 	 	 	 q.push_back({i,j});
 	  	 	 	 }
 	  	 	 }
 	  	 }
 	  }
 	  cout << (dis[eh][ew]==INT_MAX?-1:dis[eh][ew]);
  }

  int main(){
    IOS
    	solve();
    //cerr <<endl <<"[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
  }

// integer overflow
// remember to clear visited array
// take input as vector<string> while using grid
// don't assign after modifying	

Measured memory_bytes: 13468