#include "bits/stdc++.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define int ll
typedef pair<int,int> pii;
#define fi first
#define se second
#define Sort(a) sort(a.begin(),a.end())
#define rep(i,n) for(int i = 0;i < (n) ; i++) 
#define REP(i,n) for(int i = 0;i < (n) ; i++) 
#define MP(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define INF LLONG_MAX/4
#define INTINF INT_MAX/2
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr<<#x<<": "<<x<<endl
#define debug_vec(v) cerr<<#v<<":";rep(i,v.size())cerr<<" "<<v[i];cerr<<endl
template<class T> inline bool chmax(T &a, T b){if(a < b){a = b;return true;}return false;}
template<class T> inline bool chmin(T &a, T b){if(a > b){a = b;return true;}return false;}
/*----------------------------------------------------------------*/
// const int MOD = 998244353;
// const int MOD = 1000000007;
int h,w;
int ch,cw,gh,gw;
vector<string> s;

const int N = 1000011;
int par[N];
int rnk[N];

void init(int n){
    for(int i=0;i<n;i++){
        par[i] = i;
        rnk[i] = 0;
    }
}

int find(int x){
    if(par[x] == x) return x;
    else return par[x] = find(par[x]);
}

void unite(int x,int y){
    x = find(x);
    y = find(y);
    if(x == y) return;

    if(rnk[x] < rnk[y]) par[x] = y;
    else{
        par[y] = x;
        if(rnk[x] == rnk[y]) rnk[x]++;
    }
}

bool same(int x,int y){
    return find(x) == find(y);
}

int dh[4] = {0,0,-1,1};
int dw[4] = {1,-1,0,0};

bool isin(int i,int j){
	return (0 <= i && i < h && 0 <= j && j < w);
}

#define MAX_V 1000010
int V;
set<int> G[MAX_V];
int d[MAX_V];
void dijkstra(int s){
    priority_queue<pii,vector<pii>,greater<pii>> que;
    fill(d,d+V,INF);
    d[s] = 0;
    que.push(pii(0,s));
    while(!que.empty()){
        pii p = que.top();que.pop();
        int v = p.se;
        if(d[v] < p.fi) continue;
        for(auto e:G[v]){
            if(d[e] > d[v]+1){
                d[e] = d[v] + 1;
                que.push(pii(d[e],e));
            }
        }
    }
}

/*----------------------------------------------------------------*/
signed main(){

	cin >> h >> w >> ch >> cw >> gh >> gw;
	ch--;
	cw--;
	gh--;
	gw--;
	rep(i,h){
		string x;cin >> x;
		s.pb(x);
	}
	init(h*w);

	rep(i,h){
		rep(j,w){
			rep(k,4){
				int nh = i+dh[k];
				int nw = j+dw[k];
				if(isin(nh,nw) && s[i][j] == '.' && s[nh][nw] == '.'){
					unite(i*w+j,nh*w+nw);
				}
			}
		}
	}

	V = h*w;
	rep(i,h){
		rep(j,w){
			if(s[i][j] == '#')continue;
			rep(k1,5)rep(k2,5){
				int nh = i + k1 -2;
				int nw = j + k2 -2;
				if(isin(nh,nw) && s[nh][nw] == '.' && !same(i*w+j,nh*w+nw)){
					G[find(i*w+j)].insert(find(nh*w+nw));
					G[find(nh*w+nw)].insert(find(i*w+j));
				}
			}
		}
	}

	dijkstra(find(ch*w+cw));
	int ans = d[find(gh*w+gw)];
	if(ans > 10000000)cout << -1 << endl;
	else cout << ans << endl;

	// rep(i,h){
	// 	rep(j,w){
	// 		if(d[i*w+j] > 10000000)cerr << 'x';
	// 		else cerr << d[i*w+j];
	// 	}
	// 	cerr << endl;
	// }


	return 0;
}
/*----------------------------------------------------------------*/

// g++ -std=gnu++17 code1.cpp
// sudo pip3 install --upgrade online-judge-tools
// rm -r -f test;oj dl https://abc174.contest.atcoder.jp/tasks/abc174_b
// rm -r -f test;oj d https://atcoder.jp/contests/abc176/tasks/abc176_a

Measured memory_bytes: 93432