#include <iostream>
#include <string>
#include <vector>
#include <queue>


using namespace std;

int H, W;
int CH, CW;
int DH, DW;
vector<string> S;

int main() {
    cin >> H >> W;
    cin >> CH >> CW;
    cin >> DH >> DW;
    for (int i = 0; i < H; ++i) {
        string s;
        cin >> s;
        S.push_back(s);
    }

    CH--;
    CW--;
    DH--;
    DW--;

    int cost[H][W] = {10000};
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            cost[i][j] = 10000;
        }
    }
    cost[CH][CW] = 0;

    priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >, greater<pair<int, pair<int, int> > > > Q;
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            if (S[i][j] == '.')
                Q.push({cost[i][j], {i, j}});
        }
    }
    // Q.push({0, {CH, CW}});

    while (!Q.empty()) {
        auto t = Q.top();
        Q.pop();
        int i = t.second.first; 
        int j = t.second.second;
// cout << "!" << i << "," << j << " " << cost[i][j] << " " << t.first << endl;
//    for (int i = 0; i < H; ++i) {
//         for (int j = 0; j < W; ++j) {
//             cout << cost[i][j] << " ";
//         }
//         cout << endl;
//     }

        vector<pair<int, pair<int, int> > > next;
        for (int di = -2; di <= 2; ++di) {
            for (int dj = -2; dj <= 2; ++dj) {
                int c;
                if (di == 0 && dj == 0) continue;
                else if (di == 0 && abs(dj) == 1) c = 0;
                else if (abs(di) == 1 && dj == 0) c = 0;
                else c = 1;

// cout << "-" << i + di << "," << j + dj << endl;
                if (i + di >= 0 && i + di < H && j + dj >= 0 && j + dj < W && S[i + di][j + dj] == '.') {
// cout << "+" << i + di << "," << j + dj << " " << c << endl;
                    next.push_back({c, {i + di, j + dj}});
                }
            }
        }
        for (auto t : next) {
            int c = t.first;
            int i2 = t.second.first;
            int j2 = t.second.second;

            int alt = cost[i][j] + c;
            if (cost[i2][j2] > alt) {
// cout << "$" << i2 << "," << j2 << " " << alt << endl;
                cost[i2][j2] = alt;
                Q.push({alt, {i2, j2}});
            }
        }
    }
 
    // for (int i = 0; i < H; ++i) {
    //     for (int j = 0; j < W; ++j) {
    //         cout << cost[i][j] << " ";
    //     }
    //     cout << endl;
    // }

    int ans = cost[DH][DW];
    if (ans == 10000) ans = -1;
    cout << ans << endl;
}