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

typedef long long int   ll;
#define int             ll
#define PI              3.1415926535897932384626433832795

#define pb              push_back
#define rep0            for(int i=0;i<n;i++)
#define rep1            for(int i=1;i<=n;i++)
#define inputar0(ar)    for(int i=0;i<n;i++)cin>>ar[i]
#define inputar1(ar)    for(int i=1;i<=n;i++)cin>>ar[i]

#define hell            freopen("input.txt","r", stdin);freopen("output.txt", "w", stdout);freopen("error.txt", "w",stderr);
#define fastasF_boi()   ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl            "\n"
//#define cerr            if(false)cerr

int mod=1000000007;
int maxn=200001;
int binpow(int a,int b,int mod){int res=1;while(b>0){if(b%2){res=(res*a)%mod;b--;}a=(a*a)%mod;b/=2;}return res%mod;}
int binpow(int a,int b){int res=1;while(b>0){if(b%2){res=(res*a);b--;}a=(a*a);b/=2;}return res;}
bool isPrime(int n){if(n==1)return false;for(int i=2;i*i<=n;i++){if(n%i==0)return false;}return true;}
int mulmod(int a,int b){return ((a%mod)*(b%mod))%mod;}
int addmod(int a,int b){return (((a%mod)+(b%mod))+mod)%mod;}

int h,w,r1,c1,r2,c2;
int magic=0;
bool ans=false;
vector<pair<int,int>> cur;
vector<vector<int>> mat(10001,vector<int>(10001,INT_MAX));
string maze[10001];

void dfs(int i,int j){
    if(i<1 or i>h or j<1 or j>w or maze[i-1][j-1]=='#' or ans)
        return;
    cur.push_back({i,j});
    mat[i][j]=magic;
    maze[i-1][j-1]='#';
    if(i==r2 and j==c2) {
        ans=true;
        return;
    }
    dfs(i-1,j);
    dfs(i+1,j);
    dfs(i,j-1);
    dfs(i,j+1);
}

vector<pair<int,int>> mark(){
    vector<pair<int,int>> newcur;
    for(auto it:cur){
        for(int i=max(it.first-2,1ll);i<=min(h,it.first+2);i++){
            for(int j=max(1ll,it.second-2);j<=min(w,it.second+2);j++){
                if(maze[i-1][j-1]=='.' and mat[i][j]==INT_MAX){
                    if(i==r2 and j==c2){
                        ans=true;
                        return {};
                    }
                    mat[i][j]=magic;
                    newcur.push_back({i,j});
                }
            }
        }
    }
    return newcur;
}

void solve(){
    cin>>h>>w>>r1>>c1>>r2>>c2;
    mat[r1][c1]=0;
    for(int i=0;i<h;i++)
        cin>>maze[i];


    cur.clear();
    dfs(r1,c1);
    magic++;
    if(ans){
        cout<<0<<endl;
        return;
    }

//    cerr<<"cur : "; for(auto it:cur) cerr<<"("<<it.first<<","<<it.second<<") "; cerr<<endl;
//    cerr<<"mat:"<<endl;
//    for(int i=1;i<=h;i++){
//        for(int j=1;j<=w;j++)
//            cerr<<mat[i][j]<<" ";
//        cerr<<endl;
//    }
//    cerr<<"maze:"<<endl; for(int i=0;i<h;i++) cerr<<maze[i]<<endl;


    while(1){
        vector<pair<int,int>> newcur=mark();

//        cerr<<"new cur : "; for(auto it:newcur) cerr<<"("<<it.first<<","<<it.second<<") "; cerr<<endl;
//        cerr<<"mat:"<<endl;
//        for(int i=1;i<=h;i++){
//            for(int j=1;j<=w;j++)
//                cerr<<mat[i][j]<<" ";
//            cerr<<endl;
//        }
//        cerr<<"maze:"<<endl; for(int i=0;i<h;i++) cerr<<maze[i]<<endl;

        if(newcur.empty() or ans)
            break;
        cur.clear();
        for(auto it:newcur){
            dfs(it.first,it.second);
            if(ans) break;
        }

//        cerr<<"cur : "; for(auto it:cur) cerr<<"("<<it.first<<","<<it.second<<") "; cerr<<endl;
//        cerr<<"mat:"<<endl;
//        for(int i=1;i<=h;i++){
//            for(int j=1;j<=w;j++)
//                cerr<<mat[i][j]<<" ";
//            cerr<<endl;
//        }
//        cerr<<"maze:"<<endl; for(int i=0;i<h;i++) cerr<<maze[i]<<endl;

        if(ans) break;
        magic++;
    }


    if(ans)
        cout<<magic<<endl;
    else cout<<-1<<endl;
}

signed main(){
//    hell;

    fastasF_boi();
    int t=1;
//    cin>>t;
    for(int tt=1;tt<=t;tt++){
        solve();
    }
}



Measured memory_bytes: 786564