#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                    \
	sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(c i) \
	{
sim > struct rge
{
	c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug
{
#ifndef LOCAL
	~debug()
	{
		cerr << endl;
	}
	eni(!=) cerr << boolalpha << i;
	ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d)
{
	ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d)
{
	*this << "[";
	for (auto it = d.b; it != d.e; ++it)
		*this << ", " + 2 * (it == d.b) << *it;
	ris << "]";
}
#else
	sim dor(const c &)
	{
		ris;
	}
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
template <typename T>
using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define in insert
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define sz(v) (int)(v.size())
#define all(x) x.begin(), x.end()
#define prec fixed << setprecision(12)
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define test  \
	int t;    \
	cin >> t; \
	while (t--)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef set<int> si;
typedef map<int, int> mii;
typedef vector<pii> vpi;

const int mxn = 1e3 + 5;
int h, w;
char board[mxn][mxn];

bool legal(int x, int y)
{
	if (x < 0 || y < 0 || x >= w || y >= h || board[x][y] == '#')
		return false;
	return true;
}

void solve()
{
	cin >> h >> w;
	int sx, sy;
	cin >> sy >> sx;
	int ey, ex;
	cin >> ey >> ex;
	sx--, sy--, ex--, ey--;
	deque<int> xs, ys;
	rep(y, 0, h - 1)
	{
		rep(x, 0, w - 1)
		{
			cin >> board[x][y];
		}
	}
	xs.push_back(sx);
	ys.push_back(sy);
	int dist[w][h];
	memset(dist, 1e9, sizeof dist);
	rep(i, 0, w - 1)
	{
		rep(j, 0, h - 1)
		{
			dist[i][j] = 1e9;
		}
	}
	dist[sx][sy] = 0;
	int dx[] = {-1, 1, 0, 0};
	int dy[] = {0, 0, -1, 1};
	while (!xs.empty())
	{
		int curX = xs.front(), curY = ys.front();
		xs.pop_front(), ys.pop_front();
		// cout << curX << " " << curY << "\n";
		rep(d, 0, 3)
		{
			int nx = curX + dx[d];
			int ny = curY + dy[d];
			if (!legal(nx, ny))
				continue;
			if (dist[nx][ny] > dist[curX][curY])
			{
				dist[nx][ny] = dist[curX][curY];
				xs.push_front(nx);
				ys.push_front(ny);
			}
		}
		for (int x = curX - 2; x <= curX + 2; x++)
		{
			for (int y = curY - 2; y <= curY + 2; y++)
			{
				if (!legal(x, y))
					continue;
				if (dist[x][y] > dist[curX][curY] + 1)
				{
					dist[x][y] = dist[curX][curY] + 1;
					xs.push_back(x);
					ys.push_back(y);
				}
			}
		}
	}
	dist[ex][ey] > 1e7 ? cout << "-1" : cout << dist[ex][ey];
}

int32_t main()
{
	ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	cin.tie(NULL);
	// test
	solve();
	return 0;
}


Measured memory_bytes: 12472