#include <bits/stdc++.h>

#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pi 3.141592653
#define oo 1000000007
#define loo 10000000000000007

#define prime 998244353 
#define mod 998244353 

ll powmod(ll a,ll b) {ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

#define W(x) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << "\n";
using namespace std;


int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int h, w; cin >> h >> w;
    int ch, cw, dh, dw; cin >> ch >> cw >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    char mat[h][w];
    int visi[h][w];
    for (int i = 0; i < h; ++i){
        for (int j = 0; j < w; ++j){
            cin >> mat[i][j];
            visi[i][j] = 0;
        }
    }
    int at = 1;
    map<int, vector<ii>> p;
    for (int a = 0; a < h; ++a){
        for (int b = 0; b < w; ++b){
            if(mat[a][b] == '.' && !visi[a][b]){
                p[at].pb({a, b});
                visi[a][b] = at;
                queue<ii> q;
                q.push({a, b});
                while(!q.empty()){
                    int i = q.front().ff;
                    int j = q.front().ss;
                    if(i < h-1 && mat[i+1][j] == '.' && !visi[i+1][j]){
                        p[at].pb({i+1, j});
                        visi[i+1][j] = at;
                        q.push({i+1, j});
                    }
                    if(i && mat[i-1][j] == '.' && !visi[i-1][j]){
                        p[at].pb({i-1, j});
                        visi[i-1][j] = at;
                        q.push({i-1, j});
                    }
                    if(j < w-1 && mat[i][j+1] == '.' && !visi[i][j+1]){
                        p[at].pb({i, j+1});
                        visi[i][j+1] = at;
                        q.push({i, j+1});
                    }
                    if(j && mat[i][j-1] == '.' && !visi[i][j-1]){
                        p[at].pb({i, j-1});
                        visi[i][j-1] = at;
                        q.push({i, j-1});
                    }
                    q.pop();
                }
                at++;
            }
        }
    }
    set<int> gr[at];
    for (int a = 1; a < at; ++a){
        for(auto l : p[a]){
            for (int i = max(l.ff-2, 0); i <= min(l.ff+2, h-1); ++i){
                for (int j = max(l.ss-2, 0); j <= min(l.ss+2, w-1); ++j){
                    if(mat[i][j] == '.' && visi[i][j] != a){
                        gr[a].insert(visi[i][j]);
                        gr[visi[i][j]].insert(a);
                    }
                }
            }
        }
    }
    queue<ii> q;
    set<int> yo;
    q.push({visi[ch][cw], 0});
    yo.insert(visi[ch][cw]);
    while(!q.empty()){
        auto l = q.front();
        if(l.ff == visi[dh][dw]){
            cout << l.ss << "\n";
            return 0; 
        }
        for(auto b : gr[l.ff]){
            if(yo.find(b) == yo.end()){
                q.push({b, l.ss+1});
                yo.insert(b);
            }
        }
        q.pop();
    }

    cout << "-1\n";
    
    return 0;
}

Measured memory_bytes: 57932