#include <bits/stdc++.h>
  
using namespace std;
 
#define endl "\n"
#define JAM(x) cout<< #x <<" = "<<x<<"\n"
#define II() ( { int a ; read(a) ; a; } )
#define LL() ( { ll a ; read(a) ; a; } )
#define DD() ({double a; scanf("%lf", &a); a;})
 
 
//Fast Reader
template<class T>inline bool read(T &x){
    int c=getchar();int sgn=1;
    while(~c&&c<'0'||c>'9'){if(c=='-')sgn=-1;c=getchar();}
    for(x=0;~c&&'0'<=c&&c<='9';c=getchar())x=x*10+c-'0'; 
    x*=sgn; return ~c;
}
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> PII;
typedef vector<PII> VPI;
 
const int INF=2e9;
const int MX=100005;
const int MOD=1000000007;
const double PI=acos(-1.0);

vector<int> sieve(100001);


long long int power(long long int a,long long int b)
{
    a %= MOD;
    long long int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}
bool isprime(int n)
{
    if(n==1)
    return 0;
    int i;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        return false;
    }
    return true;
}

void SIEVE()
{
    int i,j;
    for(i=0;i<=100000;i++)
    sieve[i]=i;
    for(i=2;i*i<=100000;i++)
    {
        if(sieve[i]!=i)
        continue;
        for(j=i*i;j<=100000;j=j+i)
        {
            if(sieve[j]==j)
            sieve[j]=i;
        }
    }
}


/*_______________________________________________*/
int n,m;
int dx[]={1,-1,0,0};
int dy[]={0,0,-1,1};
bool isvalid(int x,int y)
{
    if(x<0 || x>=n || y<0 || y>=m)
    return false;
    return true;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>m;
    vector<vector<int>> times(n,vector<int>(m,INT_MAX));
    vector<vector<char>> grid(n,vector<char>(m));
    int i,j;
    int sx,sy,ex,ey;
    cin>>sx>>sy;
    cin>>ex>>ey;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        cin>>grid[i][j];
    }
    priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> q;
    q.push({0,{sx-1,sy-1}});
    times[sx-1][sy-1]=0;
    while(!q.empty())
    {
        pair<int,pair<int,int>> cur=q.top();
        q.pop();
        if(cur.second.first==ex-1 && cur.second.second==ey-1)
        break;
        for(i=0;i<4;i++)
        {
            int x=cur.second.first+dx[i];
            int y=cur.second.second+dy[i];
            if(isvalid(x,y) && times[x][y]>times[cur.second.first][cur.second.second] && grid[x][y]=='.')
            {
                times[x][y]=times[cur.second.first][cur.second.second];
                q.push({times[x][y],{x,y}});
            }
        }
        for(i=-2;i<=2;i++)
        {
            for(j=-2;j<=2;j++)
            {
                int x=cur.second.first+i;
                int y=cur.second.second+j;
                if(isvalid(x,y) && times[x][y]>(times[cur.second.first][cur.second.second]+1) && grid[x][y]=='.')
                {
                    times[x][y]=times[cur.second.first][cur.second.second]+1;
                    q.push({times[x][y],{x,y}});
                }
            }
        }
    }
    if(times[ex-1][ey-1]!=INT_MAX)
    cout<<times[ex-1][ey-1];
    else
    {
        cout<<"-1";
    }
    
}


Measured memory_bytes: 9004