using namespace std;
#include <bits/stdc++.h>
#define frr(i, r, l) for(int i = r; i >= l; --i)
#define fr(i, l, r) for(int i = l; i < r; ++i)
#define forit(it, type, var) for(type::iterator it = var.begin(); it != var.end(); it++)
#define ii pair<int, int>
#define iii pair<int, ii>
#define MEMS(a,b) memset(a,b,sizeof(a))
#define fi first
#define se second
#define m_p make_pair
#define pb push_back
#define ll long long
#define ld long double
typedef pair<ll, ll> iPair; 
priority_queue< ll, vector <ll> , greater<ll>>pq1,pq2;
#define maxN 1000000000000000000
ll power(ll a,ll b){ll res=1;while(b>0){res*=a;b--;}return res;}
ll mn[1005][1005],vst[1005][1005]={0},n,m,vis[1005][1005]={0},cnt=0,ans,fl=0;
string str[1005];
queue<pair<ll,ll>>q,q2;
ll bfs(ll x1,ll y1,ll x2,ll y2)
{
    while(fl==0)
    {
        while(!q.empty())
        {
           ll a=q.front().first;
           ll b=q.front().second;
            vis[a][b]=1;
          //  cout<<a<<" "<<b<<endl;
            q2.push({a,b});

            if(a-1>=0 && str[a-1][b]=='.' && vis[a-1][b]==0)
            {
                q.push({a-1,b});
                q2.push({a-1,b});
                vis[a-1][b]=1;
            }

            if(a+1<n && str[a+1][b]=='.' && vis[a+1][b]==0)
            {
                q.push({a+1,b});
                q2.push({a+1,b});
                vis[a+1][b]=1;
            }

            if(b-1>=0 && str[a][b-1]=='.' && vis[a][b-1]==0)
            {
                q.push({a,b-1});
                q2.push({a,b-1});
                vis[a][b-1]=1;
            }

            if(b+1<m && str[a][b+1]=='.' && vis[a][b+1]==0)
            {
                q.push({a,b+1});
                q2.push({a,b+1});
                vis[a][b+1]=1;
            }
            q.pop();
        }
        if(vis[x2][y2]==1)
        {
            ans=cnt;
            break;
            fl=1;
        }
        if(q2.size()==0)
        {
            ans=-1;
            break;
        }
        
        while(!q2.empty())
        {
            ll a=q2.front().first;
            ll b=q2.front().second;
    
            ll b1=b-2;
            if(b1<0)
            b1=0;
    
            ll b2=b+2;
            if(b2>m-1)
            b2=m-1;
    
            ll a1=a-2;
            if(a1<0)
            a1=0;
    
            ll a2=a+2;
            if(a2>n-1)
            a2=n-1;
    
            fr(i,a1,a2+1)
            {
                fr(j,b1,b2+1)
                {
                    if(vis[i][j]==0 && str[i][j]=='.' && vst[i][j]==0)
                    {
                        q.push({i,j});
                        vst[i][j]=1;
                    }
                    
                }
            }
            q2.pop();
        }
        cnt++;
    }
    
    return 0;
}
int main()
{
    
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll x1,y1,x2,y2;
    cin>>n>>m;
    cin>>x1>>y1;
    cin>>x2>>y2;
 
    fr(i,0,n)
    {
        cin>>str[i];
    }
    fr(i,0,n+1)
    {
        fr(j,0,m+1)
        {
            mn[i][j]=maxN;
        }
    }
    x1--;y1--;x2--;y2--;
    q.push({x1,y1});
    if(str[x1][y1]=='#')
    {
        cout<<"-1"<<endl;
        return 0;
    }
    
    bfs(x1,y1,x2,y2);
    if(vis[x2][y2]==0)
    cout<<"-1"<<endl;
    else
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 28528