#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;

//****************************************************                
#define lp(var,start,end) for (ll var = start; var <end ; ++var)
#define rlp(var,start,end) for(ll var = start; var>=end ; var--)
#define pb push_back
#define mp make_pair
#define pf push_front
#define ff first
#define ss second
#define vll vector<ll>
#define pll pair<ll,ll> 
#define vpll vector<pll>
#define all(X) X.begin(),X.end()
#define endl "\n" //comment it for interactive questions
#define trace1(a) cerr << #a << ": " << a << endl;
#define trace2(a,b) cerr << #a << ": " << a << " " << #b << ": " << b << endl;
#define trace3(a,b,c) cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c << endl;
#define trace4(a,b,c,d) cerr << #a << ": " << a << " " << #b << ": " << b << " " << #c << ": " << c << #d << ": " << d << endl;
#define FAST_IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
        //*******************************************************
//Some Functions

const ll MOD = (ll)1e9+7; //change it for other mods

ll powmod(ll a,ll b)
{
   ll res = 1;
   while(b > 0) {
      if(b & 1) res = (res * a) % MOD;
      a = (a*a)%MOD;
      b = b >> 1; 
  }
  return res % MOD;
}

void solve(ll testnum)
{
    ll h,w;
    cin >> h >> w;
    ll ch, cw;
    cin >> ch >> cw;
    ch--, cw--;
    ll dh, dw;
    cin >> dh >> dw;
    dh--, dw--;
    string s[h];
    for(ll i=0; i<h; i++) {
    	cin >> s[i];
    }
    deque<pair<pll, ll>> dq;
    ll vis[h+1][w+1];
    // ll dist[h+1][w+1];
    for(ll i=0; i<h; i++) {
    	for(ll j=0; j<w; j++) {
    		if(s[i][j] == '#') {
    			vis[i][j] = 1;
    		} else {
    			vis[i][j] = 0;
    		}
    	}
    }
    // ll dist = 0;
    dq.push_back({{ch, cw}, 0});
    while(!dq.empty()) {
    	pll cur = dq.front().ff;
    	vis[cur.ff][cur.ss] = 1;
    	ll dist = dq.front().ss;
    	if(cur.ff == dh && cur.ss == dw) {
    		cout << dist << "\n";
    		return;
    	}
    	dq.pop_front();
    	if(cur.ff + 1 < h) {
    		if(vis[cur.ff+1][cur.ss] == 0) {
    			dq.push_front({{cur.ff + 1, cur.ss}, dist});
    			// vis[cur.ff+1][cur.ss] = 1;
    		}
    	}
    	if(cur.ff - 1 >= 0) {
    		if(vis[cur.ff-1][cur.ss] == 0) {
    			dq.push_front({{cur.ff - 1, cur.ss}, dist});
    			// vis[cur.ff-1][cur.ss] = 1;
    		}
    	}
    	if(cur.ss + 1 < w) {
    		if(vis[cur.ff][cur.ss+1] == 0) {
    			dq.push_front({{cur.ff, cur.ss + 1}, dist});
    			// vis[cur.ff][cur.ss+1] = 1;
    		}
    	}
    	if(cur.ss - 1 >= 0) {
    		if(vis[cur.ff][cur.ss-1] == 0) {
    			dq.push_front({{cur.ff, cur.ss-1}, dist});
    			// vis[cur.ff][cur.ss-1] = 1;
    		}
    	}
    	for(ll i = -2; i <= 2; i++) {
    		for(ll j = -2; j <= 2; j++) {
    			if(cur.ff + i >= 0 && cur.ff + i < h) {
    				if(cur.ss + j >= 0 && cur.ss + j < w) {
    					if(vis[cur.ff+i][cur.ss+j] == 0) {
    						dq.push_back({{cur.ff+i, cur.ss+j}, dist+1});
    						// vis[cur.ff+i][cur.ss+j] = 1;
    					}
    				}
    			}
    		}
    	}
    }
    cout << "-1\n";
}

int main()
{
    FAST_IO;
    #ifdef STIO 
      freopen("input.txt" , "r", stdin);
      freopen("output.txt" , "w", stdout);
    #endif
    ll t = 1;
    // cin >> t;
    for (ll i = 1; i <= t; i++) {
        // cout << "Case #" << i << ": ";
        solve(i);
    }
}

Measured memory_bytes: 21300