#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>
#include <cstring>
#include <regex>
#include <random>
#include <cassert>
#include <stack>
#include <climits>
#include <chrono>
#include <fstream>
using namespace std;

#pragma region

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)

const double PI = acos(-1.0);

using ll = long long;
using P = pair<int, int>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T>
istream& operator >>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

template<class T, class U>
ostream& operator <<(ostream &os, pair<T, U> p) {
    cout << '(' << p.first << ", " << p.second << ')';
    return os;
}

template<class T>
void print(const vector<T> &v, const string &delimiter = " ") { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; }

template<class T>
void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); }

#pragma endregion

template<class Weight>
struct WeightedGraph {
    struct Edge{
        int to;
        Weight weight;
    };
    int n;
    vector<vector<Edge>> edges;
    Weight zero;
    Weight inf;
    WeightedGraph(int n, Weight zero, Weight inf) : n(n), edges(n), zero(zero), inf(inf) {}
    void add_edge(int u, int v, Weight w) { edges[u].push_back({v, w}); }
};

template<class Weight>
vector<Weight> dijkstra(int s, const WeightedGraph<Weight> &g) {
    using Node = pair<Weight, int>;
    priority_queue<Node, vector<Node>, greater<Node>> que;
    vector<Weight> d(g.n, g.inf);
    d[s] = g.zero;
    que.push(Node(g.zero, s));

    while (!que.empty()) {
        Node p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < (int)g.edges[v].size(); i++) {
            auto e = g.edges[v][i];
            if (d[e.to] > d[v] + e.weight) {
                d[e.to] = d[v] + e.weight;
                que.push(Node(d[e.to], e.to));
            }
        }
    }
    return d;
}

int dy[4] = {-1, 0, 1, 0};
int dx[4] = {0, -1, 0, 1};

int main() {
    int h, w;
    cin >> h >> w;
    int sy, sx;
    cin >> sy >> sx;
    int gy, gx;
    cin >> gy >> gx;

    sy--; sx--;
    gy--; gx--;

    vector<string> grid(h);
    cin >> grid;

    WeightedGraph<int> graph(h * w, 0, 1000000000);
    rep(i, h) rep(j, w) {
        rep(d, 4) {
            int ny = i + dy[d], nx = j + dx[d];
            if (0 <= ny && ny < h && 0 <= nx && nx < w && grid[ny][nx] == '.') {
                graph.add_edge(i * w + j, ny * w + nx, 0);
            }
        }

        repr(x, -2, 3) repr(y, -2, 3) {
            int ny = i + y, nx = j + x;
            if (0 <= ny && ny < h && 0 <= nx && nx < w && grid[ny][nx] == '.') {
                graph.add_edge(i * w + j, ny * w + nx, 1);
            }
        }
    }

    auto dist = dijkstra(sy * w + sx, graph);
    if (dist[gy * w + gx] == 1000000000) cout << -1 << endl;
    else cout << dist[gy * w + gx] << endl;
}

Measured memory_bytes: 203488