#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //GCC5.4.1で配列の範囲外アクセスをエラーにする

//cmdで g++ test.cpp とするとコンパイルできる
//制限1秒なら10^6は余裕 10^7は多分間に合う 10^8はよほど単純でないと厳しい
// https://github.com/atcoder/live_library
// cppdbg: test.exeのterminalに標準入力を打てる

#define ALL(x) x.begin(),x.end()
#define get_tp(t,i) get<i>(t)
#define len(x) (ll)(x.size())
#define ub(a,x) (upper_bound(ALL(a),x)-a.begin())
#define lb(a,x) (lower_bound(ALL(a),x)-a.begin())
typedef long long ll; //long long
typedef pair<int,int> pii; //pair<int,int>
typedef pair<long long,long long> pllll; //pair<ll,ll>
typedef vector<int> vi; //vector<int>
typedef vector<vector<int>> vvi; //vector<vector<int>>
typedef vector<ll> vll; //vector<long long>
typedef vector<vector<ll>> vvll; //vector<vector<long long>>
typedef priority_queue<ll, vector<ll>, greater<ll> > gpq; //逆順priority_queue
typedef priority_queue<ll> pq;

//const int MOD=998244353;
const int MOD=1000000007; //10^9+7
const long double PI=3.1415926535897932;

int main(){
    ll H,W;cin>>H>>W;
    ll Ch,Cw,Dh,Dw;cin>>Ch>>Cw>>Dh>>Dw;
    Ch--;Cw--;Dh--;Dw--;
    const ll MAX=99999999;
    const ll WALL=MAX+1;
    vvll S(H,vll(W,WALL)); // MAX+1:かべ MAX:未探索 >=0:探索済
    for(int h=0;h<H;h++){
        string Sh;cin>>Sh;
        for(int w=0;w<W;w++){
            if(Sh[w]=='.')S[h][w]=MAX;
        }
    }

    queue<pii> Q;
    Q.push(make_pair(Ch,Cw));
    S[Ch][Cw]=0;
    while(!Q.empty()){
        queue<pii> Q2;
        ll qfh,qfw;
        qfh=Q.front().first;qfw=Q.front().second;
        Q.pop();
        ll score=S[qfh][qfw];
        Q2.push(make_pair(qfh,qfw));
        while(!Q2.empty()){
            ll q2fh,q2fw;
            q2fh=Q2.front().first;q2fw=Q2.front().second;
            Q2.pop();
            if(0<q2fh){
                if(S[q2fh-1][q2fw]!=WALL && score<S[q2fh-1][q2fw]){
                    S[q2fh-1][q2fw]=score;
                    Q2.push(make_pair(q2fh-1,q2fw));
                }
            }
            if(0<q2fw){
                if(S[q2fh][q2fw-1]!=WALL && score<S[q2fh][q2fw-1]){
                    S[q2fh][q2fw-1]=score;
                    Q2.push(make_pair(q2fh,q2fw-1));
                }
            }
            if(q2fh<H-1){
                if(S[q2fh+1][q2fw]!=WALL && score<S[q2fh+1][q2fw]){
                    S[q2fh+1][q2fw]=score;
                    Q2.push(make_pair(q2fh+1,q2fw));
                }
            }
            if(q2fw<W-1){
                if(S[q2fh][q2fw+1]!=WALL && score<S[q2fh][q2fw+1]){
                    S[q2fh][q2fw+1]=score;
                    Q2.push(make_pair(q2fh,q2fw+1));
                }
            }
            for(ll h=max(0LL,q2fh-2);h<=min(H-1,q2fh+2);h++){
                for(ll w=max(0LL,q2fw-2);w<=min(W-1,q2fw+2);w++){
                    if(S[h][w]!=WALL && score+1<S[h][w]){
                        S[h][w]=score+1;
                        Q.push(make_pair(h,w));
                    }
                }
            }
        }
    }
    

    // for(int h=0;h<H;h++){
    //     for(int w=0;w<W;w++){
    //         cout<<S[h][w]<<" ";
    //     }
    //     cout<<endl;
    // }
    ll ans=S[Dh][Dw];
    if(ans==MAX)ans=-1;
    cout<<ans<<endl;
}


Measured memory_bytes: 11584