#include <bits/stdc++.h> 
    //#define M_PI 3.14159265358979323846



using namespace std;
#define M 1000000007
#define rep(i,a,b) for(int i=a;i<b;++i)
#define repb(i,a,b) for(int i=a;i>=b;--i)
#define repv(arr)  for(auto it=arr[i].begin();it!=arr[i].end();++it)  
#define vi vector<int>
#define vb vector<bool>
#define vs vector<string>
#define vl vector<long long int>
#define vc vector<char>
#define sz(a) (int)a.size()
#define ssortA(arr)   stable_sort(arr.begin(),arr.end())
#define ssortB(arr)   stable_sort(arr.begin(),arr.end(),greater<ll >());
#define pii pair<int,int>
#define pli pair<long long,int>
#define pll pair<long long,long long>
#define ff first
#define ss second
#define search(arr,c)  binary_search(arr.begin(),arr.end(),c)
#define pb   push_back
#define ll long long
#define mp make_pair
#define endl "\n" 
#define LCM(a,b) boost::math::lcm(a,b)
#define cl_bf cin.ignore(numeric_limits<streamsize>::max(), '\n');
 //vector<vector<bool> > visited(1000,vector<bool>(1000,false));
 int visited[1000][1000];
 //vector<pii > adj[1000][1000];
 set<int> adjcc[(int)1e6];
 char arr[1000][1000];
 int h,w;
vi dirx={-1,1,0,0};
vi diry={0,0,-1,1};
vi mx={-2,-1,0,1,2};
bool valid_check(int x,int y){
     bool ok=true;
     if(x>=h||x<0)ok&=false;
     if(y>=w||y<0)ok&=false;
     //if(arr[x][y]=='#')ok&=false;
     return ok;
 }
 void dfs(pair<int,int> temp,int x){
     int a=temp.first,b=temp.second;
    visited[a][b]=x;
    //st.insert({a,b});
    rep(i,0,4){
        int nx=a+dirx[i];int ny=b+diry[i];
       if(valid_check(nx,ny))
        if(!visited[nx][ny]){
            //int c=adjcc[a][b][i].first,d=adjcc[a][b][i].second;
           if(arr[nx][ny]=='.')
            dfs({nx,ny},x);
        }
    }
 }
 
 void below_insert(int i,int j){
     int a=visited[i][j];
     rep(l,1,3)
     rep(k,0,5){
         int ny=j+mx[k];
         int nx=i+l;
         //if(i==0&&j==1)
        // cout<<nx<<" "<<ny<<endl;
         if(nx==i+1&&ny==j)continue;
     if(valid_check(nx,ny)){
         if(arr[nx][ny]=='.'){
            // cout<<"hi"<<endl;

             if(visited[nx][ny]!=a){
                 
             adjcc[visited[nx][ny]].insert(a);
             adjcc[a].insert(visited[nx][ny]);}
         }
     }
        
     }
    }

 int main(){
      ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     cin>>h>>w;
     int a,b,c,d;cin>>a>>b>>c>>d;
     --a,--b,--c,--d;
     //char arr[h][w];
     rep(i,0,h){
         rep(j,0,w){
             cin>>arr[i][j];
         }
     }
         
    
     int k=1;
   // int visited[i][j];
    // vector<set<pair<int,int> > > vst;
     rep(i,0,h){
         rep(j,0,w){
             if(!visited[i][j]&&arr[i][j]=='.')
            { //set<pair<int,int> > st;
            dfs({i,j},k);
           // vst.pb(st);
           ++k;}
            
         }
     }
     //rep(i,0,h){rep(j,0,w)cout<<visited[i][j]<<" ";cout<<endl;}
     rep(i,0,h){
         rep(j,0,w){
             if(arr[i][j]=='.'){
                    if(valid_check(i,j+1)){
                        if(arr[i][j+1]=='#'){
                            if(valid_check(i,j+2)){
                                if(arr[i][j+2]=='.'){
                                    int e=visited[i][j+2];
                                    int f=visited[i][j];
                                    if(e!=f){
                                        adjcc[e].insert(f);
                                        adjcc[f].insert(e);
                                    }
                                }
                            }
                        }
                    }
                    if(valid_check(i+1,j))
                        {
                            if(arr[i+1][j]=='#')
                            below_insert(i,j);
                        }
                 }
                 
             }
         }
        /* rep(i,0,4){
             cout<<sz(adjcc[i])<<endl;
             for(auto it:adjcc[i]){
                 cout<<it<<" ";
             }
             cout<<endl;
         }*/
    
         int pst=visited[a][b];
         int pen=visited[c][d];
         if(pen==pst){
             cout<<0<<endl;
             return 0;
         }
         queue<int> q;
         vi seen(k,0);
         seen[pst]=1;
         q.push(pst);
         int l=0;
         vi level(k);
         level[pst]=0;
         int flag=0;
         int ans;
         while(!q.empty()){
             int x=q.front();
             //cout<<x<<endl;
           //  seen[x]=1; 
             q.pop();
             for(auto it:adjcc[x]){
                if(!seen[it]){
                    level[it]=level[x]+1;
                    if(it==pen){
                        flag=1;
                        ans=level[it];
                        break;
                    }
                    seen[it]=1;
                    q.push(it);
                }
             }
             if(flag)break;
         }
         if(flag)cout<<ans<<endl;
         else cout<<-1<<endl;
     }

     

Measured memory_bytes: 74520