#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <iterator>
#include <map>
#include <set>
#include <iomanip>
#include <vector>
#include <cstdint>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define rep(i, N) for(int i = 0; i < N; i++)
#define rep2(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000000000000
#define MAX 200001

const ll MOD = 1000000007;

template <typename T > inline string toString(const T &a) {ostringstream oss; oss << a; return oss.str();};

template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};

using mint = Fp<MOD>;

struct edge{
    ll to,cost;
};

vector<edge> G[1000010];
ll d[1000010];


void dijkstra(ll s){
    priority_queue<P,vector<P>,greater<P> > que;
    fill(d,d+1000010,INF);
    d[s]=0;
    que.push(P(0, s));
    while (!que.empty()){
        P p = que.top();
        que.pop();
        ll v=p.second;
        if(d[v]<p.first) continue;
        for (int 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;
                que.push(P(d[e.to],e.to));
            }
        }
    }
}

int main(){
    ll H, W;
    cin >> H >> W;
    ll Ch,Cw;
    cin >> Ch >> Cw;
    ll Dh,Dw;
    cin >> Dh >> Dw;
    Ch--;Cw--;
    Dh--;Dw--;
    vector<string> S(H);
    rep(i,H) cin >> S[i];
    rep(i,H){
        rep(j,W){
            if(S[i][j] == '#') continue;
            ll v = i*W + j;
            for(int x = -2; x <= 2; x++){
                for(int y = -2; y <= 2; y++){
                    if((x == 0 && (y == 1 or y == -1)) or (y == 0 && (x == 1 or x == -1))){
                        ll nx = j + x;
                        ll ny = i + y;
                        if(0 <= nx && nx < W && 0 <= ny && ny < H){
                            ll u = ny*W + nx;
                            if(S[ny][nx] == '#') continue;
                            edge e1 = {v,0};
                            edge e2 = {u,0};
                            G[u].push_back(e1);
                            G[v].push_back(e2);
                        }
                    }
                    else if(x==0 && y==0) continue;
                    else{
                        ll nx = j + x;
                        ll ny = i + y;
                        if(0 <= nx && nx < W && 0 <= ny && ny < H){
                            ll u = ny*W + nx;
                            if(S[ny][nx] == '#') continue;
                            edge e1 = {v, 1};
                            edge e2 = {u, 1};
                            G[u].push_back(e1);
                            G[v].push_back(e2);
                        }
                    }
                }
            }
        }
    }
    dijkstra(Ch*W+Cw);
    ll ans = d[Dh*W+Dw];
    if(ans == INF) ans = -1;
    cout << ans << endl;
}

Measured memory_bytes: 292432