#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long int
#define fi first
#define se second
#define pub push_back
#define mkp make_pair
#define pi pair<int,int>
#define push push
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for(int i=(int)(l);i<(int)(r);i++)
#define repd(i, l, r) for (int i=(int)(l);i>=(int)(r);i--)
int power(int x, unsigned int y){int res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;}
int powermod(int x, unsigned int y, int p){int res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
#define print2d(mat,n,m){for(int i=0;i<(int)(n);i++){for(int j=0;j<(m);j++){cout<<mat[i][j]<<" ";}cout<<endl;}}
#define print1d(mat,n){for(int i=0;i<(int)(n);i++)cout<<mat[i]<<" ";cout<<endl;}
#define clr(a,x) memset(a,x,sizeof(a))
#define rr(v) for(auto &val:v)
#define print(v) for (const auto itr : v){cout<<itr<<' ';}cout<<endl;
#define init(v,x) for (auto &itr:v){itr=x;}
#define minpq(x) x,vector<x>,greater<x>
#define ln length()
#define sz size()
#define inf 1e18;
const int mod= 1e9+7;
vector<pi> adj[1000005];
void solve(){
   int h,w,ch,cw,dh,dw;
   cin>>h>>w;
   cin>>ch>>cw;
   cin>>dh>>dw;
   string arr[h+5];
   rep(i,0,h){
       cin>>arr[i];
   }
//   cout<<"yaha"<<endl;
    rep(i,0,h){
        rep(j,0,w){
            // cout<<i<<" "<<j<<endl;
            if (arr[i][j]=='.'){
                
                if (j-1>=0 and arr[i][j-1]=='.' ){
                    adj[w*i+j].pub({w*i+(j-1),0});
                }
                // cout<<"here1"<<endl;
                if (j+1<w and arr[i][j+1]=='.' ){
                    adj[w*i+j].pub({w*i+(j+1),0});
                }
                // cout<<"here2"<<endl;
                if (i-1>=0 and arr[i-1][j]=='.' ){
                    adj[w*i+j].pub({w*(i-1)+j,0});
                }
                // cout<<"here3"<<endl;
                if (i+1<h and arr[i+1][j]=='.' ){
                    adj[w*i+j].pub({w*(i+1)+j,0});
                }
                // cout<<"here4"<<endl;
                
            }
            for(int i1=max(i-2,0ll);i1<min(i+3,h);i1++){
                for(int j1=max(0ll,j-2);j1<min(j+3,w);j1++){
                    if(arr[i1][j1]=='.'){
                        adj[w*i+j].pub({w*i1+j1,1});
                    }
                }
            }
            // cout<<"here\n";
            
            
        }

    }

    // cout<<"idhar?\n";
    int dist[w*h+5]={0};
    for(int i=0;i<w*h+1;i++)
    {
        sort(adj[i].begin(),adj[i].end());
        // rr(adj[i]){
        //     cout<<val.fi<<":"<<val.se<<", ";
        // }
        // cout<<endl;
        dist[i]=999999999999999;
    }
    dist[w*(ch-1)+(cw-1)]=0;
    multiset < pair <int,int> > m;
    m.insert({0,w*(ch-1)+(cw-1)});
    while(m.begin()!=m.end())
    {
        pair<int,int> p = *m.begin();
        m.erase(m.begin());
        for(auto &i : adj[p.second] )
        {
            if(p.first+i.second<dist[i.first])
            {
                dist[i.first]=p.first+i.second;
                m.insert({dist[i.first],i.first});
            }
        }
        
    }
    // print1d(dist,w*h)
    if(dist[w*(dh-1)+(dw-1)]!=999999999999999)
    cout<<dist[w*(dh-1)+(dw-1)]<<endl;
    else
    cout<<-1<<endl;
   
}

int32_t main(){
    fastIO
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Measured memory_bytes: 338568