#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef long double ld;

#define llset tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define en '\n'
#define S second
#define F first
#define pll pair<ll,ll>
#define PB push_back

const ll N = 1e6+7;
ll mod = 1e9+7;
#define calncr for(int i=1  ;i<=1e6;i++) fac[i] = (fac[i-1]*i)%mod;
ll fac[N] = {0};

ll gen_base(const ll before, const ll after) {
    auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
    std::mt19937 mt_rand(seed);
    int base = std::uniform_int_distribution<ll>(before+1, after)(mt_rand);
    return base % 2 == 0 ? base-1 : base;
}

ll pow(ll a,ll b)
{
    if(!b)
        return 1;
    ll p = pow(a,b/2);
    if(b%2)
        return (a*((p*p)%mod))%mod;
    else
        return (p*p)%mod;
}

ll modInv(ll a)
{
    //cout<<a<<" "<<mod<<en;
    return pow(a%mod,mod-2);
}

ll nCr(ll n,ll r)
{
    return (fac[n]*modInv((fac[r]*fac[n-r])%mod))%mod;
}

ll hashf(ll r,ll x,ll mod)
{
    return (x+r)%mod;
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    fac[0] = 1;
    calncr;
    ll h,w;
    cin>>h>>w;
    ll ch,cw,dh,dw;
    cin>>ch>>cw>>dh>>dw;
    ch--;cw--;dh--;dw--;
    string s[h];
    for(ll i=0;i<h;i++){
        cin>>s[i];
    }
    ll visit[h][w],dist[h][w];
    memset(visit,0,sizeof(visit));
    for(ll i=0;i<h;i++)
        for(ll j=0;j<w;j++)
        dist[i][j] = LLONG_MAX;
    dist[ch][cw] = 0;
    visit[ch][cw]++;
    deque<pll> q;
    q.push_front({ch,cw});
    while(!q.empty()){
        ll a=q.front().F, b = q.front().S;
        q.pop_front();
        if(a && s[a-1][b]=='.'){
            if(dist[a-1][b]>dist[a][b]){
                dist[a-1][b] = dist[a][b];
                q.push_front({a-1,b});
            }
        }
        if(b && s[a][b-1]=='.'){
            if(dist[a][b-1]>dist[a][b]){
                dist[a][b-1] = dist[a][b];
                q.push_front({a,b-1});
            }
        }
        if(a<(h-1) && s[a+1][b]=='.'){
            if(dist[a+1][b]>dist[a][b]){
                dist[a+1][b] = dist[a][b];
                q.push_front({a+1,b});
            }
        }
        if(b<(w-1) && s[a][b+1]=='.'){
            if(dist[a][b+1]>dist[a][b]){
                dist[a][b+1] = dist[a][b];
                q.push_front({a,b+1});
            }
        }
        ll ar[5] = {-2,-1,0,1,2},br[5] = {-2,-1,0,1,2};
        for(ll j=0;j<5;j++){
            for(ll k=0;k<5;k++){
                if(ar[j]==0 && br[k]==0)
                   continue;
                ll x = (ar[j]+a), y = (br[k]+b);
                if(x>=0 && x<(h) && y>=0 && y<(w) && s[x][y]=='.'){

                //cout<<a<<" "<<b<<" "<<x<<" "<<y<<" "<<dist[x][y]<<" "<<dist[a][b]<<en;
                    if(dist[x][y]>(dist[a][b]+1)){
                        dist[x][y] = dist[a][b]+1;
                        q.push_back({x,y});
                    }
                }
            }
        }
    }
    if(dist[dh][dw] == LLONG_MAX)
        cout<<-1<<en;
    else
        cout<<dist[dh][dw]<<en;
}


Measured memory_bytes: 28184