/* author:hellojim */
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int ddx[24]={-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,0,0,0,0,1,1,1,1,1,2,2,2,2,2};
int ddy[24]={-2,-1,0,1,2,-2,-1,0,1,2,-2,-1,1,2,-2,-1,0,1,2,-2,-1,0,1,2};
int h,w;
int ch,cw,dh,dw;
char c[1010][1010];
bool used[1010][1010];
int cl[1010][1010];
vector<pii>v[1010101];
int cnt;
bool have[1010101];
vi d[1010101];
int dis[1010101];
bool ok(int x,int y)
{
	return x>=0&&y>=0&&x<h&&y<w;
}
void dfs(int x,int y){
	cl[x][y]=cnt;
	used[x][y]=1;
	v[cnt].pb(m_p(x,y));
	rep(i,4){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(!ok(xx,yy)||c[xx][yy]=='#'||used[xx][yy])continue;
		else dfs(xx,yy);
	}
}
int main()
{
	ios::sync_with_stdio(0);
	//think twice,code once
	rep(i,1000001)dis[i]=1e9;
	cnt=0;
	cin>>h>>w;
	cin>>ch>>cw>>dh>>dw;
	rep(i,h)
		rep(j,w)
			cin>>c[i][j];
	ch--;cw--;dh--;dw--;
	rep(i,h)
		rep(j,w)
			if(!used[i][j]&&c[i][j]=='.')
			{
				dfs(i,j);
				cnt++;
			}
	queue<int>q;
	have[cl[ch][cw]]=1;
	q.push(cl[ch][cw]);
	while(!q.empty())
	{
		int x=q.front();
		q.pop();
		rep(i,v[x].size())
		{
			rep(j,24)
			{
				int xx=v[x][i].X+ddx[j];
				int yy=v[x][i].Y+ddy[j];
				if(have[cl[xx][yy]]||!ok(xx,yy)||c[xx][yy]=='#')continue;
				q.push(cl[xx][yy]);
				have[cl[xx][yy]]=1;
				d[x].pb(cl[xx][yy]);
				d[cl[xx][yy]].pb(x);
			}
		}	
	}
	if(!have[cl[dh][dw]])
	{
		cout<<-1<<endl;
		return 0;
	}
	dis[cl[ch][cw]]=0;
	q.push(cl[ch][cw]);
	while(!q.empty()){
		int x=q.front();
		q.pop();
		rep(i,d[x].size()){
			int xx=d[x][i];
			if(dis[xx]<=dis[x]+1)
			continue;
			dis[xx]=dis[x]+1;
			q.push(xx);
		}
	}
	cout<<dis[cl[dh][dw]]<<endl;
	return 0;
}



Measured memory_bytes: 70708