#include<bits/stdc++.h>
using namespace std;
 
template <class T1, class T2>
ostream& operator << (ostream& out, const pair <T1, T2> p)
{
    out << '(' << p.first << ',' << p.second << ')';
        return out;
}
 
template <class T1, class T2>
istream& operator >> (istream& in, pair<T1, T2> &p)
{
    in >> p.first >> p.second;
        return in;
}
 
template <class T>
istream& operator >> (istream& in, vector<T> &v)
{
    for (T &x : v)
        in >> x;
    return in;
}
 
template <class T>
ostream& operator << (ostream& out, const vector<vector<T> > &v)
{
    for (const vector<T> &x : v)
        out << x << '\n';
    return out;
}
 
template <class T>
ostream& operator << (ostream& out, const vector<T> &v)
{
    for (const T &x : v)
        out << x << ' ';
    return out;
}
 
template <class T>
ostream& operator << (ostream& out,const set<T> &s)
{
    for (const T &x : s)
        out << x << ' ';
    return out;
}
 
 
#define ll long long
#define lf long double
#define pb push_back
#define mp make_pair
#define F first
#define S second
typedef pair <ll,ll > ii;
typedef vector <ll> vi;
typedef vector <ii> vii;
typedef vector <bool> vb;
typedef vector <vector <ll> > vv;
typedef vector <vector <vector<ll> > > vvv;
typedef vector <string> vs;
typedef vector <lf> vf;
#define sz(x) (ll)(x).size()
#define tr1(x) cerr << #x << ": " << x << nl
#define tr2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << nl
#define tr3(x,y,z) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<nl
#define tr4(x,y,z,w) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<" | "<<#w<<": "<<w<<nl
#define epsilon 0.00000001L
#define inf 9999999999999999ll// ~ 10^16
#define nl "\n"
#define all(x) x.begin(), x.end()
#define r_all(x) x.rbegin(), x.rend()
#define fast()  ios_base::sync_with_stdio(false);cin.tie(NULL)
#define mod 1000000007ll
#define modulo 998244353ll
ll power(ll a,ll b){return b?power(a*a%mod,b/2)*(b%2?a:1)%mod:1;}

ll n,m;
bool valid(ll x,ll y)
{
    return x>=0 && x<n && y>=0 &&y<m;
}
void bfs(ll x11,ll y11,vs &s,vv &col,ll color)
{
    queue <ii>q;
    q.push({x11,y11});
    col[x11][y11]=color;
    while(!q.empty())
    {
        ll x1=q.front().F;
        ll y1=q.front().S;
        q.pop();
        
        int x[]={0,0,1,-1};
        int y[]={1,-1,0,0};
        for(int i=0;i<4;i++)
        {
            ll x2=x1+x[i];
            ll y2=y1+y[i];
            if(valid(x2,y2) && col[x2][y2]==-1 && s[x2][y2]!='#')
            {
                col[x2][y2]=color;
                q.push({x2,y2});
            }
        }
    }
}
int main()
{
    fast();
    cin>>n>>m;
    ll s1,s2,e1,e2;
    cin>>s1>>s2>>e1>>e2;
    s1--;s2--;e1--;e2--;

    vs s(n);
    cin>>s;
    vv col(n,vi(m,-1));
    ll color=0;
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(col[i][j]==-1 && s[i][j]=='.')
            {
                bfs(i,j,s,col,color);
                color++;
            }
        }
    }
    vector <set <ll>> e(color,set <ll>());
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
        {
            if(s[i][j]=='.')
            {
                for(ll x=-2;x<=2;x++)
                {
                    for(ll y=-2;y<=2;y++)
                    {
                        ll x1=i+x;
                        ll y1=j+y;
                        if(valid(x1,y1) && s[x1][y1]=='.')
                        {
                            ll col1=col[i][j];
                            ll col2=col[x1][y1];
                            if(col1!=col2)
                            {
                                e[col1].insert(col2);
                                e[col2].insert(col1);
                            }
                        }
                    }
                }
            }
        }
    }
    ll c1=col[s1][s2];
    ll c2=col[e1][e2];
    queue <ll>q;
    q.push(c1);
    vi dist(color,-1);
    dist[c1]=0;
    while(!q.empty())
    {
        ll u=q.front();
        q.pop();
        for(ll v:e[u])
        {
            if(dist[v]==-1)
            {
                dist[v]=dist[u]+1;
                q.push(v);
            }
        }
    }
    cout<<dist[c2]<<nl;
}


Measured memory_bytes: 37240