#include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <deque>
#include <complex>
#include <bitset>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<long long, long long> pll;
typedef vector<pll> vpll;

typedef long double ld;
typedef vector<ld> vld;

typedef vector<bool> vb;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define rreps(i, n) for (ll i = (n); i >= 1; i--)
#define all(v) (v).begin(), (v).end()

template <class T> void chmin(T& a, T b) { a = min(a, b);}
template <class T> void chmax(T& a, T b) { a = max(a, b);}

constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);

void solve() {
    ll h, w;
    cin >> h >> w;
    ll ch, cw;
    cin >> ch >> cw;
    ch--, cw--;
    ll dh, dw;
    cin >> dh >> dw;
    dh--, dw--;
    vector<string> s(h);
    rep(i, h) cin >> s[i];
    vvll d(h, vll(w, -1));
    bool updated = true;
    typedef tuple<ll, ll, ll> tup;
    queue<tup> que;
    queue<tup> que2;
    queue<tup> que3;
    que.push(make_tuple(ch, cw, 0));
    que2.push(make_tuple(ch, cw, 0));
    d[ch][cw] = 0;
    while(updated) {
        updated = false;
        while(!que3.empty()) {
            tup t = que3.front();
            que3.pop();
            que.push(t);
            que2.push(t);
        }
        while(!que.empty()) {
            ll di[] = {1, -1, 0, 0};
            ll dj[] = {0, 0, 1, -1};
            ll ci, cj, cd;
            tie(ci, cj, cd) = que.front();
            que.pop();
            for(int i = 0; i < 4; ++i) {
                ll ni = ci + di[i];
                ll nj = cj + dj[i];
                if(ni < 0 || ni >= h) continue;
                if(nj < 0 || nj >= w) continue;
                if(s[ni][nj] == '#') continue;
                if(d[ni][nj] != -1) continue;
                d[ni][nj] = cd;
                que.push(make_tuple(ni, nj, cd));
                que2.push(make_tuple(ni, nj, cd));
            }
        }
        /*
        cout << "test" << endl;
        rep(i, 4) {
            rep(j, 4) cout << d[i][j] << ' ';
            cout << endl;
        }
        */
        while(!que2.empty()) {
            ll ci, cj, cd;
            tie(ci, cj, cd) = que2.front();
            que2.pop();
            for(int di = -2; di <= 2; ++di) {
                ll ni = ci + di;
                if(ni < 0 || ni >= h) continue;
                for(int dj = -2; dj <= 2; ++dj) {
                    if(di == 0 && dj == 0) continue;
                    ll nj = cj + dj;
                    if(nj < 0 || nj >= w) continue;
                    if(s[ni][nj] == '#') continue;
                    if(d[ni][nj] != -1) continue;
                    d[ni][nj] = cd + 1;
                    que3.push(make_tuple(ni, nj, cd + 1));
                    updated = true;
                }
            }
        }
    }
    cout << d[dh][dw] << endl;
    return;
}

int main() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    solve();
}

Measured memory_bytes: 12672