#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
    for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
    for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define ciosup  \
    cin.tie(0); \
    ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < v.size(); ++i) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    if (v.size() == 0) return os;
    for (int i = 0; i < v.size() - 1; ++i) {
        os << v[i] << " ";
    }
    os << v[v.size() - 1];
    return os;
}

vint dx {1, 0, -1, 0}, dy {0,1,0,-1};
int h, w;

void dfs(pint now, int st, vector<vector<char>>& s, vector<vector<int>>& memo, int& goalst, pint& go) {
    if (memo[now.FI][now.SE] != -1) {
        return;
    }
    memo[now.FI][now.SE] = st;
    if (now == go) {
        goalst = st;
    }
    REP(i, 4) {
        pint newpos = {now.FI + dx[i], now.SE + dy[i]};
        if (0 <= newpos.FI && newpos.FI < h && 0 <= newpos.SE && newpos.SE < w && s[newpos.FI][newpos.SE] == '.') {
            dfs(newpos, st, s, memo, goalst, go);
        }
    }
}

void solve() {
    cin >> h >> w;
    pair<int, int> st, go;
    cin >> st.FI >> st.SE;
    st.FI--;st.SE--;
    cin >> go.FI >> go.SE;
    go.FI--;go.SE--;
    vector<vector<char>> s(h, vector<char>(w));
    REP(i, h) {
        REP(j, w) {
            cin >> s[i][j];
        }
    }
    vector<vector<int>> memo(h, vector<int>(w, {-1}));
    int goalst;
    vector<pint> nodes;
    nodes.push_back(st);
    dfs(st, 0, s, memo, goalst, go);
    int cnt = 1;
    REP(i, h) {
        REP(j, w) {
            if (memo[i][j] == -1 && s[i][j] == '.') {
                nodes.push_back(make_pair(i,j));
                pint now = make_pair(i, j);
                dfs(now, cnt, s, memo, goalst, go);
                ++cnt;
            }
        }
    }
    int sz = nodes.size();
    vector<set<int>> G(sz);
    REP(i,h) {
        REP(j,w){
            int orig = memo[i][j];
            if (orig == -1) continue;
            FOR(di, -2, 3) {
                FOR(dj, -2, 3) {
                    if (di == 0 && dj == 0) continue;
                    pint newpos {i + di, j + dj};
                    if (0 <= newpos.FI && newpos.FI < h && 0 <= newpos.SE && newpos.SE < w && s[newpos.FI][newpos.SE] == '.' && orig != memo[newpos.FI][newpos.SE]) {
                        int to = memo[newpos.FI][newpos.SE];
                        if (to == -1) continue;
                        G[orig].insert(to);
                        G[to].insert(orig);
                    }
                }
            }
        }
    }

    vint distan(sz,-1);
    queue<int> que;
    que.push(0);
    distan[0] = 0;
    while (que.size()) {
        int now = que.front(); que.pop();
        for (int to : G[now]) {
            if (distan[to] == -1) {
                distan[to] = distan[now] + 1;
                que.push(to);
            }
        }
    }
    cout << distan[goalst] << endl;
}

int main() {
    char tmp;
    while (cin >> tmp) {
        cin.putback(tmp);
        solve();
    }
}



Measured memory_bytes: 33732