#include <bits/stdc++.h>
#include <iostream>
#include <numeric> 
#define ll long long
#define mod 1000000007
#define mod1 998244353 
using namespace std;
//vector<int>adj[200000+1];
int cnt=0;
vector<int>cand;
bool prime[100006]; 
  void SieveOfEratosthenes(int n) 
{ 
   
    memset(prime, true, sizeof(prime)); 
  
    for (int p=2; p*p<=n; p++) 
    { 
        if (prime[p] == true) 
        { 
            for (int i=p*p; i<=n; i += p) 
                prime[i] = false; 
        } 
    } 
    
} 
int prob=0;
void dfs(vector<int>adj[],int v, bool visited[],stack<int>&s,bool visited2[])
{
    
    visited[v]=true;
    int i;
    int c=1;
    bool f=true;
    for(i=0;i<adj[v].size();i++)
    {
        if(!visited[adj[v][i]])
        {    
            f=false;
            dfs(adj,adj[v][i],visited,s, visited2);    
        }
    }
    if(adj[v].size()==1)
   { s.push(v); visited2[v]=true;}
}
long long power(long long x, long long  y )
{  
    long long res = 1;     
    x = x % mod; 
    if (x == 0) return 0; 
    while (y > 0)  
    {  
        if (y & 1)  
            res = (res*x) % mod;  
        y = y>>1; 
        x = (x*x) % mod;  
    }  
    return res;  
}  
long long add(long long x, long long y)
{
    x += y;
    while(x >= mod) x -= mod;
    while(x < 0) x += mod;
    return x;
}
 
long long mul(long long x, long long y)
{
    return (x * 1ll * y) % mod;
}
 
long long binpow(long long x, long long y)
{
    long long z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}
 
long long inv(long long x)
{
    return binpow(x, mod - 2);
}
 
long long divide(long long x, long long y)
{
    return mul(x, inv(y));
}
 
long long fact[100005];
 
void precalc()
{
    fact[0] = 1;
    for(long long i = 1; i < 100005; i++)
        fact[i] = mul(fact[i - 1], i);
}
 
long long C(long long n, long long k)
{
    if(n==0)
    return 1;
    return divide(fact[n], mul(fact[k], fact[n - k]));
}
int dfs(bool visited[], vector<int> adj[], int v)
{
    int c=1;
    visited[v]=true;
    for(int i=0;i<adj[v].size();i++)
    {
        if(!visited[adj[v][i]])
        {
            c+=dfs(visited,adj,adj[v][i]);
        }
    }
    return c;
}
struct comp
{
    bool operator()(const pair<int,int>& p1, const pair<int,int>&p2)
    {
        if(p1.first==p2.first)
        {
            return p1.second<p2.second;
        }
        else 
        return p1.first>p2.first;
    }
};
long long sub(long long A,long long B)
{
    return (A-B+mod)%mod;
}
bool topflag=true;
void toposort(string& order, int v, int visited[], vector<int>adj[])
{
    visited[v]=1;
    int i;
    for(i=0;i<adj[v].size();i++)
    {
        if(!visited[adj[v][i]])
        {
            toposort(order,adj[v][i],visited,adj);
        }
        else 
        if(visited[adj[v][i]]==1)
        {
           // cout<<(char)(v+97)<<"\n";
            topflag=false;
        }
    }
    order+=(v+97);
    visited[v]=2;
}
ll dfs(ll v, vector<ll>adj[], ll visited[], vector<ll>&edges,ll n)
{
    ll x=1;
    ll i;
    visited[v]+=1;
    vector<ll>e;
    for(i=0;i<adj[v].size();i++)
    {
        if(!visited[adj[v][i]])
        {
            ll temp=dfs(adj[v][i],adj,visited,edges,n);
            edges.push_back(temp*(n-temp));
            x+=temp;
        }
    }
   
    return x;
    
}
char A[1000][1000];
int B[1000][1000];
void bfs(queue<pair<int,int> >q, int no, int h, int w)
{
    while(!q.empty())
    {
        int i=q.front().first;
        int j=q.front().second;
        q.pop();
        A[i][j]='#';
        B[i][j]=no;
        if(i+1<h && A[i+1][j]=='.')
        {
            A[i+1][j]='#';
            q.push({i+1,j});
        }
        if(i-1>=0 && A[i-1][j]=='.')
        {
            A[i-1][j]='#';
            q.push({i-1,j});
        }
        if(j+1<w && A[i][j+1]=='.')
        {
            A[i][j+1]='#';
            q.push({i,j+1});
        }
        if(j-1>=0 && A[i][j-1]=='.')
        {
            A[i][j-1]='#';
            q.push({i,j-1});
        }

    }
}
int main()
{
 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cout<<fixed<<setprecision(10);
    cerr<<fixed<<setprecision(5);
    int t=1;
    //SieveOfEratosthenes(100005);
    //precalc();
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //cin>>t;
    while(t--)
    {
     int h,w;
     cin>>h>>w;
     int a,b;
     cin>>a>>b;
     int c,d;
     cin>>c>>d;
     int i,j;
     for(i=0;i<h;i++)
     {
         for(j=0;j<w;j++)
         {
             cin>>A[i][j];
             B[i][j]=-1;
         }
     }
     
     int no=1;
      for(i=0;i<h;i++)
     {
         for(j=0;j<w;j++)
         {
            if(A[i][j]=='.')
            {
                A[i][j]='#';
                queue<pair<int,int>>q;
                q.push({i,j});
                bfs(q,no,h,w);
                no++;
            }
         }
     }
    //  for(i=0;i<h;i++)
    //  {
    //      for(j=0;j<w;j++)
    //      cout<<B[i][j]<<" ";
    //      cout<<"\n";
    //  }
    int start=B[a-1][b-1];
    int stop=B[c-1][d-1];
    
    set<int>adj[no+1];
    for(i=0;i<h;i++)
    {
        for(j=0;j<w;j++)
        {
         queue<pair<int,int> >q3;
         set<pair<int,int> >s2;
         s2.insert({i,j});
         q3.push({i,j});
         if(B[i][j]==-1)
         continue;
         while(!q3.empty())
         {
             int i1=q3.front().first;
             int i2=q3.front().second;
             q3.pop();
             if(B[i1][i2]!=B[i][j] && B[i1][i2]!=-1)
             {
                 adj[B[i][j]].insert(B[i1][i2]);
                 adj[B[i1][i2]].insert(B[i][j]);
             }
             if(s2.find({i1+1,i2})==s2.end() && i1+1<h && (abs(i1+1-i)<=2 && abs(i2-j)<=2) )
             {
                 s2.insert({i1+1,i2});
                 q3.push({i1+1,i2});
             }
               if(s2.find({i1-1,i2})==s2.end() && i1-1>=0 &&  (abs(i1-1-i)<=2 && abs(i2-j)<=2))
             {
                 s2.insert({i1-1,i2});
                 q3.push({i1-1,i2});
             }
               if(s2.find({i1,i2+1})==s2.end() && i2+1<w &&  (abs(i1-i)<=2 && abs(i2+1-j)<=2))
             {
                 s2.insert({i1,i2+1});
                 q3.push({i1,i2+1});
             }
               if(s2.find({i1,i2-1})==s2.end() && i2-1>=0 &&  (abs(i1-i)<=2 && abs(i2-1-j)<=2))
             {
                 s2.insert({i1,i2-1});
                 q3.push({i1,i2-1});
             }
             
         }
        }
    }
    // for(i=1;i<=no;i++)
    // {
    //     for(auto it:adj[i])
    //     cout<<it<<" ";
    //     cout<<"\n";
    // }
    queue<pair<int,int>>q4;
    q4.push({start,0});
    bool visited[no+1];
    memset(visited,false,sizeof(visited));
    visited[start]=true;
    ll ans=-1;
    while(!q4.empty())
    {
        int num=q4.front().first;
        int co=q4.front().second;
        q4.pop();
        if(num==stop)
        ans=co;
        for(auto it: adj[num])
        {
            if(!visited[it])
            {
                visited[it]=true;
                q4.push({it,co+1});
            }
        }
    }
     cout<<ans<<"\n";
    }
    return 0;
} 

Measured memory_bytes: 32828