/*
___ is a raging sadist.
*/
//Destroy all WA at test 2
// Stay cool
// Pass the 4th wall..

#include "bits/stdc++.h"
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define endl "\n"
#define pi 3.14159265
const ll mod = 1000 * 1000 * 1000 + 7;
const ll mod1 = 998244353;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7; 
using namespace std;

ll power(ll x, ll y) 
{ 
   ll res = 1;      
  
   
    while (y > 0) 
    { 
       
        if (y & 1) 
            res = (long long)(res*x); 
  

        y = y>>1; 

        if (x<=100000000)
        x = (long long)(x*x);

        //cout<<x<<'\n'; 
    } 
    return res; 
} 



bool vis [1010][1010];
ll ans [1010][1010];
set <pii> en[1000005];
char a[1010][1010];
ll n,m;


 bool check(int x,int y){
    //cout<<n<<" "<<m<<" "<<x<<" "<<y<<" possible"<<endl;
 
     if (x<=n&&x>=1 and y<=m&&y>=1)
     {
        if (a[x][y]=='.' and vis[x][y]==false)
            return true;
        else
            return false;
     } else {
        return false;
     }
 }


void bfs_grid(int srx, int sry, ll cur)
{

    vis[srx][sry]=true;
    queue <pair<int,int> > q;
    q.push(mp(srx,sry));
 
 
    while(siz(q))
    {
        int topx=q.front().ff; int topy=q.front().ss;
        q.pop();
        
        if (check(topx-1,topy)==true)
        {
            en[cur].insert(mp(topx-1,topy));
            vis[topx-1][topy]=true;
            q.push(mp(topx-1,topy));
        }

        if (check(topx+1,topy)==true)
        {
            en[cur].insert(mp(topx+1,topy));
            vis[topx+1][topy]=true;
            q.push(mp(topx+1,topy));
        }

        if (check(topx,topy-1)==true)
        {
            en[cur].insert(mp(topx,topy-1));
            vis[topx][topy-1]=true;
            q.push(mp(topx,topy-1));
        }

        if (check(topx,topy+1)==true)
        {
            en[cur].insert(mp(topx,topy+1));
            vis[topx][topy+1]=true;
            q.push(mp(topx,topy+1));
        }

    }
 
}




int main()
{

#ifndef ONLINE_JUDGE    
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

    cin>>n>>m;

    ll s1,s2;
    cin>>s1>>s2;

    ll e1,e2;
    cin>>e1>>e2;

    For(i,1,n+1)
    {
        For(j,1,m+1)
        cin>>a[i][j];
    }

    en[0].insert(mp(s1,s2));
    ans[s1][s2]=0;

    //cout<<(*en[0].begin()).ff<<endl;

    //cerr<<a[1][3]<<endl;

    for(int i=0;i<=1000005;i++)
    {
        vector <pii> temp;

        while(siz(en[i]))
        {   
            int topx=(*en[i].begin()).ff; int topy=(*en[i].begin()).ss;
            //cerr<<topx<<' '<<topy<<endl;
            if (!vis[topx][topy])
            {
                bfs_grid(topx,topy,i);
                temp.pb(mp(topx,topy));
                en[i].erase(mp(topx,topy));
                ans[topx][topy]=i;
            } else {
                temp.pb(mp(topx,topy));
                en[i].erase(mp(topx,topy));
                ans[topx][topy]=i;
            }
        }

        for(int j=0;j<siz(temp);j++)
        {

            for(int k=-2;k<=2;k++)
            {
                for(int l=-2;l<=2;l++)
                {
                    if (k==0 and l==0)
                        continue;
                    else
                    {
                        if (check(temp[j].ff+k,temp[j].ss+l)==true)
                        {
                            en[i+1].insert(mp(temp[j].ff+k,temp[j].ss+l));
                        }
                    }
                }
            }
        }

    }

    if (!vis[e1][e2])
        cout<<-1;
    else
    {
        cout<<ans[e1][e2];
    }

return 0;
}

// check all product based operations for integer overflow

Measured memory_bytes: 60652