//***********************
//Author:kibi11
//Quarantine Days
//***********************

#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define N 100005
#define MOD 1000000007
#define dd double
#define fr(i, n) for(int i = 0; i < n; i++)
#define fr1(i,a,b) for(int i=a;i<b;i++)
#define frr(i,a,k) for(int i=a;i>=k;i--)
#define same int t;cin>>t;while(t--)
#define check(x) cout << (#x) << " is " << (x) << endl;
#define pb push_back
#define mp make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define f first
#define s second
#define XOX
#define pi = 3.14159265358979323846

//////////////////////////////////////////////////////////////////////////
/////////////////DEBUGING ZONE//////////////////////////////

vector<string> vec_splitter(string s) {
	s += ',';
	vector<string> res;
	while(!s.empty()) {
		res.push_back(s.substr(0, s.find(',')));
		s = s.substr(s.find(',') + 1);
	}
	return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx, 
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; } 
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
	if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
	stringstream ss; ss << H;
	cerr << args[idx] << " = " << ss.str();
	debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
///////////////////////////////////////////////////////////////////////////
 
ll powermod(ll base, ll exp, ll mod) {
	ll result = 1;
	while (exp != 0) {
		if ((exp % 2) == 1) {
			result = (result * base) % mod;
		}
		base = (base * base) % mod;
		exp /= 2;
	}
	return result;
}

///////////////////////////////

/*
check a cycle in a directed graph: source codeforces;

void dfs(int v, int p){
  color[v] = 1; // GREY
  for(int w : g[v]){
    if(color[w] == 1){
      // you found a cycle, it's easy to recover it now.
    }
    if(color[w] == 0) dfs(w, v);
  }
  color[v] = 2; // BLACK
}
*/

///////////////////////////////////////////////////////////////////////////
//Fuctions if any:





///////////////////////////////////////////////////////////////////////////
int main() 
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tt = clock();

int h,w;
int sx,sy;
int ex,ey;
cin>>h>>w;
cin>>sx>>sy;
cin>>ex>>ey;
int dir[5]={0,1,0,-1,0};
vector<string> v;
fr(i,h)
{
	string temp;
	cin>>temp;
	v.pb(temp);
}
int dis[h][w];
fr(i,h)
{
	fr(j,w)
	{
		if(v[i][j]=='.')
		{
			dis[i][j]=1e7;
		}
	}
}
int r,c;
pair<int,int> temp;
// check("bit");
// for(auto s: dis) cout<<s.f.f<<" "<<s.f.s<<" "<<s.s<<endl;
dis[sx-1][sy-1]=0;
deque<pair<int,int> > d;
d.pb({sx-1,sy-1});
while(!d.empty())
{
	temp=d.front();
	d.pop_front();
	

		fr(i,4)
		{
			r=temp.f+dir[i];
			c=temp.s+dir[i+1];
			if((r>=0 and c>=0 and r<h and c<w)  and  v[r][c]=='.')
			{
				// debug()
				if(dis[r][c]>dis[temp.f][temp.s])
				{
								d.push_front({r,c});
								dis[r][c]=dis[temp.f][temp.s];
				}

			}
		}
		fr1(i,-2,3)
		{
			fr1(j,-2,3)
			{
			    r=temp.f+i;
				c=temp.s+j;
				if((r>=0 and c>=0 and r<h and c<w) and v[r][c]=='.')
				{
					if(dis[r][c]>dis[temp.f][temp.s]+1)
					{
						dis[r][c]=dis[temp.f][temp.s]+1;
						d.pb({r,c});
					}
				}
			}
		}

	

}

// debug(dis[{ex-1,ey-1}]);
cout<<((dis[ex-1][ey-1]==1e7)?-1: dis[ex-1][ey-1])<<endl;
// for(auto s: dis) cout<<s.f.f<<" "<<s.f.s<<" "<<s.s<<endl;
cerr << "TIME = " << (double) 1.0 * (clock() - tt) / CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}

Measured memory_bytes: 8788