#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
//cout << setprecision(20) << 

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

int main() {
    int h, w; cin >> h >> w;
    int sh, sw; cin >> sh >> sw;
    sh--; sw--;
    int gh, gw; cin >> gh >> gw;
    gh--; gw--;
    vector<string> G(h);
    // sh = 0; sw = 0; gh = h - 1; gw = w - 1;
    // cout << sh << "," << sw << " " << gh << "," << gw << endl;
    for (int i = 0; i < h; ++i) {
        cin >> G[i];
    }
    // if (G[sh][sw] == '#' || G[gh][gw] == '#') return 0;
    // for (int i = 0; i < h; ++i) {
    //     cout << G[i] << endl;
    // }

    // ワープ使用回数のメモ
    vector<vector<int> > dist(h, vector<int>(w, -1));
    dist[sh][sw] = 0;

    queue<pair<int, int> > islandQue;
    islandQue.push(make_pair(sh, sw));

    while (!islandQue.empty()) {
        pair<int, int> front = islandQue.front();
        int x = front.first;
        int y = front.second;
        // cout << "islandque" << endl;
        // cout << x << " " << y << endl;
        // usleep(1 * 1000000);
        islandQue.pop();

        queue<pair<int, int> > searchQue;
        searchQue.push(front);
        queue<pair<int, int> > seenQue;
        seenQue.push(front);

        while (!searchQue.empty()) {
            pair<int, int> searchFront = searchQue.front();
            int curX = searchFront.first;
            int curY = searchFront.second;
            searchQue.pop();

            for (int direction = 0; direction < 4; direction++) {
                int next_h  = curX + dx[direction];
                int next_w  = curY + dy[direction];
                if (next_h < 0 || next_h >= h || next_w < 0 || next_w >= w) continue;
                if (G[next_h][next_w] == '#') continue;

                // 未探索なら
                if (dist[next_h][next_w] == -1 || dist[next_h][next_w] != dist[x][y]) {
                    dist[next_h][next_w] = dist[x][y];
                    searchQue.push(make_pair(next_h, next_w));
                    seenQue.push(make_pair(next_h, next_w));
                }
            }
        }


        // ワープ
        while (!seenQue.empty()) {
            pair<int, int> warpStart = seenQue.front();
            seenQue.pop();
            for (int i = -2; i <= 2; ++i) {
                for (int j = -2; j <= 2; ++j) {
                    int next_h = warpStart.first + i;
                    int next_w = warpStart.second + j;
                    if (next_h < 0 || next_h >= h || next_w < 0 || next_w >= w) continue;
                    if (G[next_h][next_w] == '#') continue;

                    // 未探索，または更新できるなら追加
                    if (dist[next_h][next_w] == -1 || dist[next_h][next_w] > dist[x][y] + 1) {
                        islandQue.push(make_pair(next_h, next_w));
                        dist[next_h][next_w] = dist[x][y] + 1;
                    }
                }
            }
        }
    }
    // for (auto i: dist) {
    //     for (auto j: i) {
    //         if (j == -1) cout << "# ";
    //         else 
    //             cout << j << " ";
    //     }
    //     cout << endl;
    // }

    cout << dist[gh][gw] << endl;
}



Measured memory_bytes: 9452