#include <bits/stdc++.h>

#define ll long long
#define MP make_pair
#define ERR cout << "================================================\n"
#define fi first
#define se second
#define PB push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define print(v)          \
	for (auto x : v)      \
	{                     \
		cout << x << " "; \
	}
#define MAX(v) *max_element(all(v))
#define MIN(v) *min_element(all(v))
#define SP << " " <<
#define W(t)  \
	ll t;     \
	cin >> t; \
	while (t--)
#define FIO                           \
	freopen("input.txt", "r", stdin); \
	freopen("output.txt", "w", stdout);
#define FAST                 \
	ios::sync_with_stdio(0); \
	cin.tie(0);              \
	cout.tie(0);

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

const ll INF = 1e18;
const ll NEG_INF = -1 * (1e18);
using namespace std;

void usaco(string prob)
{
	freopen((prob + ".in").c_str(), "r", stdin);
	freopen((prob + ".out").c_str(), "w", stdout);
}

ll h, w;

char mat[1005][1005];

bool check(ll i, ll j)
{
	return i >= 0 && i < h && j >= 0 && j < w && mat[i][j] == '.';
}

bool check1(ll k)
{
	ll j = k % w;
	ll i = (k - j) / w;
	return check(i, j);
}

bool vis[1000005];

void solve()
{
	/* Write Solution Here */
	cin >> h >> w;
	ll ch, cw, dh, dw;
	cin >> ch >> cw >> dh >> dw;
	--ch;
	--cw;
	--dh;
	--dw;

	vll dist(1000005, INF);

	ll s = w * ch + cw;
	ll d = w * dh + dw;

	forn(i, h)
	{
		forn(j, w)
		{
			cin >> mat[i][j];
		}
	}

	dist[s] = 0;
	// vis[s] = 1;
	deque<ll> dq;
	dq.push_front(s);
	while (!dq.empty())
	{
		ll u = dq.front();
		dq.pop_front();
		forn(i, 5)
		{
			forn(j, 5)
			{
				ll v = u - (2 - i) * w - (2 - j);
				// if travelling u to v relaxes v
				ll j1 = v % w, j0 = u % w;
				ll i1 = (v - j1) / w, i0 = (u - j0) / w;
				if (check1(v) && abs(i0 - i1) <= 2 && abs(j0 - j1) <= 2)
				{
					// node v exist in the maze
					// dist(u-->v)<dist(v) ==> V is relaxed
					ll distance = !(v == u - 1 || v == u + 1 || v == u - w || v == u + w);
					if (distance + dist[u] < dist[v])
					{
						dist[v] = distance + dist[u];
						if (distance == 0)
						{
							if (find(all(dq), v) == dq.end())
							{
								dq.push_front(v);
							}
						}
						else
						{
							if (find(all(dq), v) == dq.end())
							{
								dq.push_back(v);
							}

							// vis[v] = 1;
						}
					}
				}
			}
		}
		// cout << "Source: " << u << "\n";

		// forn(i, h)
		// {
		// 	forn(j, w)
		// 	{
		// 		if (mat[i][j] == '#')
		// 		{
		// 			cout << "# ";
		// 		}
		// 		else if (dist[w * i + j] == INF)
		// 		{
		// 			cout << "N ";
		// 		}
		// 		else
		// 		{
		// 			cout << dist[w * i + j] << " ";
		// 		}
		// 	}
		// 	cout << "\n";
		// }
		// cout << "\n";
	}

	if (dist[d] == INF)
	{
		dist[d] = -1;
	}
	cout << dist[d] << "\n";
}

int main()
{
	//FIO
	FAST

		//usaco("cowlands");

		ll TC = 1;
	/*
	Uncomment when multiple test cases
	*/

	// cin>>TC;
	for1(tt, TC)
	{
		solve();
	}
	return 0;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
*/


Measured memory_bytes: 12004