/*
 Author: Vedang Karwa
 */

//#include <bits/stdc++.h>
#include<iostream>
#include <iosfwd>
#include<limits.h>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include<stdio.h>
#include <queue>
#include <stack>
#include <math.h>
#include<string.h>
#include <map>
#include <set>
#include<vector>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define fr(i,j,n)   for(lli i=j;i<(n);++i)
#define bk(i,j,n)   for(lli i=j;i>=n&&j>=n;--i)
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define mem(p,v) memset(p,v,sizeof(p))
#define present(c,x)    ((c).find(x) != (c).end())
#define run ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#define ordered_set tree<lli, null_type,less<lli>, rb_tree_tag,tree_order_statistics_node_update>
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define debug4(x,y,z,a) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<" "<<#a<<" "<<a<<endl;
#define ub upper_bound
#define lb lower_bound
typedef long long int lli;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<lli> vl;
typedef pair<lli,lli> pl;
inline lli max(lli a,lli b){ return a>b?a:b;}
inline lli min(lli a,lli b){ return a<b?a:b;}
const long long inf = 1e18+5;
const lli M =1e9+7 ;
void show(vector<lli> v){fr(i,0,v.size())cout<<v[i]<<" ";cout<<endl;}
void show(vector<int> v){fr(i,0,v.size())cout<<v[i]<<" ";cout<<endl;}
void show(int a[], int arraysize){for (int i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(lli a[], lli arraysize){for (lli i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(bool a[], lli arraysize){for (lli i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(set<int> s){for (auto it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout<<endl;}
void show(map<lli,lli> mp){for (auto it = mp.begin(); it != mp.end(); it++){cout << it->first << " "<<it->second<<endl;}cout<<endl;}
lli modpow(lli x,lli n){lli res=1;while(n>0){if(n&1) res=((res%M)*(x%M))%M;x=((x%M)*(x%M))%M;n>>=1;}return res;}
lli power(lli x, lli n){lli res=1;while(n>0){if(n&1) res=res*x;x=x*x;n>>=1;}return res;}
const int N =1e6+5;
int r[4]={-1,1,0,0};
int c[4]={0,0,-1,1};
/////// ------ Main code starts here ------ /////
//bool visited[N];
lli dp[N];
int g[1001][1001];
int n,m;
int xs,ys,xd,yd;
int util(int i,int j)
{
    return m*i+j;
}
bool valid(int i,int j)
{
    if(i>=0&&i<n&&j>=0&&j<m&&g[i][j])
        return 1;
    return 0;
}
void dfs(int s)
{
    //debug4(s, a,b,dp[s]);
    multiset<pair<lli, lli>> set;
    set.insert({0,s});
    while(set.size())
    {
        pl p=*set.begin();
        set.erase(set.begin());
        s=p.S;
        lli d=p.F;
        if(dp[s]<d)
            continue;
        int a=s/m,b=s%m,t;
       // debug4(s, dp[s], a,b);
        fr(i,0,4)
        {
            int x=a+r[i],y=b+c[i];
            if(valid(x, y))
            {
                t=util(x,y);
                if(dp[t]>dp[s])
                {
                    dp[t]=dp[s];
                    set.insert({dp[t],t});
                }
            }
        }
       // debug2(set.size(), s);
        fr(i,a-2,a+3)
        {
            fr(j,b-2,b+3)
            {
                t=util(i, j);
                if(valid(i, j))
                {
                    if(dp[t]>dp[s]+1)
                    {
                        dp[t]=dp[s]+1;
                        set.insert({dp[t],t});
                    }
                }
            }
        }
        /*
        fr(i,a-2,a+3)
        {
            t=util(i, b+2);
            if(valid(i, b+2))
            {
                if(dp[t]>dp[s]+1)
                {
                    dp[t]=dp[s]+1;
                    set.insert({dp[t],t});
                }
            }
            t=util(i, b-2);
            if(valid(i, b-2))
            {
                if(dp[t]>dp[s]+1)
                {
                    dp[t]=dp[s]+1;
                    set.insert({dp[t],t});
                }
            }
        }
        fr(i,b-2,b+3)
        {
            t=util(a+2, i);
            if(valid(a+2, i))
            {
                if(dp[t]>dp[s]+1)
                {
                    dp[t]=dp[s]+1;
                    set.insert({dp[t],t});
                }
            }
            t=util(a-2, i);
            if(valid(a-2, i))
            {
                if(dp[t]>dp[s]+1)
                {
                    dp[t]=dp[s]+1;
                    set.insert({dp[t],t});
                    
                }
            }
        }
         */
    }
}
void solve()
{
    cin>>n>>m;
    cin>>xs>>ys>>xd>>yd;
    xs--;
    ys--;
    xd--;
    yd--;
    string x;
    fr(i,0,n)
    {
        cin>>x;
        fr(j,0,m)
        {
            if(x[j]=='#')
            {
                g[i][j]=0;
            }
            else
                g[i][j]=1;
            //debug1(c)
        }
    }
    fr(i,0,n*m)
    {
        dp[i]=inf;
    }
    int s=util(xs,ys),d=util(xd, yd);
    dp[s]=0;
    dfs(s);
    if(dp[d]==inf)
    {
        cout<<-1<<endl;
    }
    else
        cout<<dp[d]<<endl;
}
int main()
{
    run;cout<<fixed<<setprecision(0);
    int t;
    t=1;
    while(t){solve();t--;}
    return 0;
}


Measured memory_bytes: 15924