#include<cmath>
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
stack<int> st;
queue<int> qu;
queue<pair<int,int> > qu2;
priority_queue<int> pq;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define rep2(i,n) for(int i=1; i<=(int)(n); i++)
#define mins(x,y) x=min(x,y)
#define maxs(x,y) x=max(x,y)
#define ALL(a) a.begin(), a.end()
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i,s,n) for(int i = s; i < n; i++)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
deque<int> deq;
#define fi first
#define se second
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<int, int> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int visited[1000005];
vector<int> to[1000005];
vector<int> connect[1000005];
int color[1000005];
int parent[1005][1005];
int dist[1000005];
int w,h;

void dfs1(int u){
    color[u]=1;
    for(int v:connect[u]){
        if(color[v]==0){
            parent[v/w][v%w]=parent[u/w][u%w];
            dfs1(v);
        }
    }
    color[u]=2;
    return;
}

/*
int dfs(int now,int gl,int cnt){
    visited[now]=1;
    for(int v:to[now]){
        if(v==gl){
            return cnt+1;
        }
        if(visited[v]==0){
            return dfs(v,gl,cnt+1);
        }
    }
    return -1;
}
*/
int main(){
    cin >> h>> w;
    int ch,cw;
    cin >> ch>> cw;
    ch--;cw--;
    int dh,dw;
    cin >> dh >> dw;
    dh--;dw--;
    string s[h];
    rep(i,h){
        cin >> s[i];
    }
    rep(i,1000001){
        dist[i]=INF;
    }
    rep(i,h){
        rep(j,w){
            if(s[i][j]=='.'){
                parent[i][j]=i*w+j;    
            }else{
                parent[i][j]=INF;
            }
        }
    }
    
    rep(i,h){
        rep(j,w){
            if(s[i][j]=='.'){
                if(i>0&&s[i-1][j]=='.'){
                    connect[parent[i][j]].push_back(parent[i-1][j]);
                    connect[parent[i-1][j]].push_back(parent[i][j]);
                }
                if(j>0&&s[i][j-1]=='.'){
                    connect[parent[i][j]].push_back(parent[i][j-1]);
                    connect[parent[i][j-1]].push_back(parent[i][j]);
                }
                if(i<h-1&&s[i+1][j]=='.'){
                    connect[parent[i][j]].push_back(parent[i+1][j]);
                    connect[parent[i+1][j]].push_back(parent[i][j]);
                }
                if(j<w-1&&s[i][j+1]=='.'){
                    connect[parent[i][j]].push_back(parent[i][j+1]);
                    connect[parent[i][j+1]].push_back(parent[i][j]);
                }
            }
        }
    }
    
    
    rep(i,h){
        rep(j,w){
            dfs1(i*w+j);   
        }
    }
    /*
    cerr<<endl;
    rep(i,h){
        rep(j,w){
            dfs1(i*w+j);
            cerr<<parent[i][j]<<' ';
        }
        cerr<<endl;
    }
    */
    rep(i,h){
        rep(j,w){
            for(int l=-2;l<=2;l++){
                for(int r=-2;r<=2;r++){
                    if(i+l>=0&&i+l<=h-1&&j+r>=0&&j+r<=w-1){
                        if(s[i+l][j+r]=='.'&&s[i][j]=='.'&&parent[i+l][j+r]!=parent[i][j]){
                            to[parent[i+l][j+r]].push_back(parent[i][j]);
                            to[parent[i][j]].push_back(parent[i+l][j+r]);
                        }
                    }
                }
            }
        }
    }
    int st=parent[ch][cw];
    int gl=parent[dh][dw];
    //cerr<<st<<' '<<gl<<endl;
    if(st==gl){
        cout<<0<<endl;
        return 0;
    }
    dist[st]=0;
    qu.push(st);
    while(!qu.empty()){
        int u=qu.front();
        qu.pop();
        for(int v:to[u]){
            if(dist[v]>dist[u]+1){
                dist[v]=dist[u]+1;
                qu.push(v);
            }
        }
    }
    int ans=dist[gl];
    if(dist[gl]==INF){
        cout<<-1<<endl;
    }else{
        cout<<ans<<endl;
    }
    return 0;
}

Measured memory_bytes: 103044