#include <bits/stdc++.h>
using namespace std;

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
//constexpr long long MOD = (ll)1e9 + 7;
constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//グラフ関連
struct Edge {
    ll to, cap, rev;
    Edge(ll _to, ll _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void Dijkstra(Graph &G, ll s, Array &d) { //O(|E|log|V|)
    d.resize(G.size());
    REP(i, d.size())
    d[i] = INF;
    d[s] = 0;
    priority_queue<P, vector<P>, greater<P>> q;
    q.push(make_pair(0, s));
    while(!q.empty()) {
        P a = q.top();
        q.pop();
        if(d[a.second] < a.first)
            continue;
        REP(i, G[a.second].size()) {
            Edge e = G[a.second][i];
            if(d[e.to] > d[a.second] + e.cap) {
                d[e.to] = d[a.second] + e.cap;
                q.push(make_pair(d[e.to], e.to));
            }
        }
    }
}

int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

void solve() {
    ll h, w;
    cin >> h >> w;
    ll sy, sx, gy, gx;
    cin >> sy >> sx >> gy >> gx;
    sy--;
    sx--;
    gy--;
    gx--;

    vec<string> s(h);
    REP(i, h) {
        cin >> s[i];
    }

    Graph g(h * w);
    REP(i, h) {
        REP(j, w) {
            if(s[i][j] == '#')
                continue;
            REP(k, 4) {
                int ni = i + dy[k];
                int nj = j + dx[k];
                if(ni >= h or ni < 0 or nj >= w or nj < 0)
                    continue;
                if(k < 4 and s[ni][nj] == '.') {
                    add_edge(g, i * w + j, ni * w + nj, 0, false, 0);
                }
            }

            for(int ni = i - 2; ni <= i + 2; ni++) {
                for(int nj = j - 2; nj <= j + 2; nj++) {
                    if(ni >= h or ni < 0 or nj >= w or nj < 0)
                        continue;
                    if(i == ni and j == nj)
                        continue;
                    if(s[ni][nj] == '#')
                        continue;
                    add_edge(g, i * w + j, ni * w + nj, 1, false, 1);
                }
            }
        }
    }

    Array d;
    Dijkstra(g, sy * w + sx, d);

    if(d[gy * w + gx] == INF)
        cout << -1 << en;
    else
        cout << d[gy * w + gx] << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    /*
    ll t;
    cin >> t;
    while(t--)*/
    solve();

    return 0;
}


Measured memory_bytes: 265520