#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <tuple>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <random>
#include <set>
#include <stack>
#include <time.h>

//#include <bits/stdc++.h>
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i,i0,n) for(int (i)=(i0);(i)<(n);(i)++)
#define FORR(i,i0,n) for(int (i)=(n)-1; (i)>=(i0);(i)--)
#define SORT(x) sort(x.begin(),x.end())
#define SORTR(x) sort(x.begin(),x.end(),greater<vector<int>>())
#define rn return
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple

using namespace std;
using ll = long long;
typedef std::pair<int, int> pii;
typedef std::pair<int, double> pid;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;

#define PI 3.14159265358979323846264338327950L
const int mod = 1e9+7;




struct item{ // CHANGE
    ll val;
};

class SegTree{
public:
    ll size;
    vector<item> values;
    
    SegTree(ll n){
        size = 1;
        while(size<n) size*=2;
        values.resize(size*2-1);
    }
    
    item NEUTRAL_ELEMENT = {0}; // CHANGE
    item single(ll val) { // CHANGE
        return {val};
    }
    item merge(item a, item b){ // CHANGE
        return {a.val+b.val};
    }
    
    
    void build(vector<ll>& a){
        build(a,0,0,size);
    };
    void build(vector<ll>& a, int node, int nodeL, int nodeR){
        if (nodeR-nodeL == 1) {
            if(nodeL<a.size()) values[node] = single(a[nodeL]); // SINGLE
            return;
        }
        int m = (nodeL+nodeR)/2;
        build(a,node*2+1,nodeL,m);
        build(a,node*2+2,m,nodeR);
        values[node] = merge(values[node*2+1],values[node*2+2]);
    }
    
    
    
    void set(ll index, ll val){
        set(index,val,0,0,size);
    }
    void set(ll index, ll val, ll node, ll nodeL, ll nodeR){
        if (nodeR-nodeL==1) {
            values[node]= single(val); // SINGLE
            return;
        }
        int m = (nodeL+nodeR)/2;
        if (index < m) set(index,val,node*2+1,nodeL,m);
        else set(index,val,node*2+2,m,nodeR);
        values[node] = merge(values[node*2+1],values[node*2+2]);
    }
   
    
    
    void add(ll index, ll val){
        add(index,val,0,0,size);
    }
    void add(ll index, ll val, ll node, ll nodeL, ll nodeR){
        if (nodeR-nodeL==1) {
            values[node].val += val; // SINGLE
            return;
        }
        int m = (nodeL+nodeR)/2;
        if (index < m) add(index,val,node*2+1,nodeL,m);
        else add(index,val,node*2+2,m,nodeR);
        values[node] = merge(values[node*2+1],values[node*2+2]);
    }
    
    
    
    item calc(ll l, ll r){
        return calc(l,r,0,0,size); 
    }
    item calc(ll l,ll r, ll node, ll nodeL, ll nodeR){
        if (r<=nodeL || l>=nodeR) return NEUTRAL_ELEMENT;
        if (l<=nodeL && nodeR<=r) return values[node];
        int m = (nodeL+nodeR)/2;
        return merge(calc(l,r,node*2+1,nodeL,m),calc(l,r,node*2+2,m,nodeR));
    }
    
    
    int find(int k){
        return find(k,0,0,size);
    }
    int find(ll k, ll node, ll nodeL,ll nodeR){
        if (nodeR-nodeL==1) return nodeL;
        int m = (nodeL+nodeR)/2;
        int lsum = values[2*node+1].val;
        if(k<=lsum) return find(k,2*node+1,nodeL,m);
        else return find(k-lsum,2*node+2,m,nodeR);
    }
    
    int findAbove(ll x, ll l){
        return findAbove(x,l,0,0,size);
    }
    int findAbove(ll x, ll l, ll node, ll nodeL, ll nodeR){
        if (values[node].val<x || nodeR<=l) return -1;
        if (nodeR-nodeL==1) return nodeL;
        int m = (nodeR+nodeL)/2;
        int cand = findAbove(x,l,node*2+1, nodeL,m);
        if (cand==-1) cand = findAbove(x,l,node*2+2,m,nodeR);
        return cand;
    }
    
};


void solve(){
    int h,w;
    cin >> h >> w;
    int ch,cw;
    cin >> ch >> cw;
    ch--,cw--;
    int dh,dw;
    cin >> dh >> dw;
    dh--,dw--;
    
    vector<string> f(h);
    rep(i,h) cin >> f[i];
    
    queue<pair<int,int>> q;
    q.push({ch,cw});
    
    int ans = 0;
    vector<vector<int>> v(h,vector<int>(w,0));
    v[ch][cw] = 1;
    
    vector<int> dx = {0,0,-1,1};
    vector<int> dy = {1,-1,0,0};
    while(q.size()){
        
        queue<pair<int,int>> nq;
        
        while(q.size()){
            int sz = q.size();
            for(int i = 0; i < sz; i++){
                auto cur = q.front();
                nq.push(cur);
                q.pop();
                if (cur.first == dh && cur.second == dw){
                    cout << ans;
                    return;
                }
                for(int k = 0; k < 4;k++){
                    int nh = cur.first+dx[k];
                    int nw = cur.second + dy[k];
                    if (nh >= 0 && nh < h && nw >= 0 && nw < w && f[nh][nw] =='.'){
                        if (v[nh][nw]) continue;
                        v[nh][nw] = 1;
                        q.push({nh,nw});
                    }
                }
            }
        }
        
        q = nq;
        
        
        int sz = q.size();
        for(int i = 0; i < sz; i++){
            auto cur = q.front();
            q.pop();
            if (cur.first == dh && cur.second == dw){
                cout << ans;
                return;
            }
            for(int ii = -2; ii <= 2; ii++){
                for(int jj = -2; jj <= 2; jj++){
                    int nh = cur.first+ii;
                    int nw = cur.second+jj;
                    if (nh >= 0 && nh < h && nw >= 0 && nw < w && f[nh][nw] =='.'){
                        if (v[nh][nw]) continue;
                        v[nh][nw] = 1;
                        q.push({nh,nw});
                    }
                }
            }
        }
        ans++;
    }
    cout << -1;
}


int main() {
    int T;
    //cin >> T;
    T = 1;
    while (T--) {
        solve();
        cout << endl;
    }
}














Measured memory_bytes: 9668