#include <iostream>
#include <vector>
#include <string>
#include <algorithm>  // sort, 二分探索, etc
#include <iomanip>  // setprecision (浮動小数点の出力の誤差)
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <cmath>
#include <list>
#include <assert.h>

using namespace std;

#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=a,i##_len=(b);i<i##_len;++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define cauto const auto&
#define UNIQUE(v) (v).erase( unique(all(v), (v).end() );
#define SZ(x) ((ll)(x).size()) // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(all(x))
#define MIN(x) *min_element(all(x))
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define SUM(v) accumulate((v).begin(), (v).end(), (ll)0)
#define INF 1000000000000
#define MOD 1000000007
#define perm(c) sort(all(c));for(bool c##p=1;c##p;c##p=next_permutation(all(c)))
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }

using ll = long long;
using ull = unsigned long long;

template<class T>bool chmax(T &a, const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a, const T &b){if(a>b){a=b;return true;}return false;}

const char newl = '\n';

ll qp(ll a,ull b){ll ans=1;do{if(b&1u)ans*=a;a*=a;}while(b>>=1u);return ans;}
ll qp(ll a,ull b,ll mod){ll ans=1;do{if(b&1u)ans=ans*a%mod;a=a*a%mod;}while(b>>=1u);return ans;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll comb(int n, int r) {
    vector<vector<ll> > v(n+1, vector<ll>(r+1, 0));
    for(int i=0;i<n+1;i++) {v[i][0]=1; if(i < r+1)v[i][i]=1; }
    for(int i=1;i<n+1;i++) {for(int j=1;j<min(i,r+1);j++) {v[i][j]=v[i-1][j-1]+v[i-1][j];}}
    return v[n][r];
}

// INT_MAX   = 2.1e+09 (2147483647)
// LONG_MAX  = 9.2e+18 (9223372036854775807)
// LLONG_MAX = 9.2e+18 (9223372036854775807)


int main() {
    ll h, w; cin >> h >> w;
    ll ch, cw; cin >> ch >> cw; auto p_start = pair<ll, ll>(ch - 1, cw - 1);
    ll dh, dw; cin >> dh >> dw; auto p_end = pair<ll, ll>(dh - 1, dw - 1);
    vector<vector<bool> > vec(h, vector<bool>(w, false));
    rep(i, h) {
        string s; cin >> s;
        rep(j, w) {
            if (s[j] == '.') {
                vec[i][j] = true;
            }
        }
    }

    set<pair<ll, ll> > searched;
    set<pair<ll, ll> > ps;
    ll cnt = 0;

    while (true) {
        set<pair<ll, ll> > buff;

        // init buff
        if (cnt > 0) {
            set<pair<ll, ll> > tb_searched;
            for (cauto p : ps) {
                // p: 前ループで追加された point
                ll x = p.first; ll y = p.second;
                tb_searched.insert(pair<ll, ll>(x-2, y-2));
                tb_searched.insert(pair<ll, ll>(x-1, y-2));
                tb_searched.insert(pair<ll, ll>(x+0, y-2));
                tb_searched.insert(pair<ll, ll>(x+1, y-2));
                tb_searched.insert(pair<ll, ll>(x+2, y-2));
                tb_searched.insert(pair<ll, ll>(x-2, y-1));
                tb_searched.insert(pair<ll, ll>(x-1, y-1));
                tb_searched.insert(pair<ll, ll>(x+0, y-1));
                tb_searched.insert(pair<ll, ll>(x+1, y-1));
                tb_searched.insert(pair<ll, ll>(x+2, y-1));
                tb_searched.insert(pair<ll, ll>(x-2, y));
                tb_searched.insert(pair<ll, ll>(x-1, y));
                tb_searched.insert(pair<ll, ll>(x+1, y));
                tb_searched.insert(pair<ll, ll>(x+2, y));
                tb_searched.insert(pair<ll, ll>(x-2, y+1));
                tb_searched.insert(pair<ll, ll>(x-1, y+1));
                tb_searched.insert(pair<ll, ll>(x+0, y+1));
                tb_searched.insert(pair<ll, ll>(x+1, y+1));
                tb_searched.insert(pair<ll, ll>(x+2, y+1));
                tb_searched.insert(pair<ll, ll>(x-2, y+2));
                tb_searched.insert(pair<ll, ll>(x-1, y+2));
                tb_searched.insert(pair<ll, ll>(x+0, y+2));
                tb_searched.insert(pair<ll, ll>(x+1, y+2));
                tb_searched.insert(pair<ll, ll>(x+2, y+2));
            }
            for (cauto p : tb_searched) {
                if (p.first >= 0 && p.second >= 0 && p.first < h && p.second < w) {
                    if (vec[p.first][p.second] && searched.find(p) == searched.end()) {
                        buff.insert(p);
                    }
                }
            }
        } else {
            buff.insert(p_start);
        }
        if (buff.empty()) {
            break;
        }


        ps.clear();
        while (true) {
            if (buff.empty()) {
                break;
            }
            auto p = *buff.begin(); buff.erase(buff.begin());  // pop
            if (p == p_end){
                cout << cnt << newl;
                return 0;
            }
        searched.insert(p);
            ps.insert(p);
            // 上
            if (p.first - 1 >= 0) {
                pair<ll, ll> p_new(p.first - 1, p.second);
                if (vec[p_new.first][p_new.second] && searched.find(p_new) == searched.end()) {
                    if (p_new == p_end){
                        cout << cnt << newl;
                        return 0;
                    }
                    buff.insert(p_new);
                    searched.insert(p_new);
                    ps.insert(p_new);
                }
            }
            // 下
            if (p.first + 1 < h) {
                pair<ll, ll> p_new(p.first + 1, p.second);
                if (vec[p_new.first][p_new.second] && searched.find(p_new) == searched.end()) {
                    if (p_new == p_end){
                        cout << cnt << newl;
                        return 0;
                    }
                    buff.insert(p_new);
                    searched.insert(p_new);
                    ps.insert(p_new);
                }
            }
            // 左
            if (p.second - 1 >= 0) {
                pair<ll, ll> p_new(p.first, p.second - 1);
                if (vec[p_new.first][p_new.second] && searched.find(p_new) == searched.end()) {
                    if (p_new == p_end){
                        cout << cnt << newl;
                        return 0;
                    }
                    buff.insert(p_new);
                    searched.insert(p_new);
                    ps.insert(p_new);
                }
            }
            // 上
            if (p.second + 1 < w) {
                pair<ll, ll> p_new(p.first, p.second + 1);
                if (vec[p_new.first][p_new.second] && searched.find(p_new) == searched.end()) {
                    if (p_new == p_end){
                        cout << cnt << newl;
                        return 0;
                    }
                    buff.insert(p_new);
                    searched.insert(p_new);
                    ps.insert(p_new);
                }
            }
        }
        // cout << cnt << newl;
        // for (cauto val : ps) {
        //     cout << '(' << val.first << ", " << val.second << ')' << newl;
        // }

        cnt += 1;
    }

    cout << -1 << newl;


    return 0;
}


Measured memory_bytes: 31384