#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define REP(a,b) for(int a=0;a<(b);++a)
#define REP1(i,n) for(int i=1;i<=(n);++i)
#define debug(x) cerr<<#x<<": "<<x<<'\n'
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isPossible(x) printf("%s\n",(x) ? "Possible" : "Impossible")
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
// #define INF (1<<29)

const long long INF = 1LL<<50;

#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define pie 3.14159265358979323846
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;
template<class T=int>
T in(){T x;cin>>x;return (x);}
template<class T>
void print(T& x){cout<<x<<'\n';}

const int MOD =(int)1e9+7;
// const int mod =(int)998244353;
const int mod =(int)1e9+7;

const int MAX =510000;
ll fac[MAX],finv[MAX],inv[MAX];
void COMint(){
    fac[0]=fac[1]=1;
    finv[0]=finv[1]=1;
    inv[1]=1;
    for(int i=2;i<MAX;i++){
        fac[i]=fac[i-1]*i%MOD;
        inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
        finv[i]=finv[i-1]*inv[i]%MOD;
    }
}
ll COM(int n,int k){
    if(n<k) return 0;
    if(n<0||k<0)return 0;
    return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
ll gcd(ll a,ll b){
    if(a<0)a=-a;
    if(b<0)b=-b;
    if(b==0)return a;
    if(a>b){
        swap(a,b);
    }
    return gcd(a,b%a);
}
ll lcm(ll a,ll b){
  if(a<0)a=-a;
  if(b<0)b=-b;
    ll g;g=gcd(a,b);
    return b/g*a;
}
bool compare_by_b(pair<int, int> a, pair<int, int> b) {
    if(a.second != b.second){
        return a.second < b.second;
    }else{
        return a.first < b.first;
    }
}
bool compare_by_a(pair<int, int> a, pair<int, int> b) {
    if(a.first != b.first){
        return a.first < b.first;
    }else{
        return a.second < b.second;
    }
}
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main() {
    ios::sync_with_stdio(false);
    int H,W;
    cin>>H>>W;
    int ch,dh,cw,dw;
    cin>>ch>>cw>>dh>>dw;
    ch--;cw--;dh--;dw--;
    vector<string>S(H);
    REP(i,H)cin>>S[i];
    vector<vector<int>>type(H,vi(W,-1));
    //歩ける空間のかたまりがいくつあるかを調べる
    int t=0;
    REP(ih,H){
        REP(iw,W){
            if(type[ih][iw]!=-1)continue;
            if(S[ih][iw]=='#')continue;
            type[ih][iw]=t;
            t++;
            queue<P> q;
            q.push(mp(ih,iw));
            while(!q.empty()){
                int nh=q.front().first,nw=q.front().second;
                q.pop();
                if(nh>0){
                    if(S[nh-1][nw]=='.'&&type[nh-1][nw]==-1){
                        q.push(mp(nh-1,nw));
                        type[nh-1][nw]=type[nh][nw];
                    }
                }
                if(nh<H-1){
                    if(S[nh+1][nw]=='.'&&type[nh+1][nw]==-1){
                        q.push(mp(nh+1,nw));
                        type[nh+1][nw]=type[nh][nw];
                    }
                }
                if(nw>0){
                    if(S[nh][nw-1]=='.'&&type[nh][nw-1]==-1){
                        q.push(mp(nh,nw-1));
                        type[nh][nw-1]=type[nh][nw];
                    }
                }
                if(nw<W-1){
                    if(S[nh][nw+1]=='.'&&type[nh][nw+1]==-1){
                        q.push(mp(nh,nw+1));
                        type[nh][nw+1]=type[nh][nw];
                    }
                }
            }
        }
    }
    //どのかたまりどうしがワープで移動できるのかを調べる
    vector<map<int,int>>to(t);
    REP(ih,H){
        REP(iw,W){
            if(type[ih][iw]<0)continue;
            int it=type[ih][iw];
            int hs,nh,ws,ns;
            hs=max(ih-2,0);
            ws=max(iw-2,0);
            for(int jh=hs;jh<min(ih+3,H);jh++){
                for(int jw=ws;jw<min(iw+3,W);jw++){
                    if(type[jh][jw]<0)continue;
                    if(type[jh][jw]==it)continue;
                    int nt=type[jh][jw];
                    to[it][nt]++;
                    to[nt][it]++;
                }
            }
        }
    }
    //スタートとゴールの塊が何回のワープで移動可能かを調べる
    int st,gt;
    st=type[ch][cw];
    gt=type[dh][dw];
    vector<int>dist(t,-1);
    dist[st]=0;
    queue<int> qu;
    qu.push(st);
    while(!qu.empty()){
        int nt=qu.front();
        qu.pop();
        for(auto i=to[nt].begin();i!=to[nt].end();i++){
            int a=i->first;
            if(dist[a]==-1){
                qu.push(a);
                dist[a]=dist[nt]+1;
            }
        }
    }
    cout<<dist[gt]<<endl;
    return 0;
} 
 

Measured memory_bytes: 32980