#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <deque>
#include <string>

#include <algorithm>
#include <functional>
#include <bitset>
#include <functional>
#include <chrono>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
//const i64 smod = 998244353ll;
//const i64 inf = 1'000'000'000'000'000'000ll + 7;
//const i64 inf = 1'000'000'007;

const double inf = 1e12;

const long double eps = 1e-8;

i64 rows, cols;
i64 srow, scol;
i64 frow, fcol;

vector<string> a;
vector<vector<i64>> b;
i64 mb;

const vector<pair<i64, i64>> d = { {0, 1}, {0, -1}, {1, 0}, {-1, 0} };

int main(int argc, char* argv[]) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed); cerr.precision(15); cerr.setf(ios::fixed);

    cin >> rows >> cols;
    cin >> srow >> scol;
    cin >> frow >> fcol;
    srow -= 1;
    scol -= 1;
    frow -= 1;
    fcol -= 1;

    a = vector<string>(rows);
    b = vector<vector<i64>>(rows, vector<i64>(cols));

    for (i64 i = 0; i < rows; i++) {
        cin >> a[i];
    }

    for (i64 i = 0; i < rows; i++) {
        for (i64 j = 0; j < cols; j++) {
            if (a[i][j] == '#') {
                continue;
            }
            if (b[i][j]) {
                continue;
            }

            mb += 1;
            queue<pair<i64, i64>> q;
            q.push({ i, j });
            b[i][j] = mb;

            while (!q.empty()) {
                auto p = q.front();
                q.pop();

                for (auto t : d) {
                    pair<i64, i64> np = { p.first + t.first, p.second + t.second };
                    if (0 > np.first || np.first >= rows || 0 > np.second || np.second >= cols) {
                        continue;
                    }
                    if (a[np.first][np.second] == '#') {
                        continue;
                    }
                    if (b[np.first][np.second]) {
                        continue;
                    }
                    q.push({ np.first, np.second });
                    b[np.first][np.second] = mb;
                }
            }
        }
    }


    vector<set<i64>> e(mb + 1);
    for (i64 i = 0; i < rows; i++) {
        for (i64 j = 0; j < cols; j++) {
            if (a[i][j] == '#') {
                continue;
            }
            for (i64 x = -2; x <= 2; x++) {
                for (i64 y = -2; y <= 2; y++) {
                    pair<i64, i64> np = { i + x, j + y };
                    if (0 > np.first || np.first >= rows || 0 > np.second || np.second >= cols) {
                        continue;
                    }
                    if (a[np.first][np.second] == '#') {
                        continue;
                    }
                    if (b[i][j] != b[np.first][np.second]) {
                        e[b[i][j]].insert(b[np.first][np.second]);
                        e[b[np.first][np.second]].insert(b[i][j]);
                    }
                }
            }
        }
    }

    i64 s = b[srow][scol];
    i64 f = b[frow][fcol];

    i64 ans = 0;

    vector<i64> q;
    set<i64> v;

    q.push_back(s);
    v.insert(s);

    i64 len = q.size();

    for (i64 i = 0; i < q.size(); i++) {
        if (i == len) {
            ans += 1;
            len = q.size();
        }

        i64 c = q[i];
        if (c == f) {
            cout << ans << endl;
            return 0;
        }

        for (auto n : e[c]) {
            if (v.count(n)) {
                continue;
            }

            q.push_back(n);
            v.insert(n);
        }
    }

    cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 44764