/************************************************************LET US BREAK BAD!****************************************************************/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ld long double
#define pb push_back
#define pll pair<ll, ll> 
#define vi vector<int>
#define vl vector<ll> 
#define vvl vector< vector<ll> > 
#define vvi vector< vector<int> >
#define vlp vector< pair<ll, ll> >
#define vllp vector<pair<ll, ll>, ll > 
#define mll map<ll, ll> 
#define rep(i,a)  for(ll i=0; i< a; i++)
#define rep1(i,a)   for(ll i = 1; i< a; i++)
#define foi(i, a, b)    for(ll i = a; i<b ; i++)
#define fod(i, a, b)    for(ll i = a; i>=b ; i--)
#define mp make_pair
#define fst first
#define sec second
#define ff first.first
#define fs first.second
#define pi pair<int, int>
#define ppi pair<pair<int, int>, int>
#define max3(a, b, c)   max(max(a, b), c)
#define min3(a, b, c)   min(min(a, b), c)
#define MAX 1000001
#define MOD 1000000007
// #define MOD 998244353
#define endl "\n"
#define INF (ll)1e18
#define s(v) v.size()
#define e(v) v.empty()
#define bscount(x) __builtin_popcountll(x)
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
using namespace __gnu_pbds;

ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);} 
void ingraph(vvl& graph, ll m){ll x, y;rep(i, m){cin>>x>>y;x--, y--;graph[x].pb(y);graph[y].pb(x);}}
ll modify(ll n){ll res = n;res%=MOD;res+=MOD;res%=MOD;return res;}
ll cc;
void DFS(ll i, ll j, vvl & v, vector<string> & ss){
    v[i][j] = cc;
    if(i>0 && v[i-1][j]==0 && ss[i-1][j]=='.'){
        DFS(i-1, j, v, ss);
    }
    if(j>0 && v[i][j-1]==0 && ss[i][j-1]=='.'){
        DFS(i, j-1, v, ss);
    }
    if(i<(int)ss.size()-1 && v[i+1][j]==0 && ss[i+1][j]=='.'){
        DFS(i+1, j, v, ss);
    }
    if(j<(int)ss[0].length()-1 && v[i][j+1]==0 && ss[i][j+1]=='.'){
        DFS(i, j+1, v, ss);
    }

}
int main(){
    //#ifndef ONLINE_JUDGE
    //    freopen("../input.txt", "r", stdin);
    //    freopen("../output.txt", "w", stdout);
    //#endif
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll t;
    // cin>>t;
    t = 1;
    while(t--){
        ll h, w;
        cin>>h>>w;
        ll ch, cw, dh, dw;
        cin>>ch>>cw>>dh>>dw;
        ch--;cw--;dh--;dw--;
        vector<string> ss(h);
        rep(i, h){
            cin>>ss[i];
        }
        cc = 0;
        vvl v(h, vl(w, 0));
        rep(i, h){
            rep(j, w){
                if(v[i][j]==0 && ss[i][j]=='.'){
                    cc++;
                    DFS(i, j, v, ss);
                }
            }
        }
        ll src = v[ch][cw];
        ll des = v[dh][dw];
        // if(src==des){
        //     cout<<0<<endl;
        //     continue;
        // }
        vvl adj(cc+5);
        map<pll, ll> m;
        rep1(i, cc+1){
            m[mp(i, i)] = 1;
        }
        rep(i, h){
            rep(j, w){
                if(v[i][j]==0) continue;
                foi(k, -2, 3){
                    foi(l, -2, 3){
                        if(i+k>=h || j+l>=w || i+k<0 || j+l<0) continue;
                        if(k==0 && l==0) continue;
                        if(v[i+k][j+l]==0) continue;
                        if(m[mp(v[i][j], v[i+k][j+l])]) continue;
                        if(m[mp(v[i+k][j+l], v[i][j])]) continue;
                        adj[v[i][j]].pb(v[i+k][j+l]);
                        adj[v[i+k][j+l]].pb(v[i][j]);
                        m[mp(v[i+k][j+l], v[i][j])] = 1;
                        m[mp(v[i][j], v[i+k][j+l])] = 1;
                    }
                }
            }
        }
        vl d(cc+5, -1);
        queue<ll> q;
        q.push(src);
        d[src] = 0;
        while(!q.empty()){
            ll u = q.front();
            q.pop();
            rep(i, (int)adj[u].size()){
                if(d[adj[u][i]]!=-1) continue;
                d[adj[u][i]] = d[u]+1;
                q.push(adj[u][i]);
            }
        }
        cout<<d[des]<<endl;

    }
return 0;
}


Measured memory_bytes: 54096