#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i)>= min((a), (b))) && ((i) <= max((a), (b))))
typedef vector<ll> vi;
typedef pair<ll, ll> pii;
#define fi first
#define se second
#define upb upper_bound
#define lwb lower_bound
#define show(x) cout << #x << " is " << x << "\n";
const ll inf = 2e18;
const ll mod = 1e9 + 7;
const ld pi = 3.141592653589793238462643383279502884;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

void print(ll a[], ll n){for(ll i=0;i<n;i++){cout<<a[i]<<" ";}cout<<"\n";}
ll power(ll x, ll y){
    if(y<=0) return 1;
    ll ans = 1;
    x %= mod;
    while(y){
        if(y&1)
            ans = (x * ans) % mod;
        x = (x * x) % mod;
        y >>= 1;
    }
    return ans;
}
ll modInverse(ll n) {return power(n, mod-2);}
inline ll mul(ll a, ll b){ return (a * b) % mod; }
inline ll sub(ll a, ll b){ ll c = (a - b); if(c < 0) c += mod; return c; }
inline ll add(ll a, ll b){ ll c = (a + b); if(c >= mod) c -= mod; return c; }
inline ll divi(ll a, ll b){ return mul(a, modInverse(b)); }

//------------------------------------------------------------------------------------------

const ll N = 5000;
set<ll> adj[N*100];
//bool visited[N];
ll col[N][N];
ll h, w;
ll dis[N*100];

char s[N][N];

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void dfs(ll x, ll y, ll c){
	if(inrange(x, 1ll, w) && inrange(y, 1ll, h) && s[y-1][x-1]=='.' && col[y-1][x-1]==0){
		col[y-1][x-1] = c;
		for(int i=0;i<4;i++){
			int xi = x+dx[i];
			int yi = y+dy[i];
			
//			if(s[xi][yi]=='.'){
				dfs(xi, yi, c);
		}
	}
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout<<fixed<<setprecision(10);

	
	cin>>h>>w;
	ll x1, y1, x2, y2;
	cin>>y1>>x1;
	cin>>y2>>x2;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin>>s[i][j];
		}
	}
	
	// src - 1;
	// des - 2;
	dfs(x1, y1, 1);
	if(col[y2-1][x2-1]==1){
		cout<<0;
		return 0;
	}
	
//	ll ans=0;
	dfs(x2, y2, 2);
//	for(int i=0;i<h;i++){
//		for(int j=0;j<w;j++){
//			cout<<col[i][j]<<" ";
//		}
//		cout<<"\n";
//	}
//	cout<<"\n";
//	cout<<col[x2-1][y2-1];
	int c=3;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(s[i][j]=='.' && col[i][j]==0){
				dfs(j+1, i+1, c);
				c++;
			}
		}
	}
//	for(int i=0;i<h;i++){
//		for(int j=0;j<w;j++){
//			cout<<col[i][j]<<" ";
//		}
//		cout<<"\n";
//	}
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int color = col[i][j];
			if(color == 0) continue;
			
			for(int ii=-2;ii<=2;ii++){
				for(int jj=-2;jj<=2;jj++){
					int io = i+ii;
					int jo = j+jj;
					if(inrange(io, 0ll, h-1) && inrange(jo, 0ll, w-1) && s[io][jo]=='.' && color != col[io][jo]){
						adj[color].insert(col[io][jo]);
					}
				}
			}
		}
	}
	
	queue<ll> q;
    dis[1] = 0;
    q.push(1);
    while(!q.empty())
    {
        ll t = q.front();
        q.pop();
        if(t==2){
        	cout<<dis[2];
        	return 0;
		}
        for(auto u : adj[t]){
            if(!dis[u] && u!= 1 && u!=0)
            {
                dis[u] = dis[t] + 1;
                q.push(u);
            }
        }
    }
    cout<<-1;
    return 0;
}


Measured memory_bytes: 63100