#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
using vs = vector<string>;
using vb = vector<bool>;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define REPS(c, s) for (char c : s)
#define ALL(c) (c).begin(), (c).end()
#define SORT(c) sort(ALL(c))
#define REV(c) reverse(ALL(c))
#define sz(v) (int)v.size()
#define endl '\n'
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
template<class T> inline void prn(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << ' ';}
template<class T> inline void printv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << (i == n - 1 ? endl : ' ');}
template<class T> inline void printvv(vector<vector<T>>& v) {for (auto u : v) printv(u);}
template<class T> inline void printlnv(vector<T>& v) {int n = sz(v); REP(i, n) cout << v[i] << endl;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
 
void solve();
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

// FIXME : need to refactor
inline vector<int> get_d(int n) {return vector<int>(n, INF);}
inline vector<ll> get_d(ll n) {return vector<ll>(n, LINF);}
template<typename T> vector<T> dijkstra(int s, vector<vector<pair<int, T>>>& g) {
    T n = sz(g);
    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
    vector<T> d = get_d(n);
    d[s] = 0;
    q.emplace(0, s);
 
    while (!q.empty()) {
        pair<T, int> p = q.top();
        q.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
 
        for (auto x : g[v]) {
            if (d[x.first] > d[v] + x.second) {
                d[x.first] = d[v] + x.second;
                q.emplace(d[x.first], x.first);
            }
        }
    }
    return d;
}

void solve() {
    int h, w, ch, cw, dh, dw;
    cin >> h >> w >> ch >> cw >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    vs s(h);
    REP(i, h) cin >> s[i];
    vector<vector<pair<int, int>>> g(h * w, vector<pair<int, int>>());
    REP(i, h) REP(j, w) {
        if (s[i][j] == '#') continue;;
        if (i != 0) {
            if (s[i - 1][j] == '.') {
                g[i * w + j].emplace_back((i - 1) * w + j, 0);
                g[(i - 1) * w + j].emplace_back(i * w + j, 0);
            }
        }
        if (i != h - 1) {
            if (s[i + 1][j] == '.') {
                g[i * w + j].emplace_back((i + 1) * w + j, 0);
                g[(i + 1) * w + j].emplace_back(i * w + j, 0);
            }
        }
        if (j != 0) {
            if (s[i][j - 1] == '.') {
                g[i * w + j].emplace_back(i * w + j - 1, 0);
                g[i * w + j - 1].emplace_back(i * w + j, 0);
            }
        }
        if (j != w - 1) {
            if (s[i][j + 1] == '.') {
                g[i * w + j].emplace_back(i * w + j + 1, 0);
                g[i * w + j + 1].emplace_back(i * w + j, 0);
            }
        }
        REP(k, 5) REP(l, 5) {
            if (k == 2 and (l == 1 or l == 2 or l == 3)) continue;
            if (l == 2 and (k == 1 or k == 2 or k == 3)) continue;
            if (i - 2 + k < 0) continue;
            if (i - 2 + k >= h) continue;
            if (j - 2 + l < 0) continue;
            if (j - 2 + l >= w) continue;
            if (s[i - 2 + k][j - 2 + l] == '.') {
                g[i * w + j].emplace_back((i - 2 + k) * w + j - 2 + l, 1);
                g[(i - 2 + k) * w + j - 2 + l].emplace_back(i * w + j, 1);
            }
        }
    }
    vector<int> d = dijkstra(ch * w + cw, g);
    // int z = 0;
    // for (auto q : g) {
    //     cout << z++ << " : ";
    //     for (auto p : q) {
    //         cout << p.first << "/" << p.second << " | ";
    //     }
    //     cout << endl;
    // }
    // printv(d);
    // cout << "??? " << dh * h + dw << endl;
    cout << (d[dh * w + dw] == INF ? -1 : d[dh * w + dw]) << endl;

}


Measured memory_bytes: 163592