#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <utility>
#include <set>
#include <stack>
#include <climits>
#include <cmath>
#include <iomanip>
#include <unordered_map>
#include <map>
#include <boost/multiprecision/cpp_int.hpp>
#include <deque>

#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define rep1(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define reps(i,s,n) for(int i=s, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define MEMSET(v, h) memset((v), h, sizeof(v))
#define INF 10000000
typedef long long ll;
typedef long double ld;
const char sp = ' ';

using namespace boost::multiprecision;
using namespace std;
ll H, W, Ch, Cw, Dh, Dw;
string S[1020];
struct node{
    ll depth;
    vector<pair<ll, ll> > links;
    node(ll GRAPH_MAX){
        depth=GRAPH_MAX;
    }
};

struct graph{
    vector<node> NODES;
    ll MAXVAL;
    deque<ll> bfs01_;
    vector<ll> searching_;
    vector<ll> searched_;
    graph(ll size, ll maxweight){
        rep(i, size){
            MAXVAL=size*maxweight+1;
            node NEWNODE(MAXVAL);
            NODES.push_back(NEWNODE);
        }
    }
    //双方向リンク
    void connect(ll a, ll b, ll weight){
        add_link(a, b, weight);
        add_link(b, a, weight);
    }
    //単方向リンク
    void add_link(ll from, ll to, ll weight){
        NODES[from].links.push_back({to, weight});
    }
    //01-BFS
    void bfs01(ll base){
        bfs01_.push_back(base);
        vector<ll> dist(NODES.size(), MAXVAL);
        
                dist[base]=0;
        while(bfs01_.size()){
            int i=bfs01_.front(); bfs01_.pop_front();
            /*
            rep(i, H){
                rep(j, W){
                    if(S[i][j]=='.') cout<<dist[i*W+j]<<"\t";
                    else cout<<S[i][j]<<"\t";
                }
                cout<<endl;
            }
            rep(i, bfs01_.size()) cout<<bfs01_[i]<<sp;
            cout<<endl;
            cout<<endl;
            */
            
            for(auto [j, c] : NODES[i].links){
                int d = dist[i] + c;
                if(d < dist[j]){
                    dist[j] = d;
                    if(c){
                        bfs01_.push_back(j);
                    }else{
                        bfs01_.push_front(j);
                    }
                }
            }
            /*
            rep(i, H){
                rep(j, W){
                    if(S[i][j]=='.') cout<<dist[i*W+j]<<"\t";
                    else cout<<S[i][j]<<"\t";
                }
                cout<<endl;
            }
            rep(i, bfs01_.size()) cout<<bfs01_[i]<<sp;
            cout<<endl;
            cout<<endl;
            */
        }
        rep(i, dist.size()) searched_.push_back(dist[i]);
    }
    ll bfs01_result(ll index){
        if(searched_[index]==MAXVAL) return -1;
        return searched_[index];
    }
};

int main(void){
    
    cin>>H>>W>>Ch>>Cw>>Dh>>Dw;
    
    rep(i, H) cin>>S[i];
    graph g(H*W, 1);
    rep(i, H) rep(j, W){
        if(S[i][j]=='#') continue;
        if(i!=0) if(S[i-1][j]=='.') g.add_link(i*W+j, (i-1)*W+j, 0);
        if(j!=0) if(S[i][j-1]=='.') g.add_link(i*W+j, i*W+j-1, 0);
        if(i!=H-1) if(S[i+1][j]=='.') g.add_link(i*W+j, (i+1)*W+j, 0);
        if(j!=W-1) if(S[i][j+1]=='.') g.add_link(i*W+j, i*W+j+1, 0);
        reps(k, -2, 3) reps(l, -2, 3){
            if(k*k+l*l<=1) continue;
            if(i+k>=0 && i+k<H && j+l>=0 && j+l<=W){
                if(S[i+k][j+l]=='.') g.add_link(i*W+j, (i+k)*W+j+l, 1);
            }
        }
    }
    /*rep(i, H){
        rep(j, W){
            if(S[i][j]=='.') cout<<to_string(i*W+j)<<"\t";
            else cout<<S[i][j]<<"\t";
        }
        cout<<endl;
    }
    rep(i, g.NODES.size()){
        cout<<i<<" :"<<endl;
        rep(j, g.NODES[i].links.size()){
            cout<<"("<<g.NODES[i].links[j].first<<", "<<g.NODES[i].links[j].second<<")"<<sp;
        }
        cout<<endl;
    }*/
    g.bfs01((Ch-1)*W+(Cw-1));
    cout<<g.bfs01_result((Dh-1)*W+(Dw-1))<<endl;
}


Measured memory_bytes: 191736