#include <iostream>
#include<bits/stdc++.h>
#define ll long long int
#define lld long double
#define F first
#define S second
#define f(i,a,b) for(int i=a;i<=b;i++)
#define g(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mh make_heap
#define ph push_heap
#define pq priority_queue
#define bits(x) __builtin_popcountll(x)
#define op(x) cout<<"Case #"<<x<<": "
#define op1(x) cout<<"Scenario #"<<x<<": "
#define endl "\n"
using namespace std;
const ll mod = 1000000007;
const ll INF = 1e18;
const int N = 18;




void solve(int t)
{
	ll n, m;
	cin >> n >> m;
	ll sx, sy;
	ll dx, dy;

	cin >> sx >> sy;
	cin >> dx >> dy;
	sx--;
	sy--;
	dx--;
	dy--;

	string s[n];
	for (int i = 0; i < n; i++) cin >> s[i];

	bool vis[n][m];
	ll dis[n][m];






	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			vis[i][j] = 0;
			dis[i][j] = INF;


		}

	}


	dis[sx][sy] = 0;
	set<pair<ll, pair<ll, ll> > > st;
	st.insert({0, {sx, sy}});

	while (!st.empty())
	{
		pair<ll, pair<ll, ll> > tmp = *(st.begin());

		pair<ll, ll> pp = tmp.S;
		ll x = pp.F;
		ll y = pp.S;

		st.erase(st.begin());

		for (int i = 0; i <= 2; i++)
		{
			for (int j = 0; j <= 2; j++)
			{
				if (i != 0 or j != 0)
				{
					if (x - i >= 0 and y - j >= 0 and s[x - i][y - j] == '.')
					{
						ll wt;
						if (i + j == 1) wt = 0;
						else wt = 1;

						ll p = x - i;
						ll q = y - j;

						if (dis[p][q] > dis[pp.F][pp.S] + wt)
						{
							if (dis[p][q] != INF)
							{
								st.erase(st.find({dis[p][q], {p, q}}));
							}
							dis[p][q] = dis[pp.F][pp.S] + wt;
							st.insert({dis[p][q], {p, q}});


						}

					}

					if (x + i < n and y - j >= 0 and s[x + i][y - j] == '.')
					{
						ll wt;
						if (i + j == 1) wt = 0;
						else wt = 1;

						ll p = x + i;
						ll q = y - j;


						if (dis[p][q] > dis[pp.F][pp.S] + wt)
						{
							if (dis[p][q] != INF)
							{
								st.erase(st.find({dis[p][q], {p, q}}));
							}
							dis[p][q] = dis[pp.F][pp.S] + wt;
							st.insert({dis[p][q], {p, q}});


						}

					}
					if (x - i >= 0 and y + j < m and s[x - i][y + j] == '.')
					{
						ll wt;
						if (i + j == 1) wt = 0;
						else wt = 1;

						ll p = x - i;
						ll q = y + j;


						if (dis[p][q] > dis[pp.F][pp.S] + wt)
						{
							if (dis[p][q] != INF)
							{
								st.erase(st.find({dis[p][q], {p, q}}));
							}
							dis[p][q] = dis[pp.F][pp.S] + wt;
							st.insert({dis[p][q], {p, q}});


						}

					}
					if (x + i < n and y + j < m and s[x + i][y + j] == '.')
					{
						ll wt;
						if (i + j == 1) wt = 0;
						else wt = 1;

						ll p = x + i;
						ll q = y + j;


						if (dis[p][q] > dis[pp.F][pp.S] + wt)
						{
							if (dis[p][q] != INF)
							{
								st.erase(st.find({dis[p][q], {p, q}}));
							}
							dis[p][q] = dis[pp.F][pp.S] + wt;
							st.insert({dis[p][q], {p, q}});


						}
					}
				}
			}
		}
	}

	/*

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			cout << dis[i][j] << " ";
		}
		cout << endl;
	}*/




	if (dis[dx][dy] == INF)
	{
		cout << -1 << endl;
		return;
	}
	cout << dis[dx][dy] << endl;





}


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




	int t = 1;
	//cin >> t;



	for (int i = 1; i <= t; i++)
	{
		solve(i);

	}
}

Measured memory_bytes: 13592