#include <iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<string>
#include <iomanip> 
#include <stack> 
#include <cmath> 

#define maxn 10001
#define LL long long
#define inf 10000000000
#define mod 1000000007
#define pi 3.14159265359
using namespace std;
int dx[24] = { 1,0,-1,0 ,2,0,-2,0 ,-1,-1,-2,-2 ,1,1,2,2    ,-1,-1,-2,-2 ,1,1,2,2 };
int dy[24] = { 0,1,0,-1 ,0,2,0,-2 ,-1,-2,-1,-2 ,-1,-2,-1,-2,1,2,1,2     ,1,2,1,2  };


LL n, m, k;
LL arr[maxn][maxn];
LL arr2[maxn];
int visit[maxn][maxn];
//int cache[maxn];
//int c[maxn];
//int g[maxn];
//int dfsnum = 0;
//vector<vector<int>>rvi;
//vector<vector<int>>scc;
//vector<pair<int, int>>ett;
//stack<int>st;
//bool flg = 1;
//LL s, d;
//
//int findp(int a) {
//	if (a == c[a])return a;
//	return c[a] = findp(c[a]);
//}
//void unionp(int a, int b) {
//	a = findp(a);
//	b = findp(b);
//
//	if (g[a] < g[b])swap(a, b);
//	c[b] = a;
//	g[a] += g[b];
//	g[b] = 1;
//
//}
//LL power(LL x, LL y) {
//	LL ret = 1;
//	while (y > 0) {
//		if (y % 2) {
//			ret *= x;
//			ret %= mod;
//		}
//		x *= x;
//		x %= mod;
//		y /= 2;
//	}
//	return ret;
//}
//void dfs2(int v) {
//	cache[v] = 1;
//	scc.back().push_back(v);
//	for (int nv : rvi[v]) {
//		if (cache[nv])continue;
//		dfs2(nv);
//	}
//
//}
//LL fac(LL n) {
//	if (n == 1) return 1;        
//	return (n * fac(n - 1) - power(2,n-1) )%mod;
//}
//int fac2(LL n) {
//	LL res, i;
//	if (n == 1)return n;
//	res = n;
//	for (int i = n - 1; i > 0; --i) {
//		res *= i;
//		res %= mod;
//	}
//	return res;
//}

LL gcd(LL a, LL b) {
	while (b != 0) {
		LL r = a % b;
		a = b;
		b = r;
	}
	return a;
}
vector<LL>vi;
vector<vector<int>>graph;
void dfs(int v, int p) {
	arr2[v] = 1;
	for (int nv : graph[v]) {
		if (nv == p)continue;
		dfs(nv, v);
		arr2[v] += arr2[nv];
	}
	if (p != 0)
		vi.push_back(arr2[v] * (n - arr2[v]));
}

int main() {
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	//int t;   cin >> t; cin.ignore();
	int t = 1;
	for (int test_case = 0; test_case < t; ++test_case) {
		int flg = 1;
		LL ans1 = 0, ans2 = 0;
		string ans[2] = { "No","Yes"};
		cin >> n>>m;
		LL stx, sty, edx, edy;
		cin >> sty >> stx >> edy >> edx;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				char input;
				cin >> input;
				if (input == '#')
					arr[i][j] = 1;
				else
					arr[i][j] = 0;

				visit[i][j] = -1;
			}
		}
		edx--, edy--, stx--, sty--;
		priority_queue<pair<pair<int,int>, pair<int, int>>>pq;
		queue<pair<int, int>>qp;
		pq.push({ {1,0},{sty,stx} });
		visit[sty][stx] = 0;

		while (!pq.empty()) {
			int x = pq.top().second.second;
			int y = pq.top().second.first;
			int sep = pq.top().first.first;
			int pre = -pq.top().first.second;
			//visit[y][x] = pre;

			if (x == edx && y == edy) {
				break;
			}
			pq.pop();
			for (int dir = 0; dir < 24; ++dir) {
				int nx = x + dx[dir];
				int ny = y + dy[dir];
				int nsep=0;
				int npre = pre;
				if (dir < 4)nsep = 1;
				if (nsep == 0)npre++;
				if (nx < 0 || nx >= m || ny < 0 || ny >= n)continue;
				if (arr[ny][nx] == 1)continue;
				if (visit[ny][nx] == -1 || visit[ny][nx] > npre) {

					visit[ny][nx] = npre;
					pq.push({ {nsep,-npre},{ny,nx} });
				}
			}
		}
		ans1 = visit[edy][edx];
		cout << ans1<< '\n';
	}
	return 0;
}


Measured memory_bytes: 22964