#include<bits/stdc++.h>
using namespace std;
const int maxn=2009;
const int inf=1000000000;

int a[4]={0,0,1,-1};
int b[4]={1,-1,0,0};

int n,m;
int cx,cy;
int dx,dy;

char s[maxn][maxn];
int d[maxn][maxn];

int l,r;
int q[maxn*maxn*2][2];
void bfs(){
	l=r=maxn*maxn;
	q[l][0]=cx;q[l][1]=cy;
	d[cx][cy]=0;
	while(l<=r){
		int x=q[l][0],y=q[l][1];++l;
		for(int k=0;k<4;++k){
			int xx=x+a[k];
			int yy=y+b[k];
			if(xx<=0||yy<=0||xx>n||yy>m)continue;
			if(s[xx][yy]=='#')continue;
			if(d[x][y]>=d[xx][yy])continue;
			d[xx][yy]=d[x][y];
			--l;q[l][0]=xx;q[l][1]=yy;
		}
		for(int i=x-2;i<=x+2;++i){
			for(int j=y-2;j<=y+2;++j){
				int xx=i,yy=j;
				if(xx<=0||yy<=0||xx>n||yy>m)continue;
				if(s[xx][yy]=='#')continue;
				if(d[x][y]+1>=d[xx][yy])continue;
				d[xx][yy]=d[x][y]+1;
				++r;q[r][0]=xx;q[r][1]=yy;
			}
		}
	}
}

int main(){
	scanf("%d%d",&n,&m);
	scanf("%d%d",&cx,&cy);
	scanf("%d%d",&dx,&dy);
	for(int i=1;i<=n;++i)scanf("%s",s[i]+1);
	
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			d[i][j]=inf;
		}
	}
	
	bfs();
	if(d[dx][dy]==inf)printf("-1\n");
	else printf("%d\n",d[dx][dy]);
	
	return 0;
}