#include <bits/stdc++.h>
using namespace std;
 
template <typename A, typename B>
string to_string(pair<A, B> p);
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
 
string to_string(const string& s) {
  return '"' + s + '"';
}
 
string to_string(const char& ch) {
	string s = "";
	s += ch;
	return "'" + s + "', ";
}
 
string to_string(const char* s) {
  return to_string((string) s);
}
 
string to_string(bool b) {
  return (b ? "true" : "false");
}
 
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
 
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
 
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
 
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
 
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
 
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
 
void debug_out() { cout << endl; }
 
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << " " << to_string(H);
  debug_out(T...);
}
 
#ifdef LOCAL_DEFINE
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
   
#define int long long
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
typedef pair<int, int> pii;
const int mod = (int) 1000000007; // 998244353;
const int inf = (int) 2e18;

vector<int> dsu;
vector<int> num;

void init(int n)
{
	dsu.resize(n + 10);
	num.resize(n + 10);
	for(int i = 0; i <= n; ++ i)
	{
		dsu[i] = i;
		num[i] = 1;
	}
}

int root(int p)
{
	return (p == dsu[p]) ? p : dsu[p] = root(dsu[p]);
}

void merge(int u, int v)
{
	u = root(u);
	v = root(v);
	if(u != v)
	{
		if(num[u] < num[v]) swap(u, v);
		num[u] += num[v];
		dsu[v] = u;
	}
}

int h, w;
int a[1010][1010];
int srcx, srcy, destx, desty;
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
vector<vector<pii>> comp(2000010);
vector<int> dist(2000010);

bool isv(int x, int y)
{
	return x >= 1 && x <= h && y >= 1 && y <= w && a[x][y] == 1;
}
int get(int x, int y)
{
	return x * 1000 + y;
}

int32_t main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(9);
	#ifdef LOCAL_DEFINE
		freopen("in.txt", "rt", stdin);
	#endif
	
	cin >> h >> w >> srcx >> srcy >> destx >> desty;
	for(int i = 1; i <= h; ++ i)
	{
		for(int j = 1; j <= w; ++ j)
		{
			char ch;
			cin >> ch;
			// debug(i, j, ch);
			a[i][j] = (ch == '.' ? 1 : 0);
		}
	}
	init(2e6);
	for(int i = 1; i <= h; ++ i)
	{
		for(int j = 1; j <= w; ++ j)
		{
			if(a[i][j] == 0) continue;
			for(int k = 0; k < sz(dx); ++ k)
			{
				int newx = i + dx[k];
				int newy = j + dy[k];
				if(!isv(newx, newy)) continue;
				merge(get(i, j), get(newx, newy));
			}
		}
	}	
	map<int, int> mp;
	int pt = 0;
	int start = -1;
	for(int i = 1; i <= h; ++ i)
	{
		for(int j = 1; j <= w; ++ j)
		{
			if(i == srcx && j == srcy)
			{
				start = root(get(i, j));
			}
			comp[root(get(i, j))].push_back({i, j});
		}
	}
	debug(start);
	dist.assign(2000010, inf);
	dist[start] = 0;
	
	set<int> done;
	queue<int> q;
	q.push(start);
	
	while(!q.empty())
	{
		int at = q.front(); q.pop();
		for(pii& is : comp[at])
		{
			int x = is.first, y = is.second;
			for(int diffx : {-2, -1, 0, 1, 2})
			{
				for(int diffy : {-2, -1, 0, 1, 2})
				{
					int newx = x + diffx;
					int newy = y + diffy;
					if(!isv(newx, newy)) continue;
					dist[root(get(newx, newy))] = min(dist[root(get(newx, newy))], dist[root(get(x, y))] + 1);
					if(!done.count(root(get(newx, newy))))
					{
						done.insert(root(get(newx, newy)));
						q.push(root(get(newx, newy)));
					}
				}
			}
		}
	}
	
	for(vector<pii>& dep : comp)
	{
		if(sz(dep))
			debug(dep);
	}
	int ans = dist[root(get(destx, desty))];
	if(ans == inf) ans = -1;
	cout << ans << '\n';
	
	return 0;
}
 
 
 
 

Measured memory_bytes: 143920