/*{{{*/
#include "bits/stdc++.h"
using namespace std;

#define MT make_tuple
#define all(x) begin(x), end(x)
#define putchar(x) cout << (x)
static int fastio = [](){ ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(17); return 0; }();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename ...T> string format(const string& fmt, T&&... args) {
    size_t sz = snprintf(nullptr, 0, fmt.c_str(), args...) + 1;
    unique_ptr<char[]> buf(new char[sz]);
    snprintf(buf.get(), sz, fmt.c_str(), args...);
    return string(buf.get(), buf.get() + sz - 1);
}

template<class T> struct rge { T b, e; auto begin() const { return b; } auto end() const { return e; } };
template<class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
template<class T> auto dud(T* x) -> decltype(cerr << *x, 0);
template<class T> char dud(...);

struct debug {
#if DEBUG == 2
    debug(int line) {
        if(line) cerr << "LINE(" << line << ") -> ";
    }
    template<class T> typename enable_if<sizeof dud<T>(0) != 1, debug&>::type operator<<(T i) {
        cerr << boolalpha << i; return * this;
    }
    template<class T> typename enable_if<sizeof dud<T>(0) == 1, debug&>::type operator<<(T i) {
        return *this << range(begin(i), end(i));
    }
    template<class T, class U> debug& operator<<(pair<T, U> d) {
        return *this << "(" << d.first << ", " << d.second << ")";
    }
    debug& operator<<(tuple<>&) { return *this << "()"; };
    template<class ...T> debug& operator<<(tuple<T...> d) {
        *this << "("; debug_tuple<sizeof...(T), 0>(d);
        return *this << ")";
    }
    template<size_t L, size_t I, class T> void debug_tuple(const T& t) {
        *this << (I == 0 ? "" : ", ") << get<I>(t);
        if(I + 1 < L) debug_tuple<L, (I + 1) % L>(t);
    }
    template<class T> debug & operator <<(rge<T> d) {
        *this << "[";
        for(auto it = d.b; it != d.e; ++it)
            *this << (it != d.b ?  ", " : "") << *it;
        return *this << "]";
    }
    debug& operator<<(ostream&(*pf)(std::ostream&)) {
        cerr << pf; return *this;
    }
#else
    debug(int) {}
    template<class T> debug& operator<<(T&&) { return *this; }
    debug& operator<<(ostream&(*)(std::ostream&)) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] "

#define debug0()  debug(__LINE__)
#define debug1(x) debug(0)
#define GET_MACRO(_0, _1, NAME, ...) NAME
#define debug(...) GET_MACRO(_0, ##__VA_ARGS__, debug1, debug0)(__VA_ARGS__)
/*}}}*/
using LL = long long;

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAX_N = 200005;

using PII = pair<int, int>;

void solve(int) {
    int H, W;
    int Ch, Cw;
    int Dh, Dw;
    cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
    --Ch, --Cw, --Dh, --Dw;

    vector<string> maze(H);
    for(int i = 0; i < H; ++i) {
        cin >> maze[i];
    }
    vector<vector<bool>> visited(H, vector<bool>(W, false));
    visited[Ch][Cw] = true;

    vector<PII> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    vector<PII> stk = {{Ch, Cw}}, tmp;
    int d = 0;
    while(not stk.empty()) {
        while(not stk.empty()) {
            int r, c;
            tie(r, c) = stk.back();
            stk.pop_back();

            if(r == Dh and c == Dw) {
                cout << d << endl;
                return;
            }

            for(auto& dir : dirs) {
                int dr, dc;
                tie(dr, dc) = dir;
                int r1 = r + dr, c1 = c + dc;
                if(r1 >= 0 and r1 < H and c1 >= 0 and c1 < W and
                    maze[r1][c1] == '.' and not visited[r1][c1]) {
                    visited[r1][c1] = true;
                    stk.emplace_back(r1, c1);
                }
            }
            //debug() << imie(MT(r, c)) imie(stk) << endl;
            tmp.emplace_back(r, c);
        }

        d += 1;

        for(auto& p : tmp) {
            int r, c;
            tie(r, c) = p;
            for(int r1 = max(0, r - 2); r1 <= min(H - 1, r + 2); ++r1) {
                for(int c1 = max(0, c - 2); c1 <= min(W - 1, c + 2); ++c1) {
                    if(maze[r1][c1] == '.' and not visited[r1][c1]) {
                        visited[r1][c1] = true;
                        stk.emplace_back(r1, c1);
                    }
                }
            }
        }
        tmp.clear();
    }
    cout << -1 << endl;
}

int main() {
    int T = 1;
    //cin >> T;
    for(int i = 1; i <= T; ++i) {
        solve(i);
    }
    return 0;
}


Measured memory_bytes: 5024