#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define mp make_pair                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
bool vis[1001][1001];
ll dist[1001][1001];
char grid[1001][1001];
int n,m;
bool chk(int x,int y)
{
	return (x>=0 && x<n && y>=0 && y<m && grid[x][y] != '#');
}
int main()
{
	owo
	cin>>n>>m;
	pii start,end;
	cin>>start.fi>>start.se;
	start.fi--;start.se--;
	cin>>end.fi>>end.se;	
	end.fi--;end.se--;
	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++){cin>>grid[i][j];dist[i][j] = INF;vis[i][j]=0;}
	
	if(grid[start.fi][start.se] == '#' || grid[end.fi][end.se] == '#'){
		cout<<-1;
		return 0;
	}
	deque<pii>q;
	q.pb(start);
	dist[start.fi][start.se] = 0;
	while(!q.empty()){
	   int x = q.front().fi;
	   int y = q.front().se;
	   q.pop_front();
	   vis[x][y] = 1;
	   for(int i=0;i<4;i++){
		   int nx = x+dx[i];
		   int ny = y+dy[i];
		   if(chk(nx,ny) && !vis[nx][ny]){
			    if(dist[nx][ny] > dist[x][y]){
					dist[nx][ny] = dist[x][y];
					vis[nx][ny] = 1;
					q.push_front({nx,ny});
				}
		   }
	   }
	    for(int i=x-2;i<=x+2;i++){
		   for(int j=y-2;j<=y+2;j++){
			  if(!chk(i,j))continue;
			  if(!vis[i][j] && dist[i][j]>dist[x][y]+1){
				  dist[i][j] = dist[x][y]+1;
				  q.pb({i,j});
			  }  
		   }
	   }
	}
	/*for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout<<dist[i][j]<<" ";
		}
		cout<<'\n';
	}*/
	cout<<(dist[end.fi][end.se]==INF?-1:dist[end.fi][end.se]);
	
}


Measured memory_bytes: 13356