#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 fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define PSET(x,y) fixed<<setprecision(y)<<x
#define pb push_back
#define pii pair<int,int>
#define pf push_front
#define mp make_pair
#define vi vector<int>
#define vip vector<pair<int,int> >
#define ff first
#define ss second
#define int long long
#define mod 1000000007
#define BIG 998244353
#define s(t) scanf("%lld",&t)
#define p(t) printf("%lld\n",t)
#define mii map<int,int>
#define MSET(table,i) memset(table, i, sizeof(table))
#define endl '\n'
#define tc int t;cin>>t;while(t--)
#define pi  3.1415926358
#define bits(x) __builtin_popcountll(x)
#define minimum(a,n) min_element(a,a+n)-a
#define maximum(a,n) max_element(a,a+n)-a
#define pqbig priority_queue<int>
#define pqsmall priority_queue<pii,vector<pii>,greater<pii> >
#define all(v) v.begin(),v.end()
#define sitr set<int>:: iterator
#define spitr set<pii>:: iterator
#define mitr map<int,int>:: iterator

int toint(const string &s) {stringstream ss; ss << s; int x; ss >> x; return x;}
string tostring ( int number ) {stringstream ss; ss << number; return ss.str();}


typedef tree<pii, null_type, less<pii>, rb_tree_tag,
        tree_order_statistics_node_update>
        new_data_set;


#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl

const int N = 1e3 + 5;
int h, w;
int ch, cw, dh, dw;
char arr[N][N];
int dist[N][N];
int dx[] = { -1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool check(int x, int y)
{
	//trace2(x,y);
	if (x >= 1 and x <= h and y >= 1 and y <= w and arr[x][y] != '#')
		return true;
	//trace2(x,y);
	return false;
}

int shortestPath(int x, int y)
{
	set< pair<int , pair<int, int> > > setds;
	setds.insert(make_pair(0, mp(x, y)));
	for (int i = 0; i < N; i++)
		for (int j = 0; j < N; j++)
			dist[i][j] = 1e18;

	dist[x][y] = 0;


	while (!setds.empty())
	{
		pair<int, pair<int, int> > tmp = *(setds.begin());
		setds.erase(setds.begin());

		int cur_x = tmp.second.first;
		int cur_y = tmp.second.second;

		//trace3(cur_x, cur_y, tmp.first);

		for (int i = 0; i < 4; ++i)
		{
			int new_x = cur_x + dx[i];
			int new_y = cur_y + dy[i];

			//trace2(new_x, new_y);
			//trace2(dist[new_x][new_y], dist[cur_x][cur_y]);

			if (check(new_x, new_y) and  (dist[new_x][new_y] > dist[cur_x][cur_y]))
			{
				//trace2(new_x, new_y);
				if (dist[new_x][new_y] != 1e18)
					setds.erase(setds.find(make_pair(dist[new_x][new_y], mp(new_x, new_y))));

				dist[new_x][new_y] = dist[cur_x][cur_y];
				setds.insert(make_pair(dist[new_x][new_y], mp(new_x, new_y)));
			}
		}


		for (int k = max(cur_x - 2, 1ll); k <= min(cur_x + 2, h); k++)
		{
			for (int l = max(cur_y - 2, 1ll); l <= min(cur_y + 2, w); l++)
			{
				if (k == cur_x and l == cur_y)
					continue;
				int new_x = k;
				int new_y = l;
				if (check(new_x, new_y) and  dist[new_x][new_y] > dist[cur_x][cur_y] + 1)
				{
					if (dist[new_x][new_y] != 1e18)
						setds.erase(setds.find(make_pair(dist[new_x][new_y], mp(new_x, new_y))));

					dist[new_x][new_y] = dist[cur_x][cur_y] + 1;
					setds.insert(make_pair(dist[new_x][new_y], mp(new_x, new_y)));
				}
			}
		}

	}

	if (dist[dh][dw] == 1e18)
		return -1;
	return dist[dh][dw];
}

int32_t main()
{
	fast;
	cin >> h >> w >> ch >> cw >> dh >> dw;
	for (int i = 1; i <= h; i++)
		for (int j = 1; j <= w; j++)
			cin >> arr[i][j];

	cout << shortestPath(ch, cw);
}


Measured memory_bytes: 12688