#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define mp(a,b) make_pair(a,b)
#define vi vector<int>
#define mii map<int,int>
#define mpi map<pair<int,int>,int>
#define vp vector<pair<int,int> >
#define pb(a) push_back(a)
#define fr(i,n) for(i=0;i<n;i++)
#define rep(i,a,n) for(i=a;i<n;i++)
#define F first
#define S second
#define endl "\n"
#define fast std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define dom 998244353
#define pi 3.14159265
#define sl(a) (int)a.length()
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define pii pair<int,int> 
#define mini 2000000000000000000
#define time_taken 1.0 * clock() / CLOCKS_PER_SEC
//mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
void shikhar7s(int cas)
{
    int n,m,i,j;
    cin>>n>>m;
    int x,y,p,r;
    cin>>x>>y>>p>>r;
    x--;
    y--;
    p--;
    r--;
    char ch;
    int a[n][m],vis[n][m];
    fr(i,n)
    {
        fr(j,m)
        {
            cin>>ch;
            if(ch=='.')
                a[i][j]=1;
            else
                a[i][j]=0;
            vis[i][j]=0;
        }
    }
    int c=1,s,t,v[n][m];
    fr(i,n)
    {
        fr(j,m)
        {
            if(a[i][j]&&!vis[i][j])
            {
                queue<pii> q;
                vis[i][j]=1;
                q.push(mp(i,j));
                while(sz(q))
                {
                    s=q.front().F;
                    t=q.front().S;
                    q.pop();
                    v[s][t]=c;
                    if(s&&!vis[s-1][t]&&a[s-1][t])
                    {
                        vis[s-1][t]=1;
                        q.push(mp(s-1,t));
                    }
                    if(t&&!vis[s][t-1]&&a[s][t-1])
                    {
                        vis[s][t-1]=1;
                        q.push(mp(s,t-1));
                    }
                    if(s<n-1&&!vis[s+1][t]&&a[s+1][t])
                    {
                        vis[s+1][t]=1;
                        q.push(mp(s+1,t));
                    }
                    if(t<m-1&&!vis[s][t+1]&&a[s][t+1])
                    {
                        vis[s][t+1]=1;
                        q.push(mp(s,t+1));
                    }
                }
                c++;
            }
        }
    }
    if(v[x][y]==v[p][r])
    {
        cout<<0;
    }
    else
    {
        vi adj[n*m];
        int viss[n*m]={0},d[n*m]={0},k,l;
        fr(i,n)
        {
            fr(j,m)
            {
                if(a[i][j])
                {
                    rep(k,-2,3)
                    {
                        rep(l,-2,3)
                        {
                            if(i+k>=0&&i+k<n&&j+l>=0&&j+l<m&&a[i+k][j+l]&&v[i][j]!=v[i+k][j+l])
                            {
                                adj[v[i][j]].pb(v[i+k][j+l]);
                                adj[v[i+k][j+l]].pb(v[i][j]);
                            }
                        }
                    }
                }
            }
        }
        queue<int> q;
        viss[v[x][y]]=1;
        q.push(v[x][y]);
        while(sz(q))
        {
            s=q.front();
            q.pop();
            fr(i,sz(adj[s]))
            {
                if(!viss[adj[s][i]])
                {
                    viss[adj[s][i]]=1;
                    d[adj[s][i]]=d[s]+1;
                    q.push(adj[s][i]);
                }
            }
        }
        if(!d[v[p][r]])
        {
            cout<<-1;
        }
        else
        {
            cout<<d[v[p][r]];
        }
    }
}
signed main()
{
    fast;
    int t=1;
    //cin>>t;
    int cas=1;
    while(cas<=t)
    {
    shikhar7s(cas);
    cas++;
    }
    return 0;
}

Measured memory_bytes: 108992