#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <iomanip>
#include <fstream>
#include <stack>

using namespace std;
 
#define ll long long
typedef unsigned long long ull;
typedef long double ld;

#define pii         pair<ll ,ll >
#define vi          vector<ll >
#define vii         vector<pii>
#define mi          map<ll ,ll >
#define mii         map<pii,int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define pb          push_back
#define hell        1000000007 
#define rep(i,a,b)  for(ll int i=a;i<b;i++)
#define take        rep(i,0,n) cin>>v[i];
#define MAXN        200005


#define allr(c) (c).rbegin(),(c).rend()
int mod = 998244353;
const int inf = 1034567891;
const long long LL_INF = 1234567890123456789ll;
#define PI 3.14159265
#define endl '\n'
#define F first
#define S second
#define debug(x) cout << #x << " = " << x << endl;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
 
#define out(container) for (auto it : container) cout << it << " "; cout << endl;
 
 
template < typename T > T GCD(T a, T b)            { ll t; while(a) { t = a; a = b % a; b = t; } return b; }
template < typename T > string toString(T a)       { return to_string(a); }
template < typename T > void toInt(string s, T &x) { stringstream str(s); str >> x;}
inline int add(long long x, long long y){ x += y; if(x >= mod) x -= mod; return x;}
inline int sub(ll x, ll y){ x -= y; if(x < 0) x += mod; return x;}
inline int mul(ll x, ll y){ return (x * 1ll * y) % mod;}
inline ll powr(ll a, ll b){
  int x = 1 % mod;
  while(b){
    if(b & 1) x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}
inline long long inv(long long a){ return powr(a, mod - 2);}
ll Powb(ll b,ll n,ll m)
{
    if(n==0) return 1LL;
    if(n==1) return b;
    ll temp = Powb(b,n/2,m);
    if(n%2==0){return (temp*temp)%m;}
    else{return (b*((temp*temp)%m))%m;}
}
ll n,m;
ll is(ll a,ll b)
{
	if(a>=0&&a<n&&b>=0&&b<m)
	{
		return 1;
	}
	return 0;
}
	

void solve()
{

	cin>>n>>m;
	vector<vector<char> >v(n,vector<char>(m));
	ll ir,ic;
	cin>>ir>>ic;
	ir--;
	ic--;
	ll fr,fc;
	
	cin>>fr>>fc;
	fr--;
	fc--;
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<m;j++)
		{
			cin>>v[i][j];
		}
	}
//	cout<<n<<m<<fr<<fc<<ir<<ic<<endl;
	vector<vector<ll> >vis(n,vector<ll>(m,hell));
	priority_queue<pair<ll,pair<ll,ll> >,vector<pair<ll,pair<ll,ll> > >,greater<pair<ll,pair<ll,ll> > > >pq;
	pq.push({0,{ir,ic}});
	vis[ir][ic]==0;
	while(pq.size()>0)
	{
		ll mg=pq.top().F;
		ll x=pq.top().S.F;
		ll y=pq.top().S.S;
	//	cout<<mg<<" "<<x<<" "<<y<<endl;
		
	
		vis[x][y]=min(mg,vis[x][y]);
		ll arr[]={0,1,0,-1};
		ll brr[]={1,0,-1,0};
		pq.pop();
		for(ll i=0;i<4;i++)
		{
			ll xx=x+arr[i];
			ll yy=y+brr[i];
			if(is(xx,yy)&&vis[xx][yy]>mg&&v[xx][yy]=='.')
			{
				vis[xx][yy]=min(mg,vis[xx][yy]);
				pq.push({mg,{xx,yy}});
			}
		}
		for(ll i=-2;i<=2;i++)
		{
			for(ll j=-2;j<=2;j++)
			{
				ll xx=x+i;
				ll yy=y+j;
				if(is(xx,yy)&&vis[xx][yy]>(mg+1)&&v[xx][yy]=='.')
				{
					vis[xx][yy]=min(mg+1,vis[xx][yy]);
					pq.push({mg+1,{xx,yy}});
				}
				
			}
		}
		
	}
	if(vis[fr][fc]==hell)
	{
		cout<<-1<<endl;
		return;
	}
	else
	{
		cout<<vis[fr][fc]<<endl;
	}
	
}
int main()
{
	ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);                                                        
      
    ll t;
        
    t=1;
   
    
  // cin>>t;
    while(t--)
    solve();
}

Measured memory_bytes: 12632