#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

using Graph = vector<vector<bool> >;
struct Point {
    ll x;
    ll y;
};

ll g_warp_cnt = 10000000000000;
bool seen[1010][1010];
ll cnt[1010][1010];

void dfs(Graph& s, Point& c, ll warp_cnt) {
    const ll dx[24] = {1, 0, -1, 0, 1, 1, -1, -1, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1, 0, 1, 2, 2};
    const ll dy[24] = {0, 1, 0, -1, 1, -1, 1, -1, 0, 1, 2, 2, 2, 2, 2, 1, 0, -1, -2, -2, -2, -2, -2, -1};
    ll x = c.x;
    ll y = c.y;
    ll H = s.size();
    ll W = s[0].size();

    seen[x][y] = true;

    for (ll i = 0; i < 24; i++) {
        ll nx = x + dx[i];
        ll ny = y + dy[i];

        // 場外アウトしたり、移動先が壁の場合はスルー
        if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;

        // 移動先が探索済みの場合
        if (seen[nx][ny]) {
            continue;
        }

        if (!s[nx][ny]) {
            continue;
        }

        if (i >= 4) {
            if (!warp_cnt) {
                continue;
            }
            warp_cnt--;
        }

        // 再帰的に探索
        Point nc = {nx, ny};
        dfs(s, nc, warp_cnt);
    }
}

void bfs(Graph& s, Point& c) {
    const ll dx[4] = {1, 0, -1, 0};
    const ll dy[4] = {0, 1, 0, -1};
    ll x = c.x;
    ll y = c.y;
    ll H = s.size();
    ll W = s[0].size();

    queue<Point> q, visited;
    cnt[x][y] = 0;
    q.push({x, y});

    while (!q.empty() || !visited.empty()) {
        if (!q.empty()) {
            x = q.front().x;
            y = q.front().y;
            visited.push({x, y});
            q.pop();
            for (ll i = 0; i < 4; i++) {
                ll nx = x + dx[i];
                ll ny = y + dy[i];

                // 場外アウトしたり、移動先が壁の場合はスルー
                if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;

                if (!s[nx][ny]) {
                    continue;
                }

                if (cnt[x][y] < cnt[nx][ny]) {
                    cnt[nx][ny] = cnt[x][y];
                    q.push({nx, ny});
                }
            }
        } else {
            x = visited.front().x;
            y = visited.front().y;
            visited.pop();
            for (ll dx = -2; dx <= 2; dx++) {
                for (ll dy = -2; dy <= 2; dy++) {
                    ll nx = x + dx;
                    ll ny = y + dy;

                    // 場外アウトしたり、移動先が壁の場合はスルー
                    if (nx < 0 || nx >= H || ny < 0 || ny >= W) continue;

                    if (!s[nx][ny]) {
                        continue;
                    }

                    if (cnt[x][y] + 1 < cnt[nx][ny]) {
                        cnt[nx][ny] = cnt[x][y] + 1;
                        q.push({nx, ny});
                    }
                }
            }
        }
    }
}

int main() {
    ll h, w;
    Point c, d;
    cin >> h >> w >> c.x >> c.y >> d.x >> d.y;
    Graph s(h, vector<bool>(w));
    for (ll i = 0; i < h; i++) {
        for (ll j = 0; j < w; j++) {
            char sc;
            cin >> sc;
            if (sc == '.') {
                s[i][j] = true;
            } else {
                s[i][j] = false;
            }
        }
    }
    c.x--;
    c.y--;
    d.x--;
    d.y--;

    for (ll i = 0; i < 1010; i++) {
        for (ll j = 0; j < 1010; j++) {
            cnt[i][j] = 1e9;
        }
    }
    bfs(s, c);
    if (cnt[d.x][d.y] != 1e9) {
        cout << cnt[d.x][d.y] << endl;
    } else {
        cout << -1 << endl;
    }
    return 0;
}


Measured memory_bytes: 11880