#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string.h>
#define s(n)                        scanf("%d",&n)
#define sc(n)                       scanf("%c",&n)
#define sl(n)                       scanf("%I64d",&n)
#define sf(n)                       scanf("%lf",&n)
#define ss(n)                       scanf("%s",n)
#define INF                         (int)1e9
#define EPS                         1e-9
#define bitcount                    __builtin_popcount
#define gcd                         __gcd
#define forall(i,a,b)               for(int i=a;i<b;i++)
#define foreach(v, c)               for( typeof( (c).begin()) v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ( (b).find(a) != (b).end())
#define pb                          push_back
#define fill(a,v)                    memset(a, v, sizeof a)
#define sz(a)                       ((int)(a.size()))
#define mp                          make_pair
#define MOD                         1000000007
typedef long long ll;
/*For you see , each day i love you
 more.Today more than yesterday
  less than tomorrow.*/
using namespace std;
int vis[1001][1001];
int dx[4] = {-1 , 0 , 0 , 1};
int dy[4] = {0 , -1 , 1 , 0};

int main()
{
	int h,w;
	cin>>h>>w;
	int x1 , y1 , x2 , y2;
	cin>>x1>>y1>>x2>>y2;
	x1--;y1--;x2--;y2--;
	
	vector<string> vec;
	
	for(int i = 0;i<h;i++)
	{
		string str;
		cin>>str;
		vec.push_back(str);	
	}
	
	int col = 1;
	vector<vector<int> > g(h*w+2);
	
	for(int i = 0;i<h;i++)
	{
		for(int j = 0;j<w;j++)
		{
			if(vis[i][j] != 0 || vec[i][j] == '#') continue;
			
			queue<int> q;
			q.push(i);
			q.push(j);
			vis[i][j] = col;
			
			while(!q.empty())
			{
				int x = q.front();q.pop();
				int y = q.front();q.pop();
				g[col].push_back(x);
				g[col].push_back(y);
				for(int k = 0;k<4;k++)
				{
					int xx = dx[k] + x;
					int xy = dy[k] + y;
					
					if(xx < 0 || xx >= h || xy <0 || xy >= w || vec[xx][xy] == '#' || vis[xx][xy]) continue;
					
					q.push(xx);
					q.push(xy);
					vis[xx][xy] = col;
					
				}
			 } 
			 
			 col++;
		}	
	}
	
//	for(int i = 1;i<col;i++)
//	{
//		for(int j = 0;j<g[i].size();j+=2)
//		{
//			cout<<'('<<g[i][j]<<','<<g[i][j+1]<<')'<<" ";	
//		}
//		cout<<endl;
//	}
	
	int start = vis[x1][y1];
	int end = vis[x2][y2];
	
	queue<int> q;
	vector<int> viss(col+1 , 0);
	q.push(start);q.push(0);
	viss[start] = 1;
	
	while(!q.empty())
	{
		int x = q.front();q.pop();
		int dis = q.front();q.pop();
		
		if(x == end)
		{
			cout<<dis<<endl;
			return 0;
		}
		
		for(int i = 0;i<g[x].size();i+=2)
		{
			int xx = g[x][i];
			int xy = g[x][i+1];
			
			for(int k = xx-2;k<=xx+2;k++)
			{
				for(int l = xy-2;l<=xy+2;l++)
				{
					if(k<0 || k>=h || l <0 || l>=w || vec[k][l] =='#' || viss[vis[k][l]]) continue;
					
					q.push(vis[k][l]);
					q.push(dis+1);
					viss[vis[k][l]] = 1;
				}
			}	
		}	
	}
	
	cout<<"-1"<<endl;
	
	return 0;
}

Measured memory_bytes: 38344