//#pragma optimization_level 3
#include <bits/stdc++.h>
#define  f               first
#define  s               second
#define  sym(s)          s = "#" + s + "#";
#define  all(x)          (x).begin(), (x).end()
#define  alll(x,n)       x+1, x+n+1
#define  newl            cout<<"\n";
#define  foo(i,a,n)      for(ll i = (a); i <= n; i++)
#define  deb1(a)         cout<<a<<"\n";
#define  deb2(a,b)       cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)     cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)   cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  debp(a)         cout<<a.f<<" "<<a.s<<"\n";
#define  debv(a)         for(auto it: a)cout<<it<<" ";newl;
#define  debm(a)         for(auto it: a)cout<<"{"<<it.f<<","<<it.s<<"}, ";newl;
#define  deb1d(a,n)      foo(i,1,n)cout<<a[i]<<" ";newl;
#define  deb2d(a,n,m)    foo(i,1,n){foo(j,1,m){cout<<a[i][j]<<" ";}newl;}

using namespace std;
using ll              =  long long;
using ld              =  long double;
const ll   MOD        =  1e+9 + 7;
const ll   INF        =  LLONG_MAX;
const int  N          =  (int)1e+3 + 8;

ll n, m, stx, sty, enx, eny;
string s[N];
ll a[N][N], vis[N*N];
ll grp;
set<ll> adj[N*N];
ll res = INF;

ll dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0};


ll dx25[] = {-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,0,0,0,0,0,1,1,1,1,1,2,2,2,2,2};
ll dy25[] = {-2,-1,0,1,2,-2,-1,0,1,2,-2,-1,0,1,2,-2,-1,0,1,2,-2,-1,0,1,2};

void dfs(ll x, ll y){
    a[x][y] = grp;
    for(int k = 0; k < 4; k++){
        int dx = x + dx4[k];
        int dy = y + dy4[k];
        if(s[dx][dy] == '.' && a[dx][dy] == 0){
            dfs(dx,dy);
        }
    }
}

void bfs(){
    queue<pair<int, int>> q;
    q.push({a[stx][sty], 0});
    vis[a[stx][sty]] = 1;
    while(q.size()){
        pair<int, int> p = q.front(); q.pop();
        ll v = p.f, d = p.s;
        if(v == a[enx][eny]){
            res = d;
            return;
        }
        for(auto to: adj[v]){
            if(vis[to] == 0){
                vis[to] = 1;
                q.push({to, d+1});
            }
        }
    }
}

void MAIN(int tc) {
    cin >> n >> m;
    cin >> stx >> sty;
    cin >> enx >> eny;
    for (int i = 1; i <= n; i++) {
        cin >> s[i];
        s[i] = "#" + s[i] + "#";
    }
    s[0] = s[n + 1] = string(m + 2, '#');
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if(a[i][j] == 0 && s[i][j] == '.'){
                grp += 1;
                dfs(i,j);
            }
        }
    }


    for (int x = 1; x <= n; x++) {
        for (int y = 1; y <= m; y++) {
            if(s[x][y] == '#') continue; 
            for(int k = 0; k < 25; k++) {
                int dx = x + dx25[k];
                int dy = y + dy25[k];
                if(dx >= 1 && dx <= n && dy >= 1 && dy <= m){
                    if(s[dx][dy] == '.' && a[x][y] != a[dx][dy]){
                        adj[a[x][y]].insert(a[dx][dy]);
                        adj[a[dx][dy]].insert(a[x][y]);
                    }
                }
            }
        }
    }


    //deb2d(a,n,m);
    bfs();

    if(res == INF) res = -1;
    deb1(res);

}

int main() {
    ios:: sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cout << fixed; cout << setprecision(10);

    int test_cases = 1;
    //cin>>test_cases;
    for (int i = 1; i <= test_cases; i++) {
        MAIN(i);
    }
}

Measured memory_bytes: 80624