#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <complex>
#include <cstring>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

constexpr int dx[4] = {1, 0, -1, 0};
constexpr int dy[4] = {0, 1, 0, -1};
constexpr int dx2[4] = {1, -1, -1, 1};
constexpr int dy2[4] = {1, 1, -1, -1};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int H, W;
    cin >> H >> W;
    vector<int> X(2), Y(2);
    for(int i = 0; i < 2; ++i){
        cin >> X[i] >> Y[i];
        --X[i], --Y[i];
    }
    vector<string> s(H);
    for(int i = 0; i < H; ++i)  cin >> s[i];
    deque<tuple<int, int, int>> deq;
    deq.emplace_back(0, X[0], Y[0]);
    vector<vector<int>> dist(H, vector<int>(W, INF));
    dist[X[0]][Y[0]] = 0;
    while(!deq.empty()){
        int d, x, y;
        tie(d, x, y) = deq.front();
        deq.pop_front();
        if(d > dist[x][y])  continue;
        for(int i = 0; i < 4; ++i){
            int nx = x + dx[i], ny = y + dy[i];
            if(nx < 0 || nx >= H || ny < 0 || ny >= W)  continue;
            if(s[nx][ny] == '#')    continue;
            if(chmin(dist[nx][ny], dist[x][y])){
                deq.emplace_front(dist[nx][ny], nx, ny);
            }
        }
        for(int i = 0; i < 4; ++i){
            int cx = x + dx2[i], cy = y + dy2[i];
            for(int j = 0; j < 4; ++j){
                int nx = cx + dx[j], ny = cy + dy[j];
                if(nx < 0 || nx >= H || ny < 0 || ny >= W)  continue;
                if(s[nx][ny] == '#')    continue;
                if(chmin(dist[nx][ny], dist[x][y] + 1)){
                    deq.emplace_back(dist[nx][ny], nx, ny);
                }
            }
            for(int j = 0; j < 4; ++j){
                int nx = cx + dx2[j], ny = cy + dy2[j];
                if(nx < 0 || nx >= H || ny < 0 || ny >= W)  continue;
                if(s[nx][ny] == '#')    continue;
                if(chmin(dist[nx][ny], dist[x][y] + 1)){
                    deq.emplace_back(dist[nx][ny], nx, ny);
                }
            }
            if(cx < 0 || cx >= H || cy < 0 || cy >= W)  continue;
            if(s[cx][cy] == '#')    continue;
            if(chmin(dist[cx][cy], dist[x][y] + 1)){
                deq.emplace_back(dist[cx][cy], cx, cy);
            }
        }
    }
    // for(int i = 0; i <= H; ++i){
    //     for(int j = 0; j <= W; ++j){
    //         if(dist[i][j] == INF)   cerr << "x ";
    //         else    cerr << dist[i][j] << ' ';
    //     } cerr << '\n';
    // }
    if(dist[X[1]][Y[1]] == INF) cout << -1 << endl;
    else    cout << dist[X[1]][Y[1]] << endl;
}

Measured memory_bytes: 8644