#include <assert.h>
#include <bits/stdc++.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <fstream>
#include <unordered_map>
#include <iostream>
#include <list>
#include <set>
#include <stack>
#include <string>
#include <vector>

 
using namespace std;
 
typedef string str;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<long long, long long> pl;

#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define trav(a, x) for (auto &a : x)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define PRINT_ARRAY(a, N)        \
  F0R(z, N) cout << a[z] << " "; \
  cout << "\n"
 
const int MOD = 1000000007;  // 998244353
const ll INF = 1e18;
const ld PI = 4 * atan((ld)1);


ll H, W, Ch, Dh, Cw, Dw;

inline int idx(int i, int j) {
		return H * i + j;
}

void dijkstra(const vector<vector<pl>>& AdjList, int s, int destination, vector<ll>& d) {
		int n = AdjList.size();
		d.assign(n, INF);
		vector<bool> u(n, false);
		d[s] = 0;
		
		priority_queue<pl, vector<pl>, greater<pl>> q;

		q.push({0, s});
		while (!q.empty()) {
				pl next = q.top();
				q.pop();
				ll u = next.s, dist = next.f;
				if (u == destination) {
						break;
				}
				for (auto v : AdjList[u]) {
						if (dist + v.f < d[v.s]) {
								d[v.s] = dist + v.f;
								q.push({dist + v.f, v.s});
						}
				}
		}
}


int main() {
		ios_base::sync_with_stdio(false);
		cin.tie(NULL);
		cin >> H >> W >> Ch >> Cw >> Dh >> Dw;

		vector<vector<char>> grid(H, vector<char>(W));
		F0R(i, H) F0R(j, W) cin >> grid[i][j];

		int sz = idx(H - 1, W - 1);
		vector<vector<pl>> AdjList(sz + 1);


		for (int i = 0; i < H; ++i) {
				for (int j = 0; j < W; ++j) {
						int start_row = i - 2, end_row = i + 2;
						int start_col = j - 2, end_col = j + 2;

						for (int k = start_row; k <= end_row; ++k) {
								for (int l = start_col; l <= end_col; ++l) {
										if (k < H && l < W && k >= 0 && l >= 0 && (k != i || l != j) && grid[k][l] == '.') {
												AdjList[idx(i, j)].push_back({1, idx(k, l)});
										}
								}
						}

						int l = i - 1, r = i + 1, u = j - 1, d = j + 1;

						if (l >= 0 && grid[l][j] == '.') {
								AdjList[idx(i, j)].push_back({0, idx(l, j)});
						}

						if (r < H && grid[r][j] == '.') {
								AdjList[idx(i, j)].pb({0, idx(r, j)});
						}

						if (u >= 0 && grid[i][u] == '.') {
							AdjList[idx(i, j)].pb({0, idx(i, u)});
						}

						if (d < W && grid[i][d] == '.') {
							AdjList[idx(i, j)].pb({0, idx(i, d)});
						}
				}
		}
		int src = idx(Ch - 1, Cw - 1);
		int destination = idx(Dh - 1, Dw - 1);

		vector<ll> dist(sz + 1);

		dijkstra(AdjList, src, destination, dist);

		if (dist[destination] == INF) {
				cout << -1 << endl;
		} else {
				cout << dist[destination] << endl;
		}
}


Measured memory_bytes: 346476