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

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007LL

#define rep(i, n) for(ll (i) = 0LL;(i) < (ll)(n);(i)++)
#define rep2(i, s, e) for(ll (i) = (ll)(s);(i) < (ll)(e);(i)++)
#define repi(i, n) for(ll (i) = 0LL;(i) <= (ll)(n);(i)++)
#define repi2(i, s, e) for(ll (i) = (ll)(s);(i) <= (ll)(e);(i)++)
#define per(i, n) for(ll (i) = (ll)(n) - 1LL;(i) >= 0LL;(i)--)
#define per2(i, s, e) for(ll (i) = (ll)(s) - 1LL;(i) >= (ll)(e);(i)--)
#define peri(i, n) for(ll (i) = (ll)(n);(i) >= 0LL;(i)--)
#define peri2(i, s, e) for(ll (i) = (ll)(s);(i) >= (ll)(e);(i)--)
#define iter(i, it) for(auto &(i): (it))

template<typename T, typename U> ostream& operator<<(ostream &s, const pair<T, U> m) {
    cout << "(" << m.first << ", " << m.second << ")";
    return s;
}
template<typename T, typename U> ostream& operator<<(ostream &s, const map<T, U> m) {
    ll c = 0;
    cout << "{ ";
    iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const vector<T> &v) {
    cout << "{ ";
    rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}
template<typename T> ostream& operator<<(ostream &s, const list<T> &v) {
    ll c = 0;
    cout << "{ ";
    iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
    cout << "}";
    return s;
}

int main(void) {
    ll H, W;
    cin >> H >> W;
    ll Ch, Cw;
    cin >> Ch >> Cw;
    Ch--; Cw--;
    ll Dh, Dw;
    cin >> Dh >> Dw;
    Dh--; Dw--;
    vector<string> S(H);
    vector<vector<pair<pair<bool, bool>, ll>>> SD(H, vector<pair<pair<bool, bool>, ll>>(W, pair<pair<bool, bool>, ll>{pair<bool, bool>{false, false}, numeric_limits<ll>::max() / 2}));
    rep(h, H) cin >> S[h];
    deque<pair<ll, ll>> Q;
    Q.emplace_front(Ch, Cw);
    SD[Ch][Cw].first = pair<bool, bool>{true, true};
    SD[Ch][Cw].second = 0;
    while(!Q.empty()) {
        static vector<pair<ll, ll>> directions{
            {-1, 0},
            {1, 0},
            {0, 1},
            {0, -1},
        };
        pair<ll, ll> current = Q.front();
        Q.pop_front();
        iter(dir, directions) {
            pair<ll, ll> temp = current;
            temp.first += dir.first;
            temp.second += dir.second;
            if(temp.first >= 0 && temp.first < H && temp.second >= 0 && temp.second < W) {
                if(S[temp.first][temp.second] != '#' && !SD[temp.first][temp.second].first.first) {
                    SD[temp.first][temp.second].second = min(SD[temp.first][temp.second].second, SD[current.first][current.second].second);
                    SD[temp.first][temp.second].first.first = true;
                    Q.push_front(temp);
                }
            }
        }
        rep(i, 5) {
            rep(j, 5) {
                pair<ll, ll> temp = current;
                temp.first += -2 + i;
                temp.second += -2 + j;
                if(temp.first >= 0 && temp.first < H && temp.second >= 0 && temp.second < W) {
                    if(S[temp.first][temp.second] != '#' && !SD[temp.first][temp.second].first.second) {
                        SD[temp.first][temp.second].first.second = true;
                        SD[temp.first][temp.second].second = min(SD[temp.first][temp.second].second, SD[current.first][current.second].second + 1);
                        Q.push_back(temp);
                    }
                }
            }
        }
    }
    if(SD[Dh][Dw].second == numeric_limits<ll>::max() / 2) cout << -1 << endl;
    else cout << SD[Dh][Dw].second << endl;
    return 0;
}


Measured memory_bytes: 21332