// in case of multiple test cases and N=1e5 don't intialize arrays as global except for graph vector-array
// always check whether or not you are doing mod of a negative number
// always use 1LL instead of 1
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
 
using namespace __gnu_pbds;
using namespace std;

#define double      	long double
//#define int         	long long
#define pb          	push_back
#define pii         	pair<int,int>
#define vi          	vector<int>
#define vii         	vector<pii>
#define mi          	map<int,int>
#define mii         	map<pii,int>
#define all(a)      	(a).begin(),(a).end()
#define sz(x)       	(int)x.size()
// just comment the line below in case of interactive problems
#define endl        	"\n"
#define repp(i,a,b) 	for(int i=a;i<b;i++)
#define rep(i,a,b) 		for(int i=a;i<=b;i++)
#define brep(i,a,b)     for(int i=a;i>=b;i--)
#define deb1(x)      	cout << #x << "=" << x << endl
#define deb2(x, y)  	cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)  	cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define trace(v) 		for(auto it=v.begin();it!=v.end();it++)cout<<*it<<" ";cout<<endl;
#define tracearr(a,l,r) for(int iii=l;iii<=r;iii++)cout << a[iii] << " ";cout << endl;
#define PI          	3.1415926535897932384626
#define F 				first
#define S 				second
#define clr(x,y) 		memset(x, y, sizeof(x))
#define IOS         	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);


typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are strictly smaller than x


const int N=1e3 + 5;
const int MOD=1e9 + 7;

int n,m;
char a[N][N];
int ans[N][N];
int sx,sy;
int ex,ey;

const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};

bool ok(int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= m && a[x][y]=='.';  }

int32_t main()
{
	IOS;
	int T=1;
	//cin >> T;
	// int numOfSetBits = __builtin_popcountll(n);
	std::cout << std::fixed << std::setprecision(12);
	while(T--)
	{
		cin >> n >> m;
		cin >> sx >> sy >> ex >> ey;
		deque<pair<int,int>> q;
		rep(i,1,n)
		{
			rep(j,1,m)
				ans[i][j] = 1e9; 
		}
		rep(i,1,n)
		{
			rep(j,1,m)
				cin >> a[i][j];
		}
		q.push_front({sx,sy});
		ans[sx][sy] = 0;
		while(!q.empty())
		{
			pii p = q.front();
			q.pop_front();
			int x = p.F;
			int y = p.S;
			int cost = ans[x][y];
			for(int k=0;k<4;k++)
            {
                int newX = x + dx[k];
                int newY = y + dy[k];
                if(ok(newX,newY)==1 && ans[newX][newY]>cost)
                {
                    ans[newX][newY] = cost;
                    q.push_front({newX,newY});
                }
            }
            int lx = max(x-2,1);
            int rx = min(x+2,n);
            int ly = max(y-2,1);
            int ry = min(y+2,m);
            for(int i=lx;i<=rx;i++)
            {
            	for(int j=ly;j<=ry;j++)
            	{
            		if(ok(i,j)==1 && ans[i][j]>cost+1)
            		{
            			ans[i][j] = cost+1;
						q.push_back({i,j});
					}
				}
			}
		}
		if(ans[ex][ey]==1e9)
			cout << "-1";
		else
			cout << ans[ex][ey];
	}
}


Measured memory_bytes: 8612