//          ★★ ASHWINI SHARMA ★★ 
#include <bits/stdc++.h>
using namespace std;
 
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fr(i,a,b) for (int i = (a), _b = (b); i <= _b; i++)
#define frr(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define rep(i,n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i,n) for (long long i = (n) - 1; i >= 0; i--)
#define foreach(it,ar) for ( typeof(ar.begin()) it = ar.begin(); it != ar.end(); it++ )
#define ull unsigned long long int
#define ll long long int
#define ui unsigned int
#define pb push_back
 
#define PI 3.1415926535897932385
#define INF 1e7
#define EPS 1e-7
#define MAXN 1000000005
#define MOD 1000000007 
                              //          ★★ ASHWINI SHARMA ★★      
ll gcd(ll a, ll b) 
{ 
    if (a == 0) 
        return b; 
    return gcd(b % a, a); 
} 
ll exp(ll x, ll y, ll p)
{
    ll res = 1;   
    x = x % p;  
    while (y > 0)
    {
        if (y & 1)    
            res = (res*x) % p;
        y = y>>1;     
        x = (x*x) % p; 
    }
    return res;
}


int node[1001][1001];
vector<pair<int, int>> adj[1000005];


int main() {
    fastio;
    int h, w;
    cin >> h >> w;
    int ch, dh, cw, dw;
    cin >> ch >> cw >> dh >> dw;
    int cnt = 0;
    fr(i, 1, h)
    {
        fr(j, 1, w)
        {
            char s;
            cin >> s;
            if(s == '.')
                node[i][j] = ++cnt;
            else
                node[i][j] = 0;
            
        }
    }
    fr(i, 1, h)
    {
        fr(j, 1, w)
        {
            if(node[i][j])
            {
                if(i-1 >= 1 && node[i-1][j])
                {
                    adj[node[i][j]].pb(make_pair(node[i-1][j], 0));
                }
                if(i+1 <= h && node[i+1][j])
                {
                    adj[node[i][j]].pb(make_pair(node[i+1][j], 0));
                }
                if(j-1 >= 1 && node[i][j-1])
                {
                    adj[node[i][j]].pb(make_pair(node[i][j-1], 0));
                }
                if(j+1 <= w && node[i][j+1])
                {
                    adj[node[i][j]].pb(make_pair(node[i][j+1], 0));
                }


                for(int k=max(1, i-2);k<=min(h, i+2);k++)
                {
                    for(int l=max(1, j-2);l<=min(w, j+2);l++)
                    {
                        int x = abs(i-k) + abs(j-l);
                        if(x > 1)
                        {
                            if(node[k][l])
                                adj[node[i][j]].pb(make_pair(node[k][l], 1));
                        }
                    }
                }
            }
        }
    }
    vector<int> d(cnt+1, INF);
    d[node[ch][cw]] = 0;
    deque<int> q;
    q.push_front(node[ch][cw]);
    while(!q.empty())
    {
        int v = q.front();
        q.pop_front();
        for(auto edge: adj[v])
        {
            int to = edge.first;
            int wt = edge.second;
            if(d[v] + wt < d[to])
            {
                d[to] = d[v] + wt;
                if(wt == 1)
                    q.push_back(to);
                else
                    q.push_front(to);
            }
        }
    }
    if(d[node[dh][dw]] == INF)
        cout << -1;
    else
        cout << d[node[dh][dw]];
    
    


    return 0; 
}

Measured memory_bytes: 101980