#include <iostream>
#include <iomanip>
#include <algorithm>
#include <assert.h>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define rrep(i,a) for(ll i=ll(a-1);i>=0;--i)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}

const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll EPS = 1e-10;
int sgn(const double a){
    return (a < -EPS ? -1 : (a > EPS ? +1 : 0));
}
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> tri;
typedef pair<double,double> point;
typedef complex<double> Point;
const ll MAX = 30000;
constexpr ll nx[5] = {-2,-1,0,1,2};
constexpr ll ny[5] = {-2,-1,0,1,2};
constexpr ll nnx[4] = {-1,0,1,0};
constexpr ll nny[4] = {0,-1,0,1};
class Dijkstra{
    private:
        struct edge{
            ll to,cost;
        };
        vector<vector<edge>> G;
        vector<ll> d;
        ll siz;
    public:
        Dijkstra(ll n){
            G.resize(n);
            siz = n;
        }
        void add_edge(ll from,ll to,ll cost){
            G[from].push_back(edge{to,cost});
        }
        vector<ll> dijkstra(ll s){
            priority_queue<P,vector<P>,greater<P>> q;
            d.assign(siz,INF);
            d[s] = 0;
            q.push(P(0,s));
            while(!q.empty()){
                P p = q.top();q.pop();
                ll v = p.second;
                if (d[v] < p.first) continue;
                for(edge &e:G[v]){
                    if (chmin(d[e.to],d[v]+e.cost)){
                        q.push(P(d[e.to],e.to));
                    }
                }
            }
            return d;
        }
};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll h,w;
    cin >> h >> w;
    ll c1,d1,c2,d2;
    cin >> c1 >> d1 >> c2 >> d2;
    vector<string> grid(h);
    vector<vector<ll>> ver(h,vector<ll>(w));
    rep(i,h) cin >> grid[i];
    ll V = 0;
    rep(i,h){
        rep(j,w){
            if (grid[i][j] == '.'){
                ver[i][j] = V;
                V++;
            }
        }
    }
    Dijkstra g(V);
    rep(i,h){
        rep(j,w){
            if (grid[i][j] == '#') continue;
            rep(a,5){
                rep(b,5){
                    if (nx[a] == 0 && ny[b] == 0) continue;
                    ll xn = i+nx[a],yn = j+ny[b];
                    if (xn >= 0 && xn < h && yn >= 0 && yn < w && grid[xn][yn] == '.'){
                        g.add_edge(ver[i][j],ver[xn][yn],1);
                    }
                }
            }
            rep(a,4){
                ll xn = i+nnx[a],yn = j+nny[a];
                if (xn >= 0 && xn < h && yn >= 0 && yn < w && grid[xn][yn] == '.'){
                    g.add_edge(ver[i][j],ver[xn][yn],0);
                }
            }
        }
    }
    auto d = g.dijkstra(ver[c1-1][d1-1]);
    if (d[ver[c2-1][d2-1]] == INF) PRINT(-1);
    else PRINT(d[ver[c2-1][d2-1]]);
}



Measured memory_bytes: 187584