// Contest: AtCoder Beginner Contest 176 (https://atcoder.jp/contests/abc176)
// Problem: D: Wizard in Maze (https://atcoder.jp/contests/abc176/tasks/abc176_d)


// region boilerplate
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <chrono>
#include <cstdint>
#include <deque>
#include <exception>
#include <forward_list>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#if __cplusplus >= 201703L
#   include <optional>
#   include <string_view>
#   include <variant>
#endif

using namespace std;

#if __cplusplus >= 201703L
#   define all(a)      begin(a),end(a)
#   define sz(x)       ((int)size(x))
#else
#   define all(a)      (a).begin(),(a).end()
#   define sz(x)       ((int)(x).size())
#endif

#define rep(a, b)      for(int a = 0; a < (b); ++a)
#define reps(a, b, c)  for(int a = (b); a < (c); ++a)
#define trav(a, b)     for(auto& a : b)

using ll = long long;
using ld = long double;
using u64 = uint64_t;
using u32 = uint32_t;

using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vpi = vector<pi>;

#if __cplusplus >= 201703L
#   define MAYBE_UNUSED [[maybe_unused]]
#else
#   define MAYBE_UNUSED
#endif

MAYBE_UNUSED static constexpr int INF = (int)1e9 + 5;
MAYBE_UNUSED static constexpr ll INFL = (ll)1e18 + 5;
MAYBE_UNUSED static mt19937 rng((u32)chrono::duration_cast<chrono::nanoseconds>(chrono::high_resolution_clock::now().time_since_epoch()).count());
//endregion

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int h, w; cin >> h >> w;
    int sx, sy; cin >> sy >> sx; --sx, --sy;
    int tx, ty; cin >> ty >> tx; --tx, --ty;
    vector<string> g(h);
    trav(gi, g) cin >> gi;

    int cnt = 0;
    vvi comp(h, vi(w, -1));
    rep(y, h) rep(x, w) {
        if (comp[y][x] != -1 || g[y][x] == '#')
            continue;
        comp[y][x] = cnt;
        queue<pi> q; q.emplace(y, x);
        while (sz(q)) {
            auto [yi, xi] = q.front(); q.pop();
            for (auto [dx, dy] : {pair(-1,0), {1,0}, {0,-1}, {0,1}})
                if (0 <= xi + dx && xi + dx < w && 0 <= yi + dy && yi + dy < h && g[yi+dy][xi+dx] != '#' && comp[yi+dy][xi+dx] == -1)
                    comp[yi+dy][xi+dx] = cnt, q.emplace(yi + dy, xi + dx);
        }
        ++cnt;
    }

    vector<set<int>> adj(cnt);
    rep(y, h) rep(x, w) {
        if (g[y][x] == '#')
            continue;
        reps(dx, -2, 3) reps(dy, -2, 3)
            if (0 <= x + dx && x + dx < w && 0 <= y + dy && y + dy < h && g[y+dy][x+dx] != '#')
                adj[comp[y][x]].emplace(comp[y+dy][x+dx]);
    }

    vi dist(cnt + 1, INF);
    queue<int> q; q.push(comp[sy][sx]);
    dist[comp[sy][sx]] = 0;
    while (sz(q)) {
        auto v = q.front(); q.pop();
        for (auto v2 : adj[v])
            if (dist[v2] == INF)
                dist[v2] = dist[v] + 1, q.push(v2);
    }

    cout << (dist[comp[ty][tx]] == INF ? -1 : dist[comp[ty][tx]]) << '\n';
    return 0;
}


Measured memory_bytes: 40096