#include <bits/stdc++.h>
#include <vector> 
# include <cstdint>
#define FOR(i,l,r) for(int i=(l);i<(r);++i)
#define RFOR(i,l,r) for(int i=(l);i>=(int)(r);i--)
#define rep(i,n)  FOR(i,0,n)
#define rrep(i,n)  RFOR(i,n-1,0)

using namespace std;
#define int long long
using ll = long long;
const int MX = 1e6;
const ll inf = 1e13;
const int mod = 1e9+7;
int h,w;
int cx,cy;
int dx,dy;
int s[1003][1003];
int v[1003][1003];

signed main() {
    cin >> h>>w>>cx>>cy>>dx>>dy;
    cx--;cy--;
    dx--;dy--;
    rep(i,h) {
        string st;
        cin >>st;
        rep(j,w) {
            s[i][j]=st[j];
            v[i][j]=inf;
        }
    }
    v[cx][cy]=0;
    deque<pair<int,int>> q;
    q.push_back({cx,cy});
    
    vector<int> a;
    while(q.size()>0) {
        pair<int,int> p = q.front();
        q.pop_front();
        int x = p.first;
        int y = p.second;
        //cout << x <<" "<<y << endl;
        rep(i,5) {
            rep(j,5) {
                if(x-2+i<0 || x-2+i>=h || y-2+j<0 || y-2+j>=w) continue;
                if(s[x-2+i][y-2+j]=='.') {
                    if(v[x-2+i][y-2+j]==inf)q.push_back({x-2+i,y-2+j});
                    v[x-2+i][y-2+j]=min(v[x-2+i][y-2+j],v[x][y]+(int)1);
                }
            }
        }
        if(x+1<h) {
            if(s[x+1][y]=='.') {
                if(v[x+1][y]!=v[x][y])q.push_front({x+1,y});
                v[x+1][y] = min(v[x+1][y],v[x][y]);
            }
        }
        if(y+1<w) {
            if(s[x][y+1]=='.') {
                if(v[x][y+1]!=v[x][y])q.push_front({x,y+1});
                v[x][y+1] = min(v[x][y+1],v[x][y]);
            }
        }
        if(x-1>=0) {
            if(s[x-1][y]=='.') {
                if(v[x-1][y]!=v[x][y])q.push_front({x-1,y});
                v[x-1][y] = min(v[x-1][y],v[x][y]);
            }
        }
        if(y-1>=0) {
            if(s[x][y-1]=='.') {
                if(v[x][y-1]!=v[x][y])q.push_front({x,y-1});
                v[x][y-1] = min(v[x][y-1],v[x][y]);
            }
        }
        if(x+1<h) {
            if(s[x+1][y]=='.') {
                rep(i,5) {
                    rep(j,5) {
                        if(x+1-2+i<0 || x+1-2+i>=h || y-2+j<0 || y-2+j>=w) continue;
                        if(s[x+1-2+i][y-2+j]=='.') {
                            if(v[x+1-2+i][y-2+j]==inf)q.push_back({x+1-2+i,y-2+j});
                            v[x+1-2+i][y-2+j]=min(v[x+1-2+i][y-2+j],v[x+1][y]+1);
                        }
                    }
                }
            }
        }
        if(y+1<w) {
            if(s[x][y+1]=='.') {
                rep(i,5) {
                    rep(j,5) {
                        if(x-2+i<0 || x-2+i>=h || y+1-2+j<0 || y+1-2+j>=w) continue;
                        if(s[x-2+i][y+1-2+j]=='.') {
                            if(v[x-2+i][y+1-2+j]==inf)q.push_back({x-2+i,y+1-2+j});
                            v[x-2+i][y+1-2+j]=min(v[x-2+i][y+1-2+j],v[x][y+1]+1);
                        }
                    }
                }
            }
        }
        if(x-1>=0) {
            if(s[x-1][y]=='.') {
                rep(i,5) {
                    rep(j,5) {
                        if(x-1-2+i<0 || x-1-2+i>=h || y-2+j<0 || y-2+j>=w) continue;
                        if(s[x-1-2+i][y-2+j]=='.') {
                            if(v[x-1-2+i][y-2+j]==inf)q.push_back({x-1-2+i,y-2+j});
                            v[x-1-2+i][y-2+j]=min(v[x-1-2+i][y-2+j],v[x-1][y]+1);
                        }
                    }
                }
            }
        }
        if(y-1>=0) {
            if(s[x][y-1]=='.') {
                rep(i,5) {
                    rep(j,5) {
                        if(x-2+i<0 || x-2+i>=h || y-1-2+j<0 || y-1-2+j>=w) continue;
                        if(s[x-2+i][y-1-2+j]=='.') {
                            if(v[x-2+i][y-1-2+j]==inf)q.push_back({x-2+i,y-1-2+j});
                            v[x-2+i][y-1-2+j]=min(v[x-2+i][y-1-2+j],v[x][y-1]+1);
                        }
                    }
                }
            }
        }

    }
    
    // rep(i,h) {
    //     rep(j,w) {
    //         if(v[i][j]!=inf) cout << v[i][j] << flush;
    //         else cout << "x" << flush;
    //     }
    //     cout << "" << endl;
    // }
    if(v[dx][dy]==inf) cout << -1 << endl;
    else cout << v[dx][dy] << endl;
    

    return 0;
}



Measured memory_bytes: 19480