#include <iostream>
#include <cstdlib>
#include <bitset>
#include <map>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>
#include <sstream>
#include <math.h>

using namespace std;

#define ll long long
#define vecll vector<long long>
#define vec2ll vector<vector<long long>>
#define vecchar vector<char>
#define vecstr vector<string>
#define vec2str vector<vector<string>>
#define vecbool vector<bool>
#define vec2bool vector<vector<bool>>
#define pairll pair<ll,ll>
#define vecpairll vector<pair<long long,long long>>
#define forll(s,a,b) for(long long s = a;s < b;s++)
#define forllde(s,a) for(long long s = a;s > 0;s--)

#define Pi 3.1415926535897932384626

bool isInFirld(int col, int row, const pairll &point)
{
	const int c = point.second;
	const int r = point.first;
	return (0 <= c && c < col) && (0 <= r && r < row);
}

int main() {
	ll h,w, ans;
	bool flag = false;

	cin >> h >> w;

	pairll c, d;

	cin >> c.first >> c.second;
	cin >> d.first >> d.second;

	c.first--;
	c.second--;
	d.first--;
	d.second--;

	vecstr map(h);

	forll(i, 0, h)
	{
		cin >> map[i];
	}

	pair<pairll, bool> start = { c,false }, goal = { d,false };

	vec2ll memo;
	forll (i,0,map.size()) {
		vecll v(map[i].size(), -1);
		memo.push_back(v);
	}

	const pairll points[4] = {
		{ -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 },
	};

	queue<pair<pairll,bool>> data;
	data.push(start);

	queue<pair<pairll, bool>> warp;

	memo[c.first][c.second] = 0;

	while (!data.empty() || !warp.empty()) {
		flag = false;

		if (data.empty() && !warp.empty())
		{
			while (!warp.empty())
			{
				pair<pairll, bool> cur = warp.front();
				warp.pop();

				forll(i, -2 - min((ll)0, cur.first.first - 2), 3 - max((ll)0, cur.first.first + 2 - (h - 1)))
				{
					forll(j, -2 - min((ll)0, cur.first.second - 2), 3 - max((ll)0, cur.first.second + 2 - (w - 1)))
					{
						if (i == 0 && j == 0)
							continue;

						pairll next = { (cur.first.first + i), (cur.first.second + j) };

						if (isInFirld(map[0].size(), map.size(), next))
						{
							const char s = map[next.first][next.second];

							if (s == '.') {
								if (memo[next.first][next.second] == -1 || memo[next.first][next.second] > memo[cur.first.first][cur.first.second] + 1) {

									data.push({ next,false });

									memo[next.first][next.second] = memo[cur.first.first][cur.first.second] + 1;
								}
							}
						}
					}
				}
			}
		}
		else
		{
			pair<pairll, bool> cur = data.front();
			data.pop();

			for (const auto &point : points) {
				pairll next = { (cur.first.first + point.first), (cur.first.second + point.second) };

				if (isInFirld(map[0].size(), map.size(), next)) {

					const char s = map[next.first][next.second];

					if (s == '.') {
						if (memo[next.first][next.second] == -1 || memo[next.first][next.second] > memo[cur.first.first][cur.first.second]) {

							data.push({ next,false });

							memo[next.first][next.second] = memo[cur.first.first][cur.first.second];
						}
					}
					else if (s == '#' && !flag)
					{
						flag = true;
						cur.second = true;

						warp.push(cur);
					}
				}
			}
		}
	}

	ans = memo[d.first][d.second];

	cout << ans;
}

Measured memory_bytes: 13564