#include <bits/stdc++.h>
using namespace std;


#define ll long long int
#define ld long double
#define mod 1000000007
#define big 1e18+100000000
#define small -big
#define pb push_back



template <typename T>

void prarr(T a)
{
     cerr << "[ ";
     
     for(auto i : a){
         cerr << i << " ";
     }
     
     cerr << "]" << endl;
}


#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void _er(const char* name, Arg1&& arg1){
    cerr << "[" << name << " : " << arg1 << "]" << endl;
}

template <typename Arg1, typename... Args>

void _er(const char* names, Arg1&& arg1, Args&&... args){
    
    const char* comma = strchr(names + 1, ',');
    cerr << "[" ; cerr.write(names, comma - names) << " : " << arg1 << "] ";
    _er(comma+2, args...);
}



char A[1002][1002];
ll ans[1002][1002];
ll H, W;
multiset<pair<ll, pair<ll, ll> > > g;

bool reach(ll x, ll y) {
        if(x >= 1 && x <= H && y >=1 && y <= W && A[x][y] == '.') {
                 return true;
        }
        return false;
}

void dikstra() {
   
        while(g.size() >= 1) {
                
                auto it = g.begin();
                ll dist = (*it).first;
                ll xind = (*it).second.first;
                ll yind = (*it).second.second;
                
                if(dist > ans[xind][yind]) {
                        g.erase(it);
                        continue;
                }
                
                for(ll i = -2; i <= 2; i++) {
                        for(ll j = -2; j <= 2; j++) {
                                ll c = abs(i) + abs(j);
                                if(reach(xind + i, yind + j)) {
                                       {
                                            if(c == 1) {
                                                    if(dist < ans[xind + i][yind + j]) {
                                                            ans[xind + i][yind + j] = dist;
                                                            g.insert({dist, {xind + i, yind + j}});
                                                    }
                                            } else if(c > 1) {
                                                    if(dist + 1 < ans[xind + i][yind + j]) {
                                                            ans[xind + i][yind + j] = dist + 1;
                                                            g.insert({dist + 1, {xind + i, yind + j}});
                                                    }
                                            }
                                      }
                                }
                        }
                }
                
                g.erase(it);
        }        
        
}

void solve() {
            
            g.clear();
            
            cin >> H >> W;    
            
            ll Cr, Cl;
            ll Dr, Dl;
            
            cin >> Cr >> Cl;
            cin >> Dr >> Dl;
            
            for(ll i = 1; i <= H; i++) {
                    for(ll j = 1; j <= W; j++) {
                            cin >> A[i][j];
                            ans[i][j] = big;
                            
                    }
            }
            
            ans[Cr][Cl] = 0;
            g.insert({0, {Cr, Cl}});
            dikstra();
            
            if(ans[Dr][Dl] >= big) {
                    cout << -1 << endl;
            } else {
                    cout << ans[Dr][Dl] << endl;
            }
            
            
            
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
   

    int t=1;
   // cin >> t;

    for(int i = 1; i <= t; i++) {  
   
         solve();
    }


    return 0;
        
}


Measured memory_bytes: 13048