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

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define REPr(i, n) for(int i = (n)-1; i >= 0; --i)
#define FORq(i, m, n) for(int i = (m); i <= (n); ++i)
#define FORqr(i, m, n) for(int i = (n); i >= (m); --i)
#define MP make_pair
#define SIN(x, S) (S.count(x) != 0)
#define M0(x) memset(x, 0, sizeof(x))
#define FILL(x, y) memset(x, y, sizeof(x))
#define MM(x) memset(x, -1, sizeof(x))
#define ALL(x) (x).begin(), (x).end()
#define DB(x) cerr << #x << " = " << x << endl
#define DB2(x, y)                                                              \
    cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n";
#define DEBUG                                                                  \
    int x12345;                                                                \
    cin >> x12345;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef long long ll;
typedef long long integer;
const long long MOD = 1e9 + 7;
///////////////////////////////////////////////
// for template
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
///////////////////////////////////////////////
/// 🍈( '-' 🍈 |AC|
const ll M = 1e9;
const ll MAX = 1e16;
char grid[1002][1002];
bool visited[1002][1002];
ll cost[1002][1002];
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};
int ddx[20] = {-2,-1,0,1,2,2,2,2,2,1,0,-1,-2,-2,-2,-2,-1,-1,1,1};
int ddy[20] = {-2,-2,-2,-2,-2,-1,0,1,2,2,2,2,2,1,0,-1,-1,1,1,-1};

int main() {
    int H,W; cin >> H >> W;
    int Ch,Cw,Dh,Dw;
    cin >> Ch >> Cw >> Dh >> Dw;
    REP(i,H){
        REP(j,W){
            cin >> grid[i][j];
        }
    }
    Ch--; Cw--; Dh--; Dw--;
    REP(i,H){REP(j,W){cost[i][j] = MAX;}}
    cost[Ch][Cw] = 0;
    deque<PII> Q;
    Q.push_front(MP(Ch,Cw));
    while(!Q.empty()){
        PII now = Q.front();
        Q.pop_front();
        visited[now.first][now.second] = true;
        REP(k,4){
            int newx = now.first + dx[k];
            int newy = now.second + dy[k];
            if (newx < 0) continue;
            if (newx >= H) continue;
            if (newy < 0) continue;
            if (newy >= W) continue;
            if (grid[newx][newy] == '#') continue;
            if (visited[newx][newy]) continue;
            cost[newx][newy] = cost[now.first][now.second];
            Q.push_front(MP(newx,newy));
        }
        for (int ddx = -2; ddx <= 2; ddx++){
            for (int ddy = -2; ddy <= 2; ddy++){
                int newx = now.first + ddx;
                int newy = now.second + ddy;
                if (newx < 0) continue;
                if (newx >= H) continue;
                if (newy < 0) continue;
                if (newy >= W) continue;
                if (grid[newx][newy] == '#') continue;
                if (visited[newx][newy]) continue;
                if (cost[newx][newy] <= cost[now.first][now.second] + 1) continue;
                cost[newx][newy] = cost[now.first][now.second] + 1;
                Q.push_back(MP(newx,newy));
            }
        }
    }

    if (cost[Dh][Dw] == MAX){
        cout << -1 << endl;
    }else{
        cout << cost[Dh][Dw] << endl;
    }

}

Measured memory_bytes: 13368