#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int di[4] = {1, 0, -1, 0};
int dj[4] = {0, 1, 0, -1};

class Graph {
    int N;
    vector<set<int>> edge;

public:
    Graph(int N) : N(N), edge(N){};
    void add(int a, int b) {
        edge[a].emplace(b);
        edge[b].emplace(a);
    }
    int bfs(int s, int g) {
        vector<int> dis(N, -1);
        dis[s] = 0;
        queue<int> qu;
        qu.emplace(s);
        while (!qu.empty()) {
            int i = qu.front();
            qu.pop();
            for (auto &j : edge[i]) {
                if (dis[j] == -1) {
                    dis[j] = dis[i] + 1;
                    qu.emplace(j);
                }
            }
        }
        return dis[g];
    }
};

class Grid {
    int H, W;
    vector<string> S;
    vector<vector<int>> group;

public:
    Grid(int H, int W, const vector<string> &S)
        : H(H), W(W), S(S), group(H + 2, vector<int>(W + 2)){};
    void dfs(int i, int j, int g) {
        if (group[i][j] > 0 || S[i][j] == '#') return;
        group[i][j] = g;
        REP(k, 4) {
            int a = i + di[k];
            int b = j + dj[k];
            dfs(a, b, g);
        }
    }
    int devide() {
        int g = 0;
        FOR(i, 1, H + 1) {
            FOR(j, 1, W + 1) {
                if (group[i][j] > 0 || S[i][j] == '#') continue;
                ++g;
                dfs(i, j, g);
            }
        }
        return g;
    }
    int ans(int ch, int cw, int dh, int dw) {
        int num_group = devide();
        int s = group[ch][cw];
        int g = group[dh][dw];
        Graph graph(num_group + 1);
        FOR(i, 1, H + 1) {
            FOR(j, 1, W + 1) {
                if (S[i][j] == '#') continue;
                FOR(k, -2, 3) {
                    int a = i + k;
                    if (a < 1 || a > H) continue;
                    FOR(l, -2, 3) {
                        int b = j + l;
                        if (b < 1 || b > W) continue;
                        if (S[a][b] == '#' || group[a][b] == group[i][j]) continue;
                        graph.add(group[i][j], group[a][b]);
                    }
                }
            }
        }
        return graph.bfs(s, g);
    }
};

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

    int H, W;
    cin >> H >> W;
    int ch, cw, dh, dw;
    cin >> ch >> cw >> dh >> dw;
    vector<string> S(H + 2);
    S[0] = S[H + 1] = string(W + 2, '#');
    REP(i, H) {
        string s;
        cin >> s;
        S[i + 1] = '#' + s + '#';
    }

    Grid grid(H, W, S);
    cout << grid.ans(ch, cw, dh, dw) << endl;

    return 0;
}


Measured memory_bytes: 34860