#include<bits/stdc++.h>
using namespace std;

#define fastio 			ios::sync_with_stdio(0);	cin.tie(0); cout.tie(0);  cout<<fixed;  cout<<setprecision(12);	
#define randomINT 		mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define	newl 			cout<<"\n"
#define DISP(as)		for(auto it : as) cout<<it<<" ";newl;
#define all(x)         	(x).begin(),(x).end()
#define mset(x,v)    	memset(x,v,sizeof(x))
#define newl           	cout<<"\n"
#define pb             	push_back
#define mp             	make_pair
#define f 				first
#define s 				second
#define dline          cerr<<"///REACHED///\n";
#define deb1(x)        cerr<<#x<<" = "<<x<<'\n';
#define deb2(x,y)      cerr<<'['<<#x<<','<<#y<<"] = "<<'['<<x<<','<<y<<']'<<'\n';
#define deb3(x,y,z)    cerr<<'['<<#x<<','<<#y<<','<<#z<<"] = "<<'['<<x<<','<<y<<','<<z<<']'<<'\n';

typedef long long 				ll;
typedef long double 			ld;
typedef vector<ll> 				vll;
typedef pair<ll ,ll> 			pll;
typedef pair<ld,ld> 			pld;
typedef unordered_map<ll,ll> 	um;
typedef vector<pll> 			vpll;

const ll 	MAX5 	= 	1e+5+7;
const ll 	MAX7 	= 	1e+7+7;
const ll 	MAXN 	= 	MAX7;
const ll   	INF   	=  	0x7f7f7f7f7f7f7f7f;
const int  	INFi 	=  	0x7f7f7f7f;
const ll 	MOD		=  	1e+9+7;
const ll 	N 		= 	2*1e6;

set<ll> adj[N];ll lvl[N]={};
//int dx8[]={0,1,1,1,0,-1,-1,-1}, dy8[]={1,1,0,-1,-1,-1,0,1};

// <------------- Declare Variables Here ------------> //

ll T=1;
ll n,x,y,m,p,q,k;
string s[2000],t;
//vll a;
ll val[2000][2000];
ll cou = 1;
// <------------- Implement Functions Here ----------> //

void dfs(ll x, ll y) {
	if(x<0 | x==n || y<0 || y==m || s[x][y]=='#' || val[x][y]!=INF) return;
	val[x][y] = cou;
	dfs(x+1,y);
	dfs(x,y+1);
	dfs(x-1,y);
	dfs(x,y-1);
}

void chkval(ll x,ll y)  {
	//ll k = INF;
	for(ll i=-2;i<=2;i++) {
		for(ll j=-2;j<=2;j++) {
			if(x+i<0 | x+i==n || y+j<0 || y+j==m || s[x+i][y+j]=='#' ) continue;
			if(val[x][y] == val[x+i][y+j] || val[x+i][y+j]==0) continue;
			
			adj[val[x][y]].insert(val[x+i][y+j]);
			adj[val[x+i][y+j]].insert(val[x][y]);
		}
	}
}

void bfs(ll s, ll d) {
	queue<ll> q;
	mset(lvl,-1);
	q.push(s);
	lvl[s] = 0;
	//cout<<s<<" "<<d;newl;
	while(!q.empty()) {
		ll x = q.front(); q.pop();
		for(auto it : adj[x]) {
			if(lvl[it] == -1) {
				lvl[it] = lvl[x]+1;
				q.push(it);
			}
		}
	}

	cout<<lvl[d];
	newl;
}

void view() {
	for(ll i=0;i<n;i++) {
		for(ll j=0;j<m;j++) {
			cout<<val[i][j]<<" ";
		}
		newl;
	}
	newl;

	for(ll i=1;i<=4;i++) {
		DISP(adj[i]);
		newl;
	}
}
 
// <------------- Start of main() -------------------> //

void MAIN() {
	cin >> n >> m;
	cin >> x >> y;
	cin >> p >> q;

	x--;y--;p--;q--;
	//mset(val,INF);

	for(ll i=0;i<=n;i++) for(ll j=0;j<=m;j++) val[i][j]=INF;

	for(ll i=0;i<n;i++) {
		cin>>s[i];
	}
	cou = 1;
	dfs(x,y);
	//view();

	for(ll i=0;i<n;i++) {
		for(ll j=0;j<m;j++) {
			if(s[i][j]=='.' && val[i][j]==INF ) {
				//k = chkval(i,j);
				cou ++;
				dfs(i,j);
				//view();
			}
		}
	}

	for(ll i=0;i<n;i++) {
		for(ll j=0;j<m;j++) {
			if(s[i][j]=='.') chkval(i,j);
		}
	}

	//view();
/*
	if(val[p][q] == INF) cout<<-1;
	else cout<<val[p][q];
	newl;*/

	bfs(val[x][y], val[p][q]);
}

int main() {
	fastio;randomINT;
	//cin>>T;
	while(T--) {
		MAIN();
	}
	return 0;
}



Measured memory_bytes: 144384