#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000003
   
int main(){
	int h,w,sx,sy,ex,ey,i,j,x,y;
	pair<int,int> t;
	cin>>h>>w;
	cin>>sx>>sy;
	cin>>ex>>ey;
	sx--;
	sy--;
	ex--;
	ey--;
	vector<vector<char>> board(h,vector<char>(w));

	for(i=0;i<h;i++){
		for(j=0;j<w;j++)
			cin>>board[i][j];
	}

	vector<vector<int>> dist(h,vector<int>(w));
	for(i=0;i<h;i++)
		for(j=0;j<w;j++)
			dist[i][j]=1e7;

	deque<pair<int,int>> q;
	q.push_front({sx,sy});
	dist[sx][sy]=0;
	int movex[]={0,-1,0,1};
	int movey[]={-1,0,1,0};
	while(!q.empty()){
		t=q.front();
		q.pop_front();
		
		for(i=0;i<4;i++){
			x=t.first+movex[i];
			y=t.second+movey[i];
			if(x<0 || x>=h || y<0 || y>=w || board[x][y]=='#')
				continue;
			if(dist[x][y] > dist[t.first][t.second]){
				dist[x][y]=dist[t.first][t.second];
				q.push_front({x,y});
			}
		}

		for(i=t.first-2;i<=t.first+2;i++){
			for(j=t.second-2;j<=t.second+2;j++){
				if(i<0 || i>=h || j<0 || j>=w || (i==t.first && j==t.second) ||board[i][j]=='#')
					continue;
				if(dist[i][j] > dist[t.first][t.second]+1){
					dist[i][j]=dist[t.first][t.second]+1;
					q.push_back({i,j});
				}
			}
		}
	}

	int ans = (dist[ex][ey]>=1e7)?-1:dist[ex][ey];
	cout<<ans<<endl;
}