#include <bits/stdc++.h>
using namespace std;

int main() {
    int R, C, sx, sy, ex, ey; 
    cin >> R >> C >> sx >> sy >> ex >> ey;
    string s[R];
    for (int i = 0; i < R; i++) cin >> s[i];

    vector<vector<int>> dist(R, vector<int>(C, -1));
    deque<tuple<int, int, int>> dq;
    dq.emplace_back(sx - 1, sy - 1, 0);
    while (!dq.empty()) {
        int x, y, d; tie(x, y, d) = dq.front();
        dq.pop_front();
        if (x < 0 || x >= R || y < 0 || y >= C || dist[x][y] >= 0 || s[x][y] == '#') continue;
        dist[x][y] = d;

        for (int dx = -2; dx < 3; dx++) for (int dy = -2; dy < 3; dy++) {
            if (dx * dx + dy * dy == 1) dq.emplace_front(x + dx, y + dy, d);
            else if (dx * dx + dy * dy > 1) dq.emplace_back(x + dx, y + dy, d + 1);
        }
    }

    cout << dist[ex - 1][ey - 1];
    return 0;
}