// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define clr clear()
#define ins insert
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
#define mem(x,val) memset((x),(val),sizeof(x));
#define cusmem(x,val) for(auto &i:x) i=val;
#define all(x) x.begin(),x.end()
#define fastio ios_base::sync_with_stdio(false);\
            cin.tie(NULL);\
            cout.tie(NULL);
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll>pll;
typedef pair<ll,int>pli;
typedef vector<ll> vl;
typedef vector<pll> vpll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vpii;
string impossible;

mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
// const int N=6;
const int N=1e3+5;
const int L=41;
int MOD=1e9+7;
ll n,m,k,i,j;
bool f0,f1,f2;
const int MAXN=2e3;

bool vis[N][N];
int col[N][N];
int curcol=-1;
char mat[N][N];
int h,w;
vector<pii>dir{{0,1},{1,0},{0,-1},{-1,0}};
void dfs(int i,int j,vector<pii>&temp){
    if(vis[i][j])   return ;
    vis[i][j]=true;
    col[i][j]=curcol;
    temp.pb({i,j});
    for(int k=0;k<4;k++){
        int ni=i+dir[k].fi;
        int nj=j+dir[k].se;
        if(ni<0||ni>=h||nj<0||nj>=w)    continue;
        if(mat[ni][nj]=='#')    continue;
        dfs(ni,nj,temp);
    }
}
int main(){
    #ifndef ONLINE_JUDGE
            freopen("input.txt","r",stdin); 
            freopen("output.txt","w",stdout);
    #endif
    fastio
    cout<<fixed<<setprecision(11);
    int tests=1;    
    // cin>>tests;
    for(int te=1;te<=tests;te++){
        cin>>h>>w;
        int a,b;    cin>>a>>b;
        int x,y;    cin>>x>>y;
        a--;b--;x--;y--;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++)    cin>>mat[i][j];
        }
        vector<vector<pii>>comp;
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(mat[i][j]=='.'&&!vis[i][j]){
                    curcol++;
                    vector<pii>temp;
                    dfs(i,j,temp);
                    comp.pb(temp);
                }
            }
        }
        if(col[a][b]==col[x][y])    cout<<"0\n";
        else{
            int lvl=0;
            int nc=comp.size();
            vector<bool>vis_comp(nc,false);
            queue<pii>q;
            for(auto i:comp[col[a][b]]){
                q.push(i);
            }
            vis_comp[col[a][b]]=true;
            q.push({-1,-1});
            while(!q.empty()){
                auto t=q.front();
                q.pop();
                if(t.fi==-1){
                    lvl++;
                    if(!q.empty()){
                        q.push({-1,-1});
                    }
                }
                else{
                    if(t.fi==x&&t.se==y){
                        cout<<lvl<<"\n";
                        return 0;
                    }
                    for(int i=max(t.fi-2,0);i<=min(h-1,t.fi+2);i++){
                        for(int j=max(t.se-2,0);j<=min(t.se+2,w-1);j++){
                            if(mat[i][j]=='.'&&(!vis_comp[col[i][j]])){
                                vis_comp[col[i][j]]=true;
                                for(auto k:comp[col[i][j]]){
                                    q.push(k);
                                }
                            }
                        }
                    }
                }
            }
            cout<<-1<<"\n";
        }
    }
    return 0;
}





Measured memory_bytes: 17728