/*
___ 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];
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;
     }
 }



vector <ll> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};


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];
            ans[i][j]=INF;
        }
    }

    set <pair<ll,pii>> energy;

    energy.insert(mp(0,mp(s1,s2)));

        while (siz(energy))
        {   

            ll en=(*energy.begin()).ff; 
            ll topx=(*energy.begin()).ss.ff;
            ll topy=(*energy.begin()).ss.ss;
            ans[topx][topy]=min(en,ans[topx][topy]);

            if (!vis[topx][topy])
            {
                for(int i=0;i<4;i++)
                {
                    if (check(topx+dx[i],topy+dy[i]))
                    energy.insert(mp(en,mp(topx+dx[i],topy+dy[i])));
                }

                for(int k=-2;k<=2;k++)
                {
                    for(int l=-2;l<=2;l++)
                    {
                        if (k==0 and l==0)
                            continue;
                        else
                        {
                            if (check(topx+k,topy+l)==true)
                            {
                                energy.insert(mp(en+1,mp(topx+k,topy+l)));
                            }      
                        }
                    }
                }

                vis[topx][topy]=true;
                energy.erase(mp(en,mp(topx,topy)));
            } else {
                energy.erase(mp(en,mp(topx,topy)));
            }
        }
        

    

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

return 0;
}

// check all product based operations for integer overflow


Measured memory_bytes: 14292