/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Gosu_Hiroo
 */

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using VI = vector<int>;
using VLL = vector<long long>;
using PII = pair<int, int>;
using PLL = pair<long long, long long>;

//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast")

//#define G(size_1) vector<vector<int>>(size_1, vector<int>())
#define SZ(x) ((int)(x).size())
#define READ ({ll t;cin >> t;t;})

#define FOR(i, _begin, _end) for (__typeof(_end) end = _end, begin = _begin,  i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define REP(i, end) for (__typeof(end) i = 0, _len = (end); i < (_len); i += 1)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define F first
#define S second
#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define MOD(x, m) ((((x) % (m)) + (m)) % (m))
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define EB emplace_back
#define PB push_back
#define fcout cout << fixed << setprecision(12)
#define fcerr cerr << fixed << setprecision(12)
#define print(x) cout << (x) << '\n'
#define printE(x) cout << (x) << endl
#define fprint(x) cout << fixed << setprecision(12) << (x) << endl
# define BYE(a) do { cout << (a) << endl; return ; } while (false)

#ifdef DEBUG
#define DBG(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); _err(cerr,_it, args); }
#define ERR(x) std::cerr << #x << " = " << x << endl;
#else
#define DBG(args...) {};
#define ERR(x) {};
#endif
void _err(std::ostream& cerr,istream_iterator<string> it) {cerr << endl;}
template<typename T, typename... Args>
void _err(std::ostream& cerr, istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << "  ";
    _err(cerr,++it, args...);
}

const double pi = 2 * acos(.0);
const int inf = 0x3f3f3f3f;
const ll mod = (ll) (1e9) + 7;

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>
istream& operator >> (istream& is, vector<T>& V) {
    for(auto&& ele : V)is >> ele;
    return is;
}

template <typename T>
ostream& operator << (ostream& os, const vector<T> V) {
    os << "[";
    int cnt = 0;
    T curr;
    if(!V.empty()){
        for (int i = 0; i < V.size() - 1; ++i) {
            if(V[i] == curr)cnt ++;
            else cnt = 0;
            if(cnt == 4)os << "... ";
            if(cnt < 4)
                os << i << ":" << V[i] << " ";
            curr = V[i];
        }
        os << V.size() - 1 << ":" << V.back();
    }
    os << "]\n";
    return os;
}

template <typename T, typename U>
ostream& operator << (ostream& os, const pair<T,U> P) {
    os << "(";
    os << P.first << "," << P.second;
    os << ")";
    return os;
}

template <typename T, typename U>
ostream& operator << (ostream& os, const set<T,U> V) {
    os << "{";
    if(!V.empty()){
        auto it = V.begin();
        for (int i = 0; i < V.size() -1; ++i) {
            os << *it << " ";
            it++;
        }
        os << *it;
    }
    os << "}\n";
    return os;
}

template <typename K, typename H, typename P>
ostream& operator << (ostream& os, const unordered_set<K, H, P> V) {
    os << "{";
    if(!V.empty()){
        auto it = V.begin();
        for (int i = 0; i < V.size() -1; ++i) {
            os << *it << " ";
            it++;
        }
        os << *it;
    }
    os << "}\n";
    return os;
}

template <typename K, typename C>
ostream& operator << (ostream& os, const multiset<K, C> V) {
    os << "{";
    if(!V.empty()){
        auto it = V.begin();
        for (int i = 0; i < V.size() -1; ++i) {
            os << *it << " ";
            it++;
        }
        os << *it;
    }
    os << "}";
    return os;
}

template <typename K, typename T, typename C>
ostream& operator << (ostream& os, const map<K,T,C> V) {
    os << "{";
    if(!V.empty()){
        auto it = V.begin();
        for (int i = 0; i < V.size() -1; ++i) {
            os << "(";
            os << it->first << "," << it->second;
            os << ") ";
            it++;
        }
        os << "(";
        os << it->first << "," << it->second;
        os << ")";
    }
    os << "}\n";
    return os;
}

template <typename K, typename T, typename C>
ostream& operator << (ostream& os, const unordered_map<K,T,C> V) {
    os << "{";
    if(!V.empty()){
        auto it = V.begin();
        for (int i = 0; i < V.size() -1; ++i) {
            os << "(";
            os << it->first << "," << it->second;
            os << ") ";
            it++;
        }
        os << "(";
        os << it->first << "," << it->second;
        os << ")";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator << (ostream& os, const deque<T> V) {
    os << "[";
    if (!V.empty()) {
        for (int i = 0; i < V.size() - 1; ++i) {
            os << V[i] << "->";
        }
        if (!V.empty())os << V.back();
    }
    os << "]\n";
    return os;
};

template <typename T, typename Cont, typename Comp>
ostream& operator << (ostream& os, const priority_queue<T, Cont, Comp> V) {
    priority_queue<T, Cont, Comp> _V = V;
    os << "[";
    if(!_V.empty()){
        while(_V.size() > 1){
            os << _V.top() << "->";
            _V.pop();
        }
        os << _V.top();
    }
    os << "]\n";
    return os;
};

template <class F>
struct y_combinator {
    F f; // the lambda will be stored here

    // a forwarding operator():
    template <class... Args>
    decltype(auto) operator()(Args&&... args) const {
        // we pass ourselves to f, then the arguments.
        // the lambda should take the first argument as `auto&& recurse` or similar.
        return f(*this, std::forward<Args>(args)...);
    }
};
// helper function that deduces the type of the lambda:
template <class F>
y_combinator<std::decay_t<F>> recursive(F&& f){
return {std::forward<F>(f)};
}

struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }

};

template <typename T, typename U>
std::vector<T> multi_vector(int n, U v) {
    return std::vector<T>(n, v);
}

template <typename U, typename... Args>
auto multi_vector(int n, Args... args) {
    auto val = multi_vector<U>(std::forward<Args>(args)...);
    return std::vector<decltype(val)>(n, std::move(val));
}
template<typename _Key, typename _Tp> class Fibonacci_Heap
{
public:
    using data_type = pair<_Key, _Tp>;
    class node
    {
    public:
        data_type _data;
        unsigned short int _child;
        bool _mark;
        node *_par, *_prev, *_next, *_ch_last;
        node(data_type&& data) : _data(move(data)),
                                 _child(0), _par(nullptr), _prev(nullptr), _next(nullptr), _ch_last(nullptr){}
        inline const _Key& get_key() const noexcept { return _data.first; }
        void insert(node *cur){
            if(_ch_last) insert_impl(cur, _ch_last);
            else _ch_last = cur, _ch_last->_prev = _ch_last->_next = _ch_last;
            ++_child, cur->_par = this;
        }
        void erase(node *cur){
            if(cur == cur->_prev){
                _ch_last = nullptr;
            }else{
                erase_impl(cur);
                if(cur == _ch_last) _ch_last = cur->_prev;
            }
            --_child, cur->_par = nullptr;
        }
    };

private:
    static constexpr float FACTOR = 1.0f / log2((1.0f + sqrt(5.0f)) / 2.0f);
    size_t _size;
    node *_minimum;
    vector<node*> rank;

    static void insert_impl(node *cur, node *next){
        cur->_prev = next->_prev, cur->_next = next;
        cur->_prev->_next = cur, next->_prev = cur;
    }
    static void erase_impl(node *cur){
        cur->_prev->_next = cur->_next, cur->_next->_prev = cur->_prev;
    }
    static int ceil2(int u){
        return 32 - __builtin_clz(u);
    }
    void root_insert(node *cur){
        if(_minimum){
            insert_impl(cur, _minimum);
            if(cur->get_key() < _minimum->get_key()) _minimum = cur;
        }else{
            _minimum = cur, _minimum->_prev = _minimum->_next = _minimum;
        }
    }
    void root_erase(node *cur){
        if(cur == cur->_prev) _minimum = nullptr;
        else erase_impl(cur);
    }
    void _delete(node *cur){
        root_erase(cur);
        delete cur;
    }
    template<typename Key, typename Data>
    node *_push(Key&& key, Data&& data){
        ++_size;
        data_type new_data(forward<Key>(key), forward<Data>(data));
        node* new_node = new node(move(new_data));
        root_insert(new_node);
        return new_node;
    }
    void _pop(){
        assert(_size > 0);
        --_size;
        if(_size == 0){
            _delete(_minimum);
            return;
        }
        if(_minimum->_ch_last){
            for(node *cur = _minimum->_ch_last->_next;;){
                node *next = cur->_next;
                _minimum->erase(cur), root_insert(cur);
                if(!_minimum->_ch_last) break;
                cur = next;
            }
        }
        unsigned int max_rank = ceil(ceil2(_size) * FACTOR);
        node *next_minimum = _minimum->_next;
        if(rank.size() <= max_rank) rank.resize(max_rank + 1);
        for(node*& cur : rank) cur = nullptr;
        for(node *cur = next_minimum; cur != _minimum;){
            if(cur->get_key() < next_minimum->get_key()) next_minimum = cur;
            node *next = cur->_next;
            unsigned int deg = cur->_child;
            while(rank[deg]){
                if(cur->get_key() < rank[deg]->get_key() || cur == next_minimum){
                    root_erase(rank[deg]), cur->insert(rank[deg]);
                }else{
                    root_erase(cur), rank[deg]->insert(cur);
                    cur = rank[deg];
                }
                rank[deg++] = nullptr;
            }
            rank[deg] = cur;
            cur = next;
        }
        _delete(_minimum);
        _minimum = next_minimum;
    }
    void _update(node *cur, const _Key& key){
        assert(!(key < (_Key)0));
        node *change = ((cur->_data.first -= key) < _minimum->get_key()) ? cur : nullptr;
        if(!cur->_par || !(cur->get_key() < cur->_par->get_key())){
            if(change) _minimum = change;
            return;
        }
        while(true){
            node *next = cur->_par;
            next->erase(cur), root_insert(cur);
            cur->_mark = false, cur = next;
            if(!cur->_par) break;
            if(!cur->_mark){
                cur->_mark = true;
                break;
            }
        }
        if(change) _minimum = change;
    }
    void clear_dfs(node *cur){
        if(cur->_ch_last){
            for(node *_cur = cur->_ch_last->_next;;){
                node *next = _cur->_next;
                if(_cur == cur->_ch_last){
                    clear_dfs(_cur);
                    break;
                }else{
                    clear_dfs(_cur);
                }
                _cur = next;
            }
        }
        delete cur;
        return;
    }
    void _clear(){
        if(!_minimum) return;
        for(node *cur = _minimum->_next;;){
            node *next = cur->_next;
            if(cur == _minimum){
                clear_dfs(cur);
                break;
            }else{
                clear_dfs(cur);
            }
            cur = next;
        }
    }

public:
    Fibonacci_Heap() noexcept : _size(0u), _minimum(nullptr){}
    Fibonacci_Heap(const Fibonacci_Heap&) = delete;
    Fibonacci_Heap(Fibonacci_Heap&& another)
            : _size(move(another._size)), rank(move(another.rank)){
        _minimum = another._minimum, another._minimum = nullptr;
    }
    Fibonacci_Heap& operator=(const Fibonacci_Heap&) = delete;
    Fibonacci_Heap& operator=(Fibonacci_Heap&& another){
        _size = move(another._size), rank = move(another.rank);
        _clear(), _minimum = another._minimum, another._minimum = nullptr;
    }
    // ~Fibonacci_Heap(){ _clear(); }
    inline bool empty() const noexcept { return (_size == 0); }
    inline size_t size() const noexcept { return _size; }
    inline const data_type& top() const noexcept { return _minimum->_data; }
    template<typename Key, typename Data>
    node *push(Key&& key, Data&& data){ return _push(forward<Key>(key), forward<Data>(data)); }
    void pop(){ _pop(); }
    void update(node *cur, const _Key& key){ _update(cur, key); }
    void clear(){ _clear(); _size = 0; rank.~vector<node*>(); }
    friend Fibonacci_Heap *unite(Fibonacci_Heap *fh1, Fibonacci_Heap *fh2){
        if(fh2->_size == 0){
            fh2->~Fibonacci_Heap();
            return fh1;
        }
        if(fh1->_size == 0){
            fh1->~Fibonacci_Heap();
            return fh2;
        }
        fh1->_minimum->_prev->_next = fh2->_minimum->_next;
        fh2->_minimum->_next->_prev = fh1->_minimum->_prev;
        fh2->_minimum->_next = fh1->_minimum;
        fh1->_minimum->_prev = fh2->_minimum;
        fh1->_size += fh2->_size;
        if(fh2->_minimum->get_key() < fh1->_minimum->get_key()) fh1->_minimum = fh2->_minimum;
        fh2->~Fibonacci_Heap();
        return fh1;
    }

    // DEBUG 用
    int dfs(node *cur, bool print, int depth) const {
        if(!cur->_ch_last){
            assert(cur->_child == 0);
            return 1;
        }
        int sz = 1;
        for(node *next = cur->_ch_last->_next; ; next = next->_next){
            if(print) cout << depth << ": " << next->_data.first << " " <<
                           next->_data.second << " " << next->_mark << endl;
            sz += dfs(next, print, depth + 1);
            if(next == cur->_ch_last) break;
        }
        return sz;
    }
    void check(bool print = false) const {
        if(!_minimum){
            assert(_size == 0);
            return;
        }
        size_t sz = 0;
        for(node *cur = _minimum->_next; ; cur = cur->_next){
            if(print) cout << "0: " << cur->_data.first << " " <<
                           cur->_data.second << endl;
            sz += dfs(cur, print, 1);
            if(cur == _minimum) break;
        }
        cout << endl;
        assert(sz == _size);
    }
};

template<typename T> class Dijkstra {
public:
    struct edge{
        int to; T cost;
    };
    const int V;
    vector<vector<edge> > G;
    vector<T> d;
    using pti = pair<T,int>;
    Fibonacci_Heap<T, int> fheap;
    typename Fibonacci_Heap<T, int>::node** keep;
    Dijkstra(int node_size) : V(node_size), G(V), d(V, numeric_limits<T>::max()),
                              keep(new typename Fibonacci_Heap<T, int>::node*[V]){}
    // ~Dijkstra(){
    // delete[] keep;
    // }
    // 有向グラフの場合
    void add_edge(int u,int v,T cost){
        G[u].PB((edge){v, cost});
    }
    void solve(int s){
        d[s] = 0;
        keep[s] = fheap.push(0, s);
        while(!fheap.empty()){
            int v = fheap.top().second, val = fheap.top().first;
            fheap.pop();
            if(d[v] < val) continue;
            for(auto& w : G[v]){
                if(d[w.to] > d[v] + w.cost){
                    if(d[w.to] == numeric_limits<T>::max()){
                        keep[w.to] = fheap.push(d[v] + w.cost, w.to);
                    }else{
                        fheap.update(keep[w.to], d[w.to] - (d[v] + w.cost));
                    }
                    d[w.to] = d[v] + w.cost;
                }
            }
        }
    }
};

class DWizardInMaze {
public:


void solve(std::istream& cin, std::ostream& cout, std::ostream& cerr) {
    int H, W;cin >> H >> W;
    int ch,dh,cw,dw;cin >> ch >> cw >> dh >> dw;
    ch--, dh--, cw--, dw--;
//    int H = 5000, W = 5000;
//    int ch = 0, cw = 0, dh = H - 1, dw = W -1;
//    vector<vector<char>> g(H,vector<char>(W, '.'));
    vector<string> g(H);
    REP(i,H)cin >> g[i];
    int start = ch*W + cw, goal = dh*W + dw;
//    vector<vector<edge>> d(H*W);
    Dijkstra<int> dijk(H*W);
    auto ok = [&](int h, int w){
        return  0<= h && h<H && 0<= w && w < W && g[h][w] == '.';
    };
    VI dx = {0,0,1,-1},dy= {1,-1,0,0};
    REP(i,H)REP(j,W){
        if(g[i][j] == '.') {
            FOR(x, -2, 3)
                FOR(y, -2, 3) {
                    if (abs(x) + abs(y) > 1 && ok(x + i, y + j))
                       dijk.add_edge(i*W+j,(x+i)*W+(y+j),1);
//                        d[i * W + j].EB(edge({(x + i) * W + (y + j), 1}));
                }
            REP(k, 4) {
                int x = i + dx[k], y = j + dy[k];
                if (ok(x, y))
                    dijk.add_edge(i*W+j,x*W+y,0);
//                    d[i * W + j].EB(edge{x * W + y, 0});
            }
        }
    }
    DBG(start, goal)
//    auto dist = dijkstra<int>(d, start);
//    DBG(dist)
    dijk.solve(start);
    DBG(dijk.d)
    if(dijk.d[goal] > 1e7)print(-1);
    else print(dijk.d[goal]);



}


};


#undef int
int main() {

	DWizardInMaze solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
    std::ostringstream err;
	in.tie(0); ios::sync_with_stdio(0);
    solver.solve(in, out,err);
	return 0;
}


Measured memory_bytes: 109584