#include <bits/stdc++.h>
#define int int64_t
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define REPP(i, n) for (int i = 1; (i) < (int)(n+1); ++ (i))
#define REPR(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, m, n) for(int i=(m)-1; i>=(n); i--)
#define ALL(v) v.begin(), v.end()
#define SZ(a) int((a).size())
#define SORT(c) sort((c).begin(),(c).end())
#define SORTR(c) sort((c).begin(),(c).end(),greater<int>());
#define BIT(n) (1LL<<(n))
#define PCNT(x) __builtin_popcountll(x)
#define GCD(x,y) __gcd(x,y)
#define LCM(x,y) x/__gcd(x,y)*y
#define PB push_back
#define MP make_pair
#define endl "\n"
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
using namespace std;
using Graph = vector<vector<int> >;
using WeightedGraph = vector<vector<pair<int, int> > >;
typedef long double LD;
typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const int INF = 2000000000;
const long double PI = acosl(-1);
const int MOD = 1000000007;
void print() {cout << endl;}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head; if (sizeof...(tail) != 0) cout << " ";print(forward<Tail>(tail)...);}
template <class T> void print(vector<T> &vec) {for (auto& a : vec) {cout << a; if (&a != &vec.back()) cout << " ";} cout << endl;}
template <class T> void print(vector<vector<T>> &df) { for (auto& vec : df) { print(vec);}}

    int H, W;
    int ch,cw,dh,dw;
std::chrono::system_clock::time_point  start; // 型は auto で可



void bfs(int i, int j, vector<vector<char> > &S, VVI &dp){
    std::chrono::system_clock::time_point  end; // 型は auto で可
    double elapsed = 0;
    deque<PII> que;
    dp[i][j] = 0;
    que.push_front({i,j});
    while (!que.empty() && elapsed < 1950000000) {
        PII v = que.front();
        i = v.first;
        j = v.second;
        que.pop_front();
        
        if (i > 0 && S[i-1][j] == '.' && dp[i][j] < dp[i-1][j]) {
            dp[i-1][j] = dp[i][j];
            que.push_front({i-1,j});
        }
        if (i < H-1 && S[i+1][j] == '.' && dp[i][j] < dp[i+1][j]) {
            dp[i+1][j] = dp[i][j];
            que.push_front({i+1,j});
        }
        if (j > 0 && S[i][j-1] == '.' && dp[i][j] < dp[i][j-1]) {
            dp[i][j-1] = dp[i][j];
            que.push_front({i,j-1});
        }
        if (j < W-1 && S[i][j+1] == '.' && dp[i][j] < dp[i][j+1]) {
            dp[i][j+1] = dp[i][j];
            que.push_front({i,j+1});
        }
        FOR(h,max((int64_t)0,i-2), min(H,i+3)) FOR(w,max((int64_t)0,j-2), min(W,j+3)) {
            if (h == i && w == j || h == i-1 && w == j || h == i+1 && w == j || h == i && w == j-1 || h == i && w == j+1) continue;
            if (S[h][w] == '.' && dp[i][j] + 1 < dp[h][w]) {
                dp[h][w] = dp[i][j] + 1;
                que.push_back({h,w});
            }
        }
        end = std::chrono::system_clock::now();  // 計測終了時間
        elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count(); //処理に要した時間をミリ秒に変換
    }
}




signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    cout << fixed << setprecision(10);
    int ans = 0;
    start = std::chrono::system_clock::now(); // 計測開始時間
    

    cin >> H >> W;

    cin >> ch >> cw >> dh >> dw;
    ch--;cw--;dh--;dw--;
    vector<vector<char> > S(H,vector<char>(W,'#'));
    REP(i,H) REP(j,W) {
        cin >> S[i][j];
    }

    VVI dp(H,VI(W,INF));
    bfs(ch,cw,S,dp);
    if (dp[dh][dw] == INF) dp[dh][dw] = -1; 
    cout << dp[dh][dw] << endl;
    
    
    return 0;
}

/*
cd ~/Desktop/Programming/AtCoder/contest/abc176/abc176_d && sh shellscripts/test_cpp.sh
cd ~/Desktop/Programming/AtCoder/contest/abc176/abc176_d && sh shellscripts/etest_cpp.sh
cd ~/Desktop/Programming/AtCoder/contest/abc176/abc176_d && sh shellscripts/submit_cpp.sh
*/


Measured memory_bytes: 12588