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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

template <typename T1, typename T2>
bool chmin(T1& a, const T2& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool chmax(T1& a, const T2& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

class Prints {
private:
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
        void operator()(const Args&... args) const { print(args...); }

    private:
        const char *sep, *term;
        void print() const { std::cout << term; }
        void print_rest() const { std::cout << term; }
        template <class T, class... Tail>
        void print(const T& head, const Tail&... tail) const { std::cout << head, print_rest(tail...); }
        template <class T, class... Tail>
        void print_rest(const T& head, const Tail&... tail) const { std::cout << sep << head, print_rest(tail...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

using Pos = array<ll, 2>;
int cell[1010][1010];
int dp[1010][1010];

void solve() {
    ll H, W;
    cin >> H >> W;
    Pos s, t;
    cin >> s[0] >> s[1];
    cin >> t[0] >> t[1];
    --s[0], --s[1], --t[0], --t[1];
    for (ll i = 0; i < H; i++) {
        string a;
        cin >> a;
        for (ll j = 0; j < W; j++) {
            cell[i][j] = a[j] == '#';
        }
    }

    for (ll i = 0; i < H; i++) {
        for (ll j = 0; j < W; j++) {
            dp[i][j] = I_INF;
        }
    }

    using Tup = tuple<int, int, int>;
    priority_queue<Tup, vector<Tup>, greater<Tup>> pq;
    pq.emplace(0, s[0], s[1]);
    dp[s[0]][s[1]] = 0;
    constexpr Pos steps[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    while (!pq.empty()) {
        auto [c, x, y] = pq.top();
        pq.pop();
        if (c > dp[x][y]) continue;
        for (ll i = 0; i < 4; i++) {
            int nx = x + steps[i][0];
            int ny = y + steps[i][1];
            int nc = c;
            if (!(nx >= 0 && nx < H && ny >= 0 && ny < W)) continue;
            if (cell[nx][ny] == 1) continue;
            if (dp[nx][ny] <= nc) continue;
            dp[nx][ny] = nc;
            pq.emplace(nc, nx, ny);
        }
        for (ll i = 0; i < 5; i++) {
            for (ll j = 0; j < 5; j++) {
                int nx = x + i - 2;
                int ny = y + j - 2;
                int nc = c + 1;
                if (!(nx >= 0 && nx < H && ny >= 0 && ny < W)) continue;
                if (cell[nx][ny] == 1) continue;
                if (dp[nx][ny] <= nc) continue;
                dp[nx][ny] = nc;
                pq.emplace(nc, nx, ny);
            }
        }
    }
    int ans = dp[t[0]][t[1]];
    if (ans == I_INF) {
        prints()(-1);
    }
    else {
        prints()(ans);
    }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    ofstream ofs("./in_out/output.txt");
    cout.rdbuf(ofs.rdbuf());
#endif
    cout << setprecision(18) << fixed;
    solve();
    cout << flush;
    return 0;
}

Measured memory_bytes: 11696