#include <deque>
#include <iostream>
#include <set>
#include <utility>
#include <vector>
#include<climits>

using namespace std;

int main() {
    int h, w, ch, cw, dh, dw, c_index, d_index;
    cin >> h >> w >> ch >> cw >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    vector<string> s(h);
    // vector<map<pair<int, int>>, bool> road;
    deque<pair<int, int>> q;
    vector<vector<int>> dist(h, vector<int>(w, INT_MAX));

    for (int i = 0; i < h; i++) {
        cin >> s[i];
    }

    vector<vector<bool>> df(h, vector<bool>(w));
    int dx[] = {-1, 0, 1, 0};
    int dy[] = {0, 1, 0, -1};

    q.push_front(pair<int, int>(ch, cw));
    dist[ch][cw] = 0;
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop_front();
        int d = dist[x][y];
        // cout << x << ", " << y << ", " << d << endl;
        for (int i = 0; i < 4; i++) {
            int x_tmp = x + dx[i];
            int y_tmp = y + dy[i];
            if (x_tmp >= 0 && x_tmp < h && y_tmp >= 0 && y_tmp < w &&
                dist[x_tmp][y_tmp] > d && s[x_tmp][y_tmp] == '.') {
                dist[x_tmp][y_tmp] = d;
                q.push_front(pair<int, int>(x_tmp, y_tmp));
                // cout << x_tmp << ", " << y_tmp << ", " << dist[x_tmp][y_tmp]
                //    << endl;
            }
        }

        for (int i = -2; i < 3; i++) {
            for (int j = -2; j < 3; j++) {
                int x_tmp = x + i;
                int y_tmp = y + j;
                if (x_tmp >= 0 && x_tmp < h && y_tmp >= 0 && y_tmp < w &&
                    dist[x_tmp][y_tmp] > d + 1 && s[x_tmp][y_tmp] == '.') {
                    dist[x_tmp][y_tmp] = d + 1;
                    q.push_back(pair<int, int>(x_tmp, y_tmp));
                    // cout << x_tmp << ", " << y_tmp << ", " <<
                    // dist[x_tmp][y_tmp]
                    //    << " ワープ" << endl;
                }
            }
        }
    }

    if (dist[dh][dw] == INT_MAX) {
        cout << -1 << endl;
    } else {
        cout << dist[dh][dw] << endl;
    }

    /*for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (s[i][j] == '.') {
                for (int k = 0; k < road.size(); k++) {
                    auto ite = road[k].find(pair<int, int>(i - 1, j));
                    if (ite != road[k].end()) {
                        if (i == ch && j == cw) {
                            c_index = k;
                        } else if (i == dh && j == dw) {
                            d_index = k;
                        }

                        road[0].insert(pair<int, int>(i, j));
                        break;
                    }

                    ite = road[k].find(pair<int, int>(i, j - 1));
                    if (ite != road[k].end()) {
                        if (i == ch && j == cw) {
                            c_index = k;
                        } else if (i == dh && j == dw) {
                            d_index = k;
                        }
                        road[0].insert(pair<int, int>(i, j));
                        break;
                    }

                    ite = road[k].find(pair<int, int>(i, j + 1));
                    if (ite != road[k].end()) {
                        if (i == ch && j == cw) {
                            c_index = k;
                        } else if (i == dh && j == dw) {
                            d_index = k;
                        }
                        road[0].insert(pair<int, int>(i, j));
                        break;
                    }

                    ite = road[k].find(pair<int, int>(i + 1, j));
                    if (ite != road[k].end()) {
                        if (i == ch && j == cw) {
                            c_index = k;
                        } else if (i == dh && j == dw) {
                            d_index = k;
                        }
                        road[0].insert(pair<int, int>(i, j));
                        break;
                    }
                }

                road.push_back(set<pair<int, int>>(i, j));
                if (i == ch && j == cw) {
                    c_index = road.size();
                } else if (i == dh && j == dw) {
                    d_index = road.size();
                }
            }
        }
    }

    vector<int> edge(road.size());
    for (int i = 0; i < road.size(); i++) {
        for (int j = i + 1; j < road.size(); i++) {
            for (auto itr = road[i].begin(); itr != road[i].end(); itr++) {
                for (auto itr2 = road[j].begin(); itr2 != road[j].end();
                     itr2++) {
                    if (itr.first - itr2.first < 3 &&
                        itr.second - itr2.second < 3) {
                        edge[i].push_back(j);
                        edge[j].push_back(i);
                        goto OUT;
                    }
                }
            }
        OUT:
        }
    }

    vector<int> dist(road.size(), -1);
    queue<int> que;

    dist[0] = c_index;
    que.push(c_index);

    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for (int i : edge[v]) {
            if (dist[i] != -1) continue;
            dist[i] = dist[v] + 1;
            que.push(i);
        }
    }

    cout << dist[d_index] << endl;*/
}

Measured memory_bytes: 9640