//*************************************JAI SAI RAM*************************************//
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl '\n'
ll mat[3000001],vis[3000001],parent[1000001],ranks[1000001];
ll P=998244353;
vector<pair<ll,ll>> path={{1,0},{0,1},{-1,0},{0,-1}};
ll find(ll x)
{
    if(parent[x]==x)
    return x;
    return parent[x]=find(parent[x]);
}
void unions(ll x,ll y)
{
    ll xp=find(x);
    ll yp=find(y);
    if(xp!=yp)
    {
        if(ranks[xp]>ranks[yp])
        parent[yp]=xp;
        else 
        {
            parent[xp]=yp;
            if(ranks[xp]==ranks[yp])
            ranks[yp]++;
        }
    }
}
vector<ll> adj[1000001];
void solve()
{
    ll i,j,k,l,m,n,o,r,p,q,ss;
    ll ans=0;
  //  cout<<ans<<endl;
    string s[1011];
    cin>>n>>m;
    for(i=1;i<=1000000;i++)
    {
        ranks[i]=0;
        parent[i]=i;
        mat[i]=0;
        vis[i]=0;
    }
    cin>>p>>q;
    cin>>r>>ss;
    for(i=1;i<=n;i++)
    {
        cin>>s[i];
        s[i]='#'+s[i];
    }
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
          ll curr=(i-1)*m+j;
          for(auto it:path)
          {
              ll x=it.first+i,y=it.second+j;
              if(x>=1&&x<=n&&y>=1&&y<=m&&s[i][j]!='#'&&s[x][y]!='#')
              {
                  unions((x-1)*m+y,curr);
              }
          }

        }
    } 
    set<ll> se;
    for(i=1;i<=n*m;i++)
    {
        adj[find(i)].push_back(i);
        se.insert(find(i));
    }
   // cout<<se.size()<<endl;
    ll src=find((p-1)*m+q);
    queue<ll> pq;
    vis[src]=1;
    mat[src]=0;
    pq.push(src);

    while(!pq.empty())
    {
        ll u=pq.front();
        pq.pop();
         //cout<<u<<endl;
         for(auto v:adj[u])
         {
             ll ii=(v+m-1)/m;
             ll jj=v%m;
             if(jj==0)
             jj=m;
             //cout<<ii<<" "<<jj<<endl;
             for(i=ii-2;i<=ii+2;i++)
             {
                 for(j=jj-2;j<=jj+2;j++)
                 {

                     ll curr=(i-1)*m+j;
                     if(curr>=1&&curr<=n*m&&s[i][j]=='.'&&s[ii][jj]=='.')
                     if(!vis[find(curr)])
                     {
                         vis[find(curr)]=1;
                         mat[find(curr)]=mat[u]+1;
                         pq.push(find(curr));
                     }
                 }
             }
         }
    }
    if(!vis[find((r-1)*m+ss)])
    cout<<"-1\n";
    else
    cout<<mat[find((r-1)*m+ss)]<<endl;
}
int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
     ll t=1;
   // cin>>t;
    ll o=1;
    while(t--)
    {

        solve();
        o++;
    }
     return 0;
}












































//manpreet.singh orz
//humane orz
//arnavvats orz
//leoamish300 orz
//cipher76 orz
//aug16arpit orz
//aayushindwan orz
//ig_monkey orz
//DeadlyCritic orz
//lucky_21 orz
//sak23 orz
//spookywooky orz
//supahotfire orz
//__FAILURE_orz
//Errichto orz
//tourist orz
//Um_nik orz
//300iq orz
//antontrygub_Oo orz
//boboniu orz
//MikeMirzayanov orz
//MiFaFaOvO orz
// Benq orz
//Radewoosh orz
//AshishGup orz
//Ashishchup orz
//aryanc403 orz
//ma_da_fa_ka orz
// Striver_79 orz
//Love babbar orz
// My Life of ultimate patience and hard work orz!!!!!!!


Measured memory_bytes: 137408