#include <iostream>
#include <array>
#include <vector>
#include <string>
#include <set>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

int main() {
    int h, w, ch, cw, dh, dw;
    cin >> h >> w;
    cin >> ch >> cw;
    cin >> dh >> dw;

    vector<string> s(h);
    rep(i, h) cin >> s[i];

    vector<vector<int>> move = {{1,0}, {-1,0}, {0,1}, {0,-1}};
    vector<vector<int>> warp;

    rep(i, 5) {
        rep(j, 5) {
            if(abs(i-2) + abs(j-2) > 1) warp.push_back({i-2,j-2});
        }
    }

    vector<vector<int>> d(h, vector(w, INT_MAX));
    d[ch-1][cw-1] = 0;
    vector<vector<int>> que = {{ch-1, cw-1, 0}};

    while (!que.empty()) {
        int x = que[0][0], y = que[0][1], z = que[0][2];
        que.erase(que.begin());

        for(auto ij : move) {
            int xx = x + ij[0], yy = y + ij[1];
            if (xx < 0 || h-1 < xx || yy < 0 || w-1 < yy) continue;
            if (s[xx][yy] == '.' && d[xx][yy] > z) {
                d[xx][yy] = z;
                que.insert(que.begin(), {xx, yy, z});
            }
        }

        for(auto ij : warp) {
            int xx = x + ij[0], yy = y + ij[1];
            if (xx < 0 || h-1 < xx || yy < 0 || w-1 < yy) continue;
            if (s[xx][yy] == '.' && d[xx][yy] > z + 1) {
                d[xx][yy] = z + 1;
                que.push_back({xx, yy, z+1});
            }
        }
    }

    cout << (d[dh-1][dw-1] == INT_MAX ? -1 : d[dh-1][dw-1]) << endl;
}