#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 1004;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
vector<int> dxA = {0, 0, 1, -1};
vector<int> dyA = {1, -1, 0, 0};
vector<int> dxB = {-2,-2,-2,-2,-2,-1,-1,-1,-1,0,0,1,1,1,1,2,2,2,2,2};
vector<int> dyB = {-2,-1,0,1,2,-2,-1,1,2,-2,2,-2,-1,1,2,-2,-1,0,1,2};
vector<vector<int>> color(MAX, vector<int>(MAX, -1));
vector<vector<int>> dist(MAX, vector<int>(MAX, INF));
class Map {
public:
    int h, w;
    int sx, sy, gx, gy;
    vector<vector<int>> maze;

    Map (int h, int w, int sx, int sy, int gx, int gy)
        : h(h), w(w), sx(sx), sy(sy), gx(gx), gy(gy) {
        maze.resize(h, vector<int>(w));
    }
    void input() {
        for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) {
            char c; cin >> c;
            if (c == '#') maze[i][j] = 0;
            else maze[i][j] = 1;
        }
    }
    bool canMove(int x, int y) {
        if (x < 0 || h <= x || y < 0 || w <= y) return false;
        if (maze[x][y] == 0) return false;
        return true;
    }

    void bfsCol(int s, int t) {
        queue<pii> q;
        q.emplace(s, t);
        while (!q.empty()) {
            pii p = q.front();
            q.pop();
            int x = p.first, y = p.second;
            // moveA
            for (int i = 0; i < dxA.size(); i++) {
                int nx = x + dxA[i], ny = y + dyA[i];
                if (!canMove(nx, ny)) continue;
                if (color[nx][ny] == NIL) {
                    color[nx][ny] = color[x][y];
                    q.emplace(nx, ny);
                }
            }
        }
    }
    int bfsB() {
        queue<pii> q;
        q.emplace(sx, sy);
        dist[sx][sy] = 0;
        while (!q.empty()) {
            pii p = q.front();
            q.pop();
            int x = p.first, y = p.second;
            // moveB
            for (int dx = -2; dx <= 2; dx++) { for (int dy = -2; dy <= 2; dy++) {
                if (dx == 0 && dy == 0) continue;
                int nx = x + dx, ny = y + dy;
                if (!canMove(nx, ny)) continue;
                if (color[x][y] == color[nx][ny]) {
                    if (dist[nx][ny] > dist[x][y]) {
                        dist[nx][ny] = dist[x][y];
                        q.emplace(nx, ny);
                    }
                }
                else {
                    if (dist[nx][ny] > dist[x][y] + 1) {
                        dist[nx][ny] = dist[x][y] + 1;
                        q.emplace(nx, ny);
                    }
                }
            }
            }
        }
        return dist[gx][gy];
    }
    void drawColor() {
        int curColor = 0;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (maze[i][j] == 1 && color[i][j] == -1) {
                    color[i][j] = curColor++;
                    bfsCol(i, j);
                }
            }
        }
    }
};

/* function */
/* main */
int main(){
    int h, w, sx, sy, gx, gy;
    cin >> h >> w >> sx >> sy >> gx >> gy;
    sx--, sy--, gx--, gy--;
    Map mp = Map(h, w, sx, sy, gx, gy);
    mp.input();
    mp.drawColor();
    int ans = mp.bfsB();
    if (ans == INF) cout << -1 << '\n';
    else cout << ans << '\n';
}


Measured memory_bytes: 15448