#include<bits/stdc++.h>
#define ull unsigned long long
#define ll int64_t 
#define f(i,s,e) for(i=s;i<e;++i)
#define endl << "\n"
#define sp << " "
#define INF INFINITY
#define mset(a,b) memset(a,b,sizeof(a))
#define mp(a,b) make_pair(a,b)
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define PI acos(-1) 
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define eb(a) emplace_back(a)
#define print(x) for(auto it:x) cout<<it<<' '; cout<<"\n";

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;

ll inf  = 1000000007 ;
int d1[] = {1,-1,0,0} ; // DURL
int d2[] = {0,0,1,-1} ; // DURl

void print1(vector < pair<ll,ll> >&temp)
{
	for(int i = 0;i<temp.size();++i)
		cout << temp[i].F + 1 sp << temp[i].S + 1 endl;
	cout endl ;
}

bool valid(ll x,ll y,ll n,ll m)
{
	if(x<0 || x>=n || y<0 || y>=m)
		return false;
	return true ; 
}

void bfs1(ll h,ll w,vector < pair<ll,ll> > &vec,vector < vector <bool> >&arr,vector < vector <bool> > &visited,vector < vector <ll> >&level,bool &f)
{
	ll i,j,k,p,q;
	k = level[vec[0].first][vec[0].second] ;
	queue < pair<ll,ll> > q1 ;
	for(i=0;i<vec.size();++i)
		q1.push(vec[i]) ;
	while(q1.size())
	{
		pair <ll,ll> x = q1.front() ;
		for(i=0;i<4;++i)
		{
			p = d1[i] + x.F ;
			q = d2[i] + x.S ;
			if(!valid(p,q,h,w))
				continue ;
			if(!arr[p][q] || visited[p][q])
				continue ;
			visited[p][q] = true ;
			f = true ;
			level[p][q] = k ;
			q1.push(mp(p,q)) ;
			vec.push_back(mp(p,q)) ;
		}
		q1.pop() ;
	}
}

void bfs2(ll h,ll w,vector < pair<ll,ll> > &vec,vector < vector <bool> >&arr,vector < vector <bool> > &visited,vector < vector <ll> >&level,bool &f)
{
	ll i,j,k,p,q;
	k = level[vec[0].first][vec[0].second] + 1 ;
	vector < pair<ll,ll> > temp ;
	for(i=0;i<vec.size();++i)
	{
		for(j=vec[i].F-2;j<=vec[i].F+2;++j)
		{
			for(k=vec[i].S-2;k<=vec[i].S+2;++k)
			{
				p = j; q = k;
				if(!valid(p,q,h,w))
					continue ;
				if(!arr[p][q] || visited[p][q])
					continue ;
				if( (abs(j-vec[i].F)==1 && k==vec[i].S) || (abs(k-vec[i].S)==1 && j==vec[i].F) )
					continue ;
				visited[p][q] = true ;
				f = true ;
				level[p][q] = level[vec[i].F][vec[i].S] + 1;
				temp.pb(mp(p,q)) ;
			}
		}
	}
	vec.clear() ;
	vec = temp ;
}

int main()
{
	FAST_IO ;
	ll i,j,k,n,h,w,ch,cw,dh,dw;
	cin >> h >> w;
	cin >> ch >> cw >> dh >> dw ;
	ch-- ; cw-- ; dh-- ; dw-- ;
	vector < vector <bool> > arr(h,vector <bool> (w)) ;
	vector < vector <bool> > visited(h,vector <bool> (w,false)) ;
	vector < vector <ll> > level(h, vector <ll>(w,-1)) ;
	f(i,0,h)
	{
		f(j,0,w)
		{
			char x;
			cin >> x;
			if(x=='#')
				arr[i][j] = false;
			else
				arr[i][j] = true ;
		}
	}
	vector < pair <ll,ll> > temp ;
	temp.push_back(mp(ch,cw)) ;
	level[ch][cw] = 0;
	visited[ch][cw] = true ;
	while(true)
	{
		bool f = false ;
//		cout << "before BFS1\n" ;
//		print1(temp) ;
		bfs1(h,w,temp,arr,visited,level,f) ;
//		cout << "after BFS1\n" ;
//		print1(temp) ;
		f = false ;
		bfs2(h,w,temp,arr,visited,level,f) ;
//		cout << "after BFS2 and before BFS1\n" ;
//		print1(temp) ;
		if(!f)
			break ;
	}
//	f(i,0,h)
//	{
//		print(level[i]) ;
//		cout endl ;
//	}
	cout << level[dh][dw] endl ;
	return 0;
}

/*
If u haven't come up with an approach yet ,skip this one
  Try this when feeling stuck -
  1. Int overflow, array bounds
  2. Special cases (n=1?)
  3. Prove your approach to yourself(mathematical induction,counter-intuitive or stronger test cases)
  4. Calm down and don't look at standings and stop thinking about rating and think about the question
  5. Just do it! 
*/ 

//Benq's advice - 
/* 
	And following advices are must for me -
	* do something instead of nothing and stay organized
	* WRITE STUFF DOWN 
*/

//#define _GLIBCXX_DEBUG 
// It will convert WA verdict to runtime error if that error is caused due to out of bound excess , declare it at the top
//#pragma GCC optimize "trapv" 
// It will convert WA to runtime error if WA was caused due to int overflow error,declare it anywhere

Measured memory_bytes: 11936