#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}
#define DEBUG
#ifdef DEBUG
void debug() { cerr << "\n"; }
template <class T> void debug(const T &x) { cerr << x << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
    for(const T &x : v) {
        cerr << x << " ";
    }
    cerr << "\n";
}
#else
template <class T, class... Args>
void debug(const T &x, const Args &... args) {}
template <class T> void debugVector(const vector<T> &v) {}
#endif

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

using P = pair<int, int>;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h, w;
    cin >> h >> w;
    P start;
    {
        int ch, cw;
        cin >> ch >> cw;
        start = P(ch - 1, cw - 1);
    }
    P goal;
    {
        int dh, dw;
        cin >> dh >> dw;
        goal = P(dh - 1, dw - 1);
    }
    vector<string> G(h);
    for(int i = 0; i < h; i++) {
        cin >> G[i];
    }
    vector d(h, vector(w, LLINF));
    d[start.first][start.second] = 0;
    priority_queue<pair<ll, P>, vector<pair<ll, P>>, greater<pair<ll, P>>> que;
    que.emplace(0, start);
    while(que.size()) {
        ll nd = que.top().first;
        auto [i, j] = que.top().second;
        que.pop();
        if(d[i][j] < nd) {
            continue;
        }
        // A
        for(int k = 0; k < 4; k++) {
            int ni = i + dy[k];
            int nj = j + dx[k];
            if(0 <= ni && ni < h && 0 <= nj && nj < w && G[ni][nj] == '.') {
                if(d[ni][nj] > nd) {
                    d[ni][nj] = nd;
                    que.emplace(d[ni][nj], P(ni, nj));
                }
            }
        }
        // B
        for(int y = -2; y <= 2; y++) {
            for(int x = -2; x <= 2; x++) {
                if(x == 0 || y == 0) {
                    if(-1 <= x && x <= 1) {
                        if(-1 <= y && y <= 1) {
                            continue;
                        }
                    }
                }
                int ni = i + y;
                int nj = j + x;
                if(0 <= ni && ni < h && 0 <= nj && nj < w && G[ni][nj] == '.') {
                    if(d[ni][nj] > nd + 1) {
                        d[ni][nj] = nd + 1;
                        que.emplace(d[ni][nj], P(ni, nj));
                    }
                }
            }
        }
    }
    if(d[goal.first][goal.second] >= LLINF) {
        cout << -1 << endl;
    } else {
        cout << d[goal.first][goal.second] << endl;
    }
    // for(int x = -2; x <= 2; x++) {
    //     for(int y = -2; y <= 2; y++) {
    //         if(x == 0 || y == 0) {
    //             continue;
    //         }
    //         debug(x, y);
    //     }
    // }
}


Measured memory_bytes: 12420