#include <bits/stdc++.h>
using namespace std;
 
#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
 
template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
 
 
lli d[1100][1100];
 
 
#define MAX_V 2000000
struct edge{
    lli to;
    lli cost;
};
bool operator< (const edge &l, const edge &r){
    return l.cost < r.cost;
}
bool operator> (const edge &l, const edge &r){
    return l.cost > r.cost;
}
 
lli h,w;
lli n;
lli pointToNum(lli nowI,lli nowJ){
    return nowI*w+nowJ;
}
 
vector<edge> G[MAX_V];
vector<lli> dijkstra(lli s){
    deque<edge> que;
    vector<lli> d(n+2,INF);
    //fill(d,d+n+2,INF);
    d[s]=0;
    que.push_back({s,0});
 
    while(!que.empty())
    {
        edge p = que.front();
        que.pop_front();
        lli v = p.to;
        if(d[v] < p.cost)continue;
 
        for(lli i=0;i<G[v].size();i++){
            edge e = G[v][i];
            if(d[e.to] > d[v]+e.cost){
                d[e.to] = d[v]+e.cost;
                if(e.cost == 0)que.push_front({e.to,d[e.to]});
                else que.push_back({e.to,d[e.to]});
            }
        }
    }
    return d;
}
 
 
int main(){
 
    REP(i,0,1100)REP(j,0,1100){
        d[i][j]=1;
    }
 
    cin>>h>>w;
    n = h*w;
    lli ch,cw;
    cin>>ch>>cw;
    ch--,cw--;
 
    lli dh,dw;
    cin>>dh>>dw;
    dh--,dw--;
    REP(i,0,h){
        REP(j,0,w){
            char c;
            cin>>c;
            if(c=='#')d[i][j]=1;
            else d[i][j]=0;
        }
    }
 
    REP(i,0,h){
        REP(j,0,w){
            if(d[i][j]==1)continue;
            lli nowNum = pointToNum(i,j);
            if(nowNum < 0 || nowNum >= n){
                cout<<"test"<<endl;
                return 0;
            }
            lli di[4]={-1,0,1,0};
            lli dj[4]={0,1,0,-1};
            REP(k,0,4){
                lli ni = i+di[k];
                lli nj = j+dj[k];
                if(ni<0 || ni>=h || nj<0 || nj>=w)continue;
                if(d[ni][nj]==1)continue;
                lli nNum = pointToNum(ni,nj);            
                if(nNum < 0 || nNum >= n){
                    cout<<"test"<<endl;
                    return 0;
                }
                G[nowNum].push_back({nNum,0});
            }
 
            REP(ni,i-2,i+3){
                REP(nj,j-2,j+3){
                    if(ni<0 || ni>=h || nj<0 || nj>=w)continue;
                    if(d[ni][nj]==1)continue;
                    if(ni==i && nj==j)continue;
                    lli nNum = pointToNum(ni,nj);                
                    if(nNum < 0 || nNum >= n){
                        cout<<"test"<<endl;
                        return 0;
                    }
                    G[nowNum].push_back({nNum,1});
                }
            }
        }
    }
 
    auto ddd = dijkstra(pointToNum(ch,cw));
    lli ans = ddd[pointToNum(dh,dw)];
    if(ans>=INF)cout<<-1<<endl;
    else cout<<ans<<endl;
 
    return 0;
}

Measured memory_bytes: 222928