#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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; }

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};

template< typename T >
struct edge {
    int src, to;
    T cost;
    edge(int to, T cost) : src(-1), to(to), cost(cost) {}
    edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
    edge &operator=(const int &x) {
        to = x;
        return *this;
    }
    operator int() const { return to; }
};
template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename T >
vector< T > dijkstra(WeightedGraph< T > &g, int s) {
    const auto INF = numeric_limits< T >::max();
    vector< T > dist(g.size(), INF);

    using Pi = pair< T, int >;
    priority_queue< Pi, vector< Pi >, greater< Pi > > que;
    dist[s] = 0;
    que.emplace(dist[s], s);
    while(!que.empty()) {
        T cost;
        int idx;
        tie(cost, idx) = que.top();
        que.pop();
        if(dist[idx] < cost) continue;
        for(auto &e : g[idx]) {
            auto next_cost = cost + e.cost;
            if(dist[e.to] <= next_cost) continue;
            dist[e.to] = next_cost;
            que.emplace(dist[e.to], e.to);
        }
    }
    return dist;
}

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);
    int H,W; cin >> H >> W;
    int Ch,Cw; cin >> Ch >> Cw;
    int Dh,Dw; cin >> Dh >> Dw;
    vector<string> masu;
    REP(i,H){
        string s; cin >> s;
        masu.eb(s);
    }
    VVI color(H,VI(W,-1));
    int cnt = 0;
    REP(i,H) REP(j,W){
        if(color[i][j] != -1 or masu[i][j] == '#'){
            continue;
        }

        queue<tuple<int,int,int>> que;
        color[i][j] = cnt;
        que.push(make_tuple(i,j,cnt));
        while(!que.empty()){
            int h,w,c;
            tie(h,w,c) = que.front();
            que.pop();
            REP(d,4){
                int nh = h + dx[d];
                int nw = w + dy[d];
                if(nh < 0 or nh >= H or nw < 0 or nw >= W or masu[nh][nw] == '#') continue;
                if(color[nh][nw] != -1) continue;

                color[nh][nw] = cnt;
                que.push(make_tuple(nh,nw,cnt));
            }
        }

        cnt++;

    }

    //REP(i,H){
    //    REP(j,W){
    //        cout << color[i][j] << " ";
    //    }
    //    cout << endl;
    //}

    WeightedGraph<int> g;
    g.resize(cnt);
    REP(i,H) REP(j,W){
        if(color[i][j] == -1) continue;
        FOR(h,-2,3) FOR(w,-2,3){
            int nh = i + h;
            int nw = j + w;
            if(nh < 0 or nh >= H or nw < 0 or nw >= W or color[nh][nw] == -1 or color[nh][nw] == color[i][j]) continue;

            int sc = color[i][j];
            int dc = color[nh][nw];
            g[sc].eb(dc,1);
            g[dc].eb(sc,1);
        }
    }

    auto res = dijkstra(g,color[Ch-1][Cw-1]);
    int dst = color[Dh-1][Dw-1];
    //dump(dst)
    if(res[dst] > INF) cout << -1 << endl;
    else cout << res[dst] << endl;
}

Measured memory_bytes: 73956