#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 100001;
static const ll MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline T int_ceil(T a, T b) { T res = a / b; if(a % b != 0) res++; return res; }
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define print(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define print(...)
#endif

// <単一始点最短経路>
// N 頂点数
// start 始点
int N;
ll H, W;
ll Ch, Cw;
ll Dh, Dw;
vector<string> vs;
ll dp[1010][1010];
vector<vector<int> > g;
void bfs(int start){
    queue<int> q;
    q.push(start);
    vector<ll> dist(N, -1);
    dist[start] = 0;

    while(!q.empty()){
        int v = q.front(); q.pop();
        for(int u : g[v]){
            if(dist[u] != -1) continue;
            dist[u] = dist[v] + 1; // 始点からの距離
            //  --- 処理 ---
            //  -----------
            q.push(u);
        }
    }
}

int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};

int main(int argc, const char * argv[]) {
	std::cout << std::fixed << std::setprecision(15);

	cin >> H >> W;
	cin >> Ch >> Cw;
	Ch--; Cw--;
	cin >> Dh >> Dw;
	Dh--; Dw--;
	vs.resize(H);
	rep(i, H) cin >> vs[i];
	memr(dp, -1);
	dp[Ch][Cw] = 0;
	queue<pair<ll, ll> > q;
	queue<pair<ll, ll> > q_out;
	queue<pair<ll, ll> > q_out2;
	q.push({Cw, Ch});
	q_out.push({Cw, Ch});
	// print(Cw, Ch);
	while(!q_out.empty() || !q.empty()){
		while(!q.empty()){

			pair<ll, ll> v2 = q.front(); q.pop();
			rep(i, 4){
				int xi = dy[i]; int xj = dx[i];
				if(v2.first + xj < 0 || W <= v2.first + xj) continue;
				if(v2.second + xi < 0 || H <= v2.second + xi) continue;
				if(vs[v2.second + xi][v2.first + xj] == '#') continue;
				if((dp[v2.second + xi][v2.first + xj] != -1 && dp[v2.second + xi][v2.first + xj] <= dp[v2.second][v2.first])) continue;
				dp[v2.second + xi][v2.first + xj] = dp[v2.second][v2.first];
				// print(v2.first, v2.second, v2.first + xj, v2.second + xi);
            	q.push({v2.first + xj,v2.second + xi});
            	q_out.push({v2.first + xj,v2.second + xi});
			}
		}
		while(!q_out.empty()){
			pair<ll, ll> v = q_out.front(); q_out.pop();

			rep(i, 5){
				rep(j, 5){
					if(i == 2 && j == 2) continue;
					int xi = i - 2; int xj = j - 2;
					if(v.first + xj < 0 || W <= v.first + xj) continue;
					if(v.second + xi < 0 || H <= v.second + xi) continue;
					if(vs[v.second + xi][v.first + xj] == '#') continue;
					if(dp[v.second + xi][v.first + xj] != -1 && dp[v.second + xi][v.first + xj] <= dp[v.second][v.first] + 1) continue;
					dp[v.second + xi][v.first + xj] = dp[v.second][v.first] + 1;
					// print(v.first, v.second, v.second + xi, v.first + xj, dp[v.second + xi][v.first + xj]);
					q.push({v.first + xj,v.second + xi});
            		q_out2.push({v.first + xj,v.second + xi});
				}
			}
		}
		q_out = q_out2;
		while(!q_out2.empty()) q_out2.pop();
		// cout << "____________________________" << endl;
		// rep(i, H){
		// 	rep(j, W){
		// 		if(j) cout << " ";
		// 		cout << dp[i][j];
		// 	}
		// 	cout << endl;
		// }
	}

	// rep(i, H){
	// 	rep(j, W){
	// 		if(j) cout << " ";
	// 		cout << dp[i][j];
	// 	}
	// 	cout << endl;
	// }

	cout << dp[Dh][Dw] << endl;

	return 0;
}


Measured memory_bytes: 13632