// ######      ###      #######    #######    ##     #     #####        ###     ##### //
// #     #    #   #          #        #       # #    #    #     #      #   #     ###  //
// #     #   #     #        #         #       #  #   #   #       #    #     #    ###  //
// ######   #########      #          #       #   #  #   #           #########    #   //
// #     #  #       #     #           #       #    # #   #    ####   #       #    #   //
// #     #  #       #    #            #       #     ##   #    #  #   #       #        //
// ######   #       #   #######    #######    #      #    #####  #   #       #    #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a,n) for(ll i=0;i<(n);++i)	cin>>(a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<=(b);++i)
#define FORD(i,a,b) for(ll i=(a);i>=(b);--i)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define mp make_pair
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 100005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 200005;
const long double epsilon = 1e-9;
inline ll myceil(ll a, ll b) { if (a % b == 0)	return (a / b);	return (a / b + 1);}
inline ll binlen(ll n) {
	ll c = 0;
	while (n > 0)
		++c , n /= 2;
	return c;
}
ll binexp(ll a, ll b, ll m) {
	a %= m;
	ll res = 1;
	while (b > 0) {
		if (b & 1)
			res = res * a % m;
		a = a * a % m;
		b >>= 1;
	}
	return res;
}
ll modinvfermat(ll a, ll m)
{
	return binexp(a, m - 2, m);
}
void task(bool flag)
{
	if (flag)
		cout << "YES\n";
	else
		cout << "NO\n";
}
ll lcm(ll a, ll b)
{
	return ((1LL * a * b) / (__gcd(a, b)));
}

ll n, m, sx, sy, fx, fy;
vector<vector<char>> grid;
vvl dist;
vl dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
bool isvalid(ll x, ll y)
{
	if (x < 1 || x > n || y < 1 || y > m)
		return false;
	if (grid[x][y] == '#')
		return false;
	return true;
}
void bfs()
{
	priority_queue<pair<ll, ppl>, vector<pair<ll, ppl>>, greater<pair<ll, ppl>>> q;
	dist[sx][sy] = 0;
	q.push({0, {sx, sy}});
	while (!q.empty())
	{
		ll x = q.top().ss.ff, y = q.top().ss.ss, d = q.top().ff;
		q.pop();
		loop(i, 4)
		{
			if (isvalid(x + dx[i], y + dy[i]))
			{
				if (d < dist[x + dx[i]][y + dy[i]])
				{
					dist[x + dx[i]][y + dy[i]] = d;
					q.push({d, {x + dx[i], y + dy[i]}});
				}
			}
		}
		FOR(i, -2, 2)
		{
			FOR(j, -2, 2)
			{
				if (isvalid(x + i, y + j))
				{
					if (d + 1 < dist[x + i][y + j])
					{
						dist[x + i][y + j] = d + 1;
						q.push({d + 1, {x + i, y + j}});
					}
				}
			}
		}
	}
}

signed main() {
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt", "r", stdin);
// 	freopen("output.txt", "w", stdout);
// #endif
	fast
	cin >> n >> m >> sx >> sy >> fx >> fy;
	grid = vector<vector<char>>(n + 1, vector<char>(m + 1));
	dist = vvl(n + 1, vl(m + 1, inf));
	FOR(i, 1, n)	FOR(j, 1, m)	cin >> grid[i][j];
	bfs();
	if (dist[fx][fy] == inf)
		cout << -1 << END;
	else
		cout << dist[fx][fy] << END;



// #ifndef ONLINE_JUDGE
// 	cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
// #endif
	return 0;
}

Measured memory_bytes: 12684