#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\n'
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                      \
    do {                                    \
        ostringstream os;                   \
        os << setw(n) << setfill('0') << s; \
        s = os.str();                       \
    } while (0);
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
#define INIT                               \
    std::cin.tie(nullptr);                 \
    std::ios_base::sync_with_stdio(false); \
    cout << std::fixed << std::setprecision(10);

template <typename T>
vector<T> make_vec(size_t a, T n) { return vector<T>(a, n); }
template <typename T, typename... Ts>
auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); }
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;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec)
        is >> x;
    return is;
}

constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

template <typename T>
T intceil(T a, T b) {
    return (a + (b - 1)) / b;
}

signed main() {
    INIT;
    cin >> H >> W;
    cin >> A >> B >> C >> D;
    A--, B--, C--, D--;
    vector<vector<char>> g(H, vector<char>(W));
    vvi used(H, vi(W));
    vvi res(H, vi(W, INF));
    REP(i, H) {
        REP(j, W) {
            cin >> g[i][j];
        }
    }
    using T = pair<int, pair<int, int>>;
    deque<T> q;
    q.push_front({0, {A, B}});
    while (!q.empty()) {
        auto now = q.front();
        dump(now.first, now.second);
        int h = now.second.first;
        int w = now.second.second;
        q.pop_front();
        if (used[h][w])
            continue;
        chmin(res[h][w], now.first);
        used[h][w] = 1;
        REP(i, 4) {
            if (h + dx[i] < 0 || H <= h + dx[i] || w + dy[i] < 0 || W <= w + dy[i])
                continue;
            if (g[h + dx[i]][w + dy[i]] == '.') {
                q.push_front(make_pair(now.first, make_pair(h + dx[i], w + dy[i])));
            }
        }
        REP(i, 5) {
            REP(j, 5) {
                int ii = i - 2;
                int jj = j - 2;
                if (ii == 0 && jj == 0)
                    continue;
                if (h + ii < 0 || H <= h + ii || w + jj < 0 || W <= w + jj)
                    continue;
                if (g[h + ii][w + jj] == '.') {
                    q.push_back(make_pair(now.first + 1, make_pair(h + ii, w + jj)));
                }
            }
        }
    }
    if (res[C][D] == INF)
        cout << -1 << endl;
    else
        cout << res[C][D] << endl;
}


Measured memory_bytes: 13748