#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cstring>
#include <functional>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for(long long i=0; i<n; i++)
#define reps(i, s, e) for(long long i=s; i<e; i++)
#define repr(i, n) for(long long i=n-1; i>=0; i--)
#define reprs(i, s, e) for(long long i=e-1; i>=s; i--)

int main(){

    cin.tie(0);
    ios::sync_with_stdio(false);

    ll h, w; cin >> h >> w;
    ll ch, cw; cin >> ch >> cw;
    ll dh, dw; cin >> dh >> dw;
    string s[h];
    rep(i, h){
        cin >> s[i];
    }

    ll num[h][w];
    rep(i, h){
        rep(j, w){
            if(s[i][j] == '#'){
                num[i][j] = -1;
            }
            else{
                num[i][j] = 0;
            }
        }
    }

    ll label = 1;
    queue<pair<ll, ll>> que;
    rep(i, h){
        rep(j, w){
            if(num[i][j] != 0) continue;

            num[i][j] = label;
            que.push({i, j});
            while(!que.empty()){
                pair<ll, ll> pnow = que.front();
                que.pop();

                ll inow = pnow.first;
                ll jnow = pnow.second;

                if(inow > 0 && num[inow - 1][jnow] == 0){
                    num[inow - 1][jnow] = label;
                    que.push({inow - 1, jnow});
                }
                if(inow < h - 1 && num[inow + 1][jnow] == 0){
                    num[inow + 1][jnow] = label;
                    que.push({inow + 1, jnow});
                }
                if(jnow > 0 && num[inow][jnow - 1] == 0){
                    num[inow][jnow - 1] = label;
                    que.push({inow, jnow - 1});
                }
                if(jnow < w - 1 && num[inow][jnow + 1] == 0){
                    num[inow][jnow + 1] = label;
                    que.push({inow, jnow + 1});
                }
            }
            label++;
        }
    }

    vector<ll> g[label];

    rep(i, h){
        rep(j, w){
            if(num[i][j] == -1) continue;
            ll numnow = num[i][j];

            set<ll> st;
            reps(is, -2, 3){
                if(i + is >= h) break;
                if(i + is < 0) continue;

                reps(js, -2, 3){
                    if(j + js >= w) break;
                    if(j + js < 0) continue;
                    if(num[i + is][j + js] == -1) continue;

                    if(numnow != num[i + is][j + js]){
                        st.insert(num[i + is][j + js]);
                    }
                }
            }

            for(ll numnxt : st){
                g[numnow].push_back(numnxt);
                g[numnxt].push_back(numnow);
            }
        }
    }

    ll num_start = num[ch - 1][cw - 1];
    ll num_goal  = num[dh - 1][dw - 1];

    queue<ll> que_search;
    que_search.push(num_start);
    ll cost[label];
    rep(i, label){
        cost[i] = 1e8;
    }
    cost[num_start] = 0;
    
    while(!que_search.empty()){
        ll now = que_search.front();
        que_search.pop();

        for(ll nxt : g[now]){
            if(cost[nxt] <= cost[now] + 1) continue;

            cost[nxt] = cost[now] + 1;
            que_search.push(nxt);
        }
    }

    if(cost[num_goal] == 1e8){
        cout << -1 << endl;
    }
    else{
        cout << cost[num_goal] << endl;
    }

    return 0;
}

Measured memory_bytes: 36984