#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef long double ld;
typedef pair<long double, long double> pld;

const ll INF = 1e18;
const ll MOD = 1e9 + 7;

const ll MAX_V = 1e6 + 10;

struct edge {
    ll to, cost;
};
typedef pair<ll, ll> P;
vector<edge> G[MAX_V];
ll d[MAX_V];

void dijkstra(ll start, ll V, vector<edge> G[], ll d[]) {
    priority_queue<pll, vector<pll>, greater<pll>> que;
    fill(d, d + V, INF);
    d[start] = 0;
    que.push(pll(0, start));

    while(!que.empty()) {
        pll p = que.top();
        que.pop();
        ll v = p.second;
        if(d[v] < p.first)
            continue;
        for(ll i = 0; i < G[v].size(); i++) {
            edge e = G[v][i];
            if(d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll i, j, k, l;

    ll H, W;
    cin >> H >> W;
    ll si, sj, gi, gj;
    cin >> si >> sj >> gi >> gj;
    si--, sj--, gi--, gj--;
    vector<string> S;
    for(i = 0; i < H; i++) {
        string s;
        cin >> s;
        S.push_back(s);
    }
    // for(i = 0; i < MAX_V; i++) {
    //     d[i] = INF;
    // }
    // d[si * W + sj] = 0;
    for(i = 0; i < MAX_V; i++) {
        vector<edge> g(0);
        G[i] = g;
    }
    for(i = 0; i < H; i++) {
        for(j = 0; j < W; j++) {
            if(S[i][j] == '#')
                continue;
            for(k = -1; k < 2; k++) {
                for(l = -1; l < 2; l++) {
                    if(i + k < 0 || i + k >= H)
                        continue;
                    if(j + l < 0 || j + l >= W)
                        continue;
                    if(k != 0 && l != 0)
                        continue;
                    if(k == 0 && l == 0)
                        continue;
                    if(S[i + k][j + l] == '#')
                        continue;
                    edge e;
                    e.to = (i + k) * W + (j + l);
                    e.cost = 0;
                    G[i * W + j].push_back(e);
                }
            }
            for(k = -2; k < 3; k++) {
                for(l = -2; l < 3; l++) {
                    if(i + k < 0 || i + k >= H)
                        continue;
                    if(j + l < 0 || j + l >= W)
                        continue;
                    if(k == 0 && l == 0)
                        continue;
                    if(S[i + k][j + l] == '#')
                        continue;
                    edge e;
                    e.to = (i + k) * W + (j + l);
                    e.cost = 1;
                    G[i * W + j].push_back(e);
                }
            }
        }
    }
    dijkstra(si * W + sj, H * W, G, d);
    ll ans = d[gi * W + gj];
    if(ans == INF) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 191680