#include <cstring>
#include <cassert>
#include <utility>
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <bitset>
#include <chrono>
#include <cstdlib>
#include <functional>
#include <tuple>
#include <climits>
#include <limits>
#include <deque>
#include <list>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <complex>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll              long long
#define pi              pair<int,int>
#define pll             pair<ll,ll>
#define pb              push_back
#define F               first
#define S               second
#define vi              vector<int>
#define vll             vector<ll>
#define endl            '\n'
#define all(v)          (v).begin(),(v).end()
#define FOR(i,a,b)      for(int i=a;i<b;i++)
#define MP(a,b)         make_pair(a,b)
#define debug(x)        cerr<< #x << " : " << x << endl;
#define debugp(p)       cerr<< #p << " : " << "(" << (p).first << "," << (p).second << ")\n";
#define debugv(v)       cerr<< #v << " : " << "["; for(int i = 0; i < (v).size(); i++) cerr << v[i] << " "; cerr<<"]\n";
#define debugs(m)       cerr<< #m << " : [ "; for(auto itr = m.begin(); itr != m.end(); itr++) cerr <<*itr << " "; cerr << "]\n";
#define debugm(m)       cerr<< #m << " : [ "; for(auto itr = m.begin(); itr != m.end(); itr++) cerr <<"(" << itr->first << "," << itr->second << ") "; cerr << "]\n";

/* ordered set: 
tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>
member functions :
1. order_of_key(k) : number of elements strictly lesser than k
2. find_by_order(k) : k-th element in the set
*/

inline ll modexp(ll x, ll ex , ll md) {
    ll ans = 1ll;
    while (ex > 0) {
        if (ex & 1ll) ans = (ans * x) % md;
        ex >>= 1ll;
        x = (x * x) % md;
    }
    return ans;
}

const int inf = 1e9 + 7;
bool vis[1005][1005];
int dis[1005][1005];
string s[1008];
int h , w , ch , cw , dh , dw;
int djikstra (int x , int y){
    multiset<pair<int,pair<int,int>>> ms;

    // cout << "called with " << x << " and " <<y << endl;

    for (int v = 1; v <= h; v++){
        for (int u = 1; u <= w; u++){
            if (v == x && u == y) dis[v][u] = 0;
            else dis[v][u] = inf;
            if (s[v][u] == '.'){
            // cout << "v : " << v << " and  " << u << endl;
                ms.insert (make_pair (dis[v][u] , make_pair(v , u)));
            }
        }
    }
    //    for (int i = 1; i <= h; i++){
    //     for (int j = 1; j <= w; j++){
    //         cout << dis[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    // cout<<"ms: " << endl;
    // for(auto t:ms){
    //     cout<<t.first<<" " << t.second.first<< " " << t.second.second<<endl; 
    // }
    while (!ms.empty()){
        auto tp = *(ms.begin());
        int dt = tp.first;
        int xs = tp.second.first;
        int ys = tp.second.second;
        // cout << xs << " and " << ys << " poped\n";
        ms.erase (tp);
        for (int i = xs - 2; i <= xs + 2; i++){
           for (int j = ys - 2; j <= ys + 2; j++){
                if(xs == i && ys == j) continue;
                if (i <= h && i >= 1 && j >= 1 && j <= w && s[i][j] == '.'){
                    // cout << "tada : " << xs << " , " << ys << " " << i << " " << j << endl;
                    auto it =  ms.find( make_pair( dis[i][j] , make_pair(i , j)));
                    if (it != ms.end()){
                        ms.erase (it);
                        int cost;
                        if (abs(xs - i) + abs(ys - j) == 1) cost = 0;
                        else cost = 1;
                        dis[i][j] = min (dis[i][j], dis[xs][ys] + cost);
                        ms.insert( make_pair(dis[i][j] , make_pair(i , j)));
                    }
               }
            }
        }
    }
    // for (int i = 1; i <= h; i++){
    //     for (int j = 1; j <= w; j++){
    //         cout << dis[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    return (dis[ch][cw] == inf ? -1 : dis[ch][cw]);
}

void solve(){
    cin >> h >> w;
    cin >> ch >> cw;
    cin >> dh >> dw;
    for (int i = 1; i <= h; i++) {
        cin >> s[i];
        s[i] = "h" + s[i];
    }
    cout << djikstra (dh , dw) << endl;
}
int main(){
    ios :: sync_with_stdio(false); 
    cin.tie(nullptr); 
    cout.tie(nullptr);
    
    int T = 1;
    // cin >> T;
    for(int tc = 1; tc <= T; tc++){
        // cout << "Case #" << tc << ": ";
        solve();
    }
    #ifdef LOCAL
    cerr << "Time elapsed\t: " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";       
    #endif
}



Measured memory_bytes: 39664