#include<bits/stdc++.h> 
using namespace std;
typedef long long ll;
template<typename T1,typename T2> bool chmin(T1 &a,T2 b){if(a<=b)return 0; a=b; return 1;}
template<typename T1,typename T2> bool chmax(T1 &a,T2 b){if(a>=b)return 0; a=b; return 1;}
int dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};

class UnionFind{
    public:
    //親の番号を格納する。親だった場合は-(その集合のサイズ)
    vector<int> parent;

    UnionFind(int N){
        parent = vector<int>(N,-1);
    }

    int root(int A){
        if(parent[A] < 0) return A;
        return parent[A]=root(parent[A]);
    }

    int size(int A){
        return -parent[root(A)];
    }

    bool unite(int A, int B) {
        A = root(A), B = root(B);
        if(A == B) return false; 

        if(size(A) < size(B)) swap(A,B);
        parent[A] += parent[B];
        parent[B] = A;
        return true;
    }

    bool same(int A, int B){
        return root(A)==root(B);
    } 
};

class Dijkstra {
public:
    struct edge { ll v, dist; };

    struct state {
        ll v, cost;
        bool operator>(const state s) const { return cost > s.cost; }
    };

    const ll INF = (1LL << 60);
    ll N;
    ll MOD = 1e9+7;
    vector< vector<edge> > E;

    Dijkstra(ll n): N(n), E(n) {}

    //有効グラフの時はこっち。u→vに距離dで結ぶ
    void add_directed_edge(ll u, ll v, ll d) {
        E[u].push_back((edge) { v, d });
    }

    //無向グラフの時はこっち。uとvを双方向に距離dで結ぶ
    void add_undirected_edge(ll u, ll v, ll d) {
        E[u].push_back((edge) { v, d });
        E[v].push_back((edge) { u, d });
    }

    //Sを始点として、他の頂点への最短経路を探す
    vector<long long> shortest_path(long long S) {
        vector<long long> dp(E.size(), INF);
        priority_queue<state, vector<state>, greater<state> > q;
        q.push((state) { S, 0 });
        while(!q.empty()) {
            long long v = q.top().v, cost = q.top().cost;
            q.pop();
            if(dp[v] <= cost) continue;
            dp[v] = cost;
            for(int i=0;i < E[v].size() ; i++) {
            long long nv = E[v][i].v, ncost = cost + E[v][i].dist;
            if(dp[nv] > ncost) q.push((state) { nv, ncost });
            }
        }
        return dp;
    }
};

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(20);

    int h,w,sh,sw,gh,gw;
    cin>>h>>w>>sh>>sw>>gh>>gw;
    string s[h];
    for(int i=0;i<h;i++)cin>>s[i];
    int a[h][w] = {};
    sh--,sw--;
    gh--,gw--;
    UnionFind uni(h*w);
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(s[i][j] == '#')continue;
            for(int k=0;k<4;k++){
                int x = i + dx[k], y = j + dy[k];
                if((unsigned int)(x) < h && (unsigned int)(y) < w && s[x][y] != '#'){
                    uni.unite(x*w + y, i*w + j);
                }
            }
        }
    }

    int now = 0;
    vector<int> v;
    map<int,int> mp;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(s[i][j] == '.' && uni.root(i*w+j) == i*w+j){
                v.push_back(i*w+j);
                mp[i*w + j] = now;
                now++;
            }
        }
    }
    Dijkstra dij(now+1);
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            if(s[i][j] == '#')continue;
            for(int x=max(i-2,0);x<min(h,i+3);x++){
                for(int y=max(j-2,0);y<min(w,j+3);y++){
                    if(s[x][y] == '.' && uni.same(x*w+y, i*w+j)==false){
                        dij.add_undirected_edge(mp[uni.root(x*w+y)],mp[uni.root(i*w+j)],1);
                    }
                }
            }
        }
    }
    ll ans = dij.shortest_path(mp[uni.root(sh*w + sw)])[mp[uni.root(gh*w + gw)]];
    if(ans > 1e9)ans = -1;
    cout << ans << endl;

    // cerr << uni.same(sh*w+sw, gh*w+gw) << endl;

}   

Measured memory_bytes: 103572