#include <bits/stdc++.h>

#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
using namespace std;

#define sqr(a) ((a)*(a))
#define forn(i, n) for(int i = 0; i < (int)n; ++i)
#define rep(i, k, n) for(int i = (int)k; i <= (int)n; ++i)
#define nrof(i, n) for(int i = (int)n - 1; i >= 0; --i)
#define x first
#define y second
#define mp make_pair
#define sz(a) (int)(a.size())
#define pb push_back
#define eb emplace_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double dd;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 1e6 + 9;
const int M = 2e3 + 9;
const int INFINT = 1e9 + 9;
const ll INFLL = 1e18 + 9;
const ll MOD = (ll) (1000000007);

vector<int> dx = {-1, 0, 1, 0};
vector<int> dy = {0, -1, 0, 1};

void solve() {
    int h, w;
    cin >> h >> w;
    int ch, cw;
    cin >> ch >> cw;
    --ch, --cw;
    int dh, dw;
    cin >> dh >> dw;
    --dh, --dw;
    vector<vector<char>> s(h, vector<char>(w));
    vector<vector<int>> cnt(h, vector<int>(w, -1));
    vector<vector<int>> status(h, vector<int>(w, 0));
    forn(i, h) forn(j, w) cin >> s[i][j];

    set<pair<int, pii>> st;

    st.insert({0, {ch, cw}});
    cnt[ch][cw] = 0;

    function<bool(pii)> is_valid = [h, w](pii v){
        return v.x >= 0 && v.x < h && v.y >= 0 && v.y < w;
    };

    function<bool(pii, pii)> can_tp = [](pii a, pii b){
        return abs(a.x - b.x) <= 2 && abs(a.y - b.y) <= 2;
    };

    while (!st.empty()) {

        pii v = st.begin()->y;
        st.erase(st.begin());

//        cout << "v: " << v.x << ' ' << v.y << endl;

        forn(tt, 4) {
            pii g = v;
            g.x += dx[tt];
            g.y += dy[tt];
//                    cout << "g " << g.x << ' ' << g.y << endl;
            if (is_valid(g) && s[g.x][g.y] == '.') {
                if (cnt[g.x][g.y] == -1) {
                    st.insert({cnt[v.x][v.y], {g.x, g.y}});
                    cnt[g.x][g.y] = cnt[v.x][v.y];
                } else if (cnt[g.x][g.y] > cnt[v.x][v.y]){
                    st.erase({cnt[g.x][g.y], {g.x, g.y}});
                    st.insert({cnt[v.x][v.y], {g.x, g.y}});
                    cnt[g.x][g.y] = cnt[v.x][v.y];
                }
            }
        }

        for (int i = -2; i <= 2; ++i) {
            for (int j = -2; j <= 2; ++j) {
                pii g = v;
                g.x += i;
                g.y += j;
//                    cout << "g " << g.x << ' ' << g.y << endl;
                if (is_valid(g) && s[g.x][g.y] == '.') {
                    if (cnt[g.x][g.y] == -1) {
                        st.insert({cnt[v.x][v.y], {g.x, g.y}});
                        cnt[g.x][g.y] = cnt[v.x][v.y] + 1;
                    } else if (cnt[g.x][g.y] > cnt[v.x][v.y] + 1){
                        st.erase({cnt[g.x][g.y], {g.x, g.y}});
                        st.insert({cnt[v.x][v.y] + 1, {g.x, g.y}});
                        cnt[g.x][g.y] = cnt[v.x][v.y] + 1;
                    }
                }
            }
        }

    }

    cout << cnt[dh][dw] << endl;
}

int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


Measured memory_bytes: 12732