#pragma GCC optimize("Ofast")   //Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>

using namespace std;

#define  fastio         ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(10);
#define  randomINIT     mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define  all(x)         (x).begin(),(x).end()
#define  mset(x,val)    memset(x,val,sizeof(x))
#define  endl           "\n"
#define  pb             push_back
#define  sym(s)         s="#"+s+"#";
#define  mp             make_pair
#define  s              second
#define  f              first
#define  dline          cerr<<"///REACHED///\n";
#define  debv(a)        for(auto it: a)cout<<it<<" ";cout<<endl;
#define  deb1(a)        cout<<a<<endl;
#define  deb2(a,b)      cout<<a<<" "<<b<<endl;
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<endl;
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());

typedef  long long             ll;
typedef  unsigned long long    ull;
typedef  long double           ld;
typedef  pair<ll,ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   MOD   =  1e+9+7;
const ll   INF   =  0x7f7f7f7f7f7f7f7f;
const int  INFi  =  0x7f7f7f7f;
const ll   N  =  3e+5+7;

vll adj[N];
int dx8[]={0,1,1,1,0,-1,-1,-1}, dy8[]={1,1,0,-1,-1,-1,0,1};
int dx4[]={0,1,0,-1}, dy4[]={1,0,-1,0};

//<<-----Declare Variable Here------->>//
int t=1;
ll n;
string s[1005],s1[1005];
ll vis[1005][1005];
ll h,w,ch,cw,dh,dw;
vpll v1[1000006];

//<<-----Implement Functions Here---->>//
//s is the array of strings(the grid)
struct Point{
	ll x, y;
};
queue<Point> q;
void bfspoint(ll id,Point p){
	q.push(p);
	while(q.size()){
		Point p=q.front();q.pop();
		ll x=p.x,y=p.y;
		vis[x][y] = id;v1[id].pb({x,y});
		for(ll k=0;k<4;k++)
		{
			ll dx=x+dx4[k];ll dy=y+dy4[k];
			if(s[dx][dy]=='.'){
				q.push({dx,dy});s[dx][dy]='#';
			}
		}
	}
}
struct Point1{
	ll x, y,time;
};
queue<Point1> q1;
ll bfspoint1(){
	while(q1.size()){
		Point1 p=q1.front();q1.pop();
		ll x=p.x,y=p.y,time = p.time;
		//cout<<x<<" "<<y<<" "<<time<<endl;
		if(x==dh&&y==dw){
			return time;
		}
		for(ll k=max(x-2,(ll)1);k<=min(x+2,h);k++)
		{
			for(ll g=max(y-2,(ll)1);g<=min(y+2,w);g++)
			{
				ll dx=k;ll dy=g;
				//cout<<k<<" "<<g<<endl;
				if(s1[dx][dy]=='.'){
					ll id = vis[dx][dy];
					for(auto it:v1[id])
					{
						Point1 a;a.x = it.f;a.y = it.s;a.time = time+1;
						q1.push(a);s1[a.x][a.y]='#';
					}
				}
			}
		}
	}
	return -1;
}
//<<-----Start of Main--------------->>//
void MAIN(){
		cin>>h>>w>>ch>>cw>>dh>>dw;
		for(ll i=1;i<=h;i++)
		{
			cin>>s[i];sym(s[i]);
		}
		s[0]=s[h+1]=string(w+2,'#');
		for(ll i=0;i<=h+1;i++)s1[i] = s[i];
		ll id = 1;
		
		for(ll i=1;i<=h;i++)
		{
			for(ll j=1;j<=w;j++)
			{
				if(vis[i][j]==0&&s[i][j]=='.')
				{
					Point p;p.x = i;p.y = j;
					bfspoint(id,p);id++;
				}
			}
		}
		ll e = vis[ch][cw];
		for(auto it:v1[e]){
			Point1 a;a.x = it.f;a.y = it.s;a.time = 0;
			q1.push(a);s1[a.x][a.y] = '#';
		}
		cout<<bfspoint1();
		

}

int main(){
    fastio;randomINIT;

    //cin>>t;
    while(t--){
        MAIN();
    }

    #ifndef ONLINE_JUDGE
    cout<<"\nTime Elapsed: " << 1.0*clock() / CLOCKS_PER_SEC << " sec\n";
    #endif
}

Measured memory_bytes: 57716