#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multi_set;

#define mod         1000000007
#define pi          3.1415926535898
#define eps         1e-9


#define fast       ios::sync_with_stdio(0); cin.tie(0);cout.tie(0)
#define vt vector
#define ar array
#define fs          first
#define sc          second
#define pb         push_back
#define sp          printf(" ")
#define nl          '\n'
#define all(a) a.begin(),a.end()
#define unique(c) (c).resize(unique(all(c)) - (c).begin())
#define sz(x) (int)(x).size()
#define revsort(x) sort(all(x));reverse(all(x))

#define set0(a)     memset(a,0,sizeof(a))
#define setneg(a)   memset(a,-1,sizeof(a))
#define setinf(a)   memset(a,126,sizeof(a))

#define REP(i,start,end) for (int i = start; i <=end; i++)
#define RREP(i,end,start) for (int i=end; i>=start ;i--)
#define EACH(x, a) for (auto& x: a)

typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<LL> vll;
typedef vector<double> vd;
typedef vector<vector<LL> > matrix;
typedef vector<vector<int> > graph;
LL INF=1e9;


 
template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vt<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}

LL powm(LL x, LL y,LL m)
{
    if(y==0)
        return 1;
    if(y==1)
        return x%m;
    if(y&1)
        return (x*(powm((x*x)%m,y/2,m)))%m;
    return powm((x*x)%m,y/2,m)%m;
}

LL inv(LL x, LL m)
{
    return powm(x,m-2,m);
}

int add(int var1, int var2){
    return (((((LL)(var1)) % mod + (LL)(var2)) % mod) + mod) % mod;
}


int mul(int var1, int var2){
    return (((LL)(var1)) % mod * (LL)(var2)) % mod;
}


int h,w;
vector<string> maze;
int dist[1005][1005];
int visited[1005][1005];

int sx,sy,dx,dy;
int adjx[]={1,-1,0,0};
int adjy[]={0,0,1,-1};

bool valid(int x, int y)
{
    return (x >= 0 && x <h and y >= 0 && y < w);
}

void solve()
{
    read(h,w);
    read(sx,sy,dx,dy);
    sx--;
    sy--;
    dx--;
    dy--;

    maze.resize(h);
    read(maze);

    REP(i,0,h-1) REP(j,0,w-1)
    {   
        dist[i][j]=INF;
        visited[i][j]=0;
    } 

    deque <pii> dq;
    dist[sx][sy]=0;
    visited[sx][sy]=1;
    dq.push_back({sx,sy});

    while(!dq.empty())
    {
        auto nx = dq.front();
        dq.pop_front();

        int cx= nx.first;
        int cy= nx.second;
        if(visited[cx][cy]==2) continue;
        visited[cx][cy]=2;
        
        REP(i,-2,2) REP(j,-2,2)
        {
            int cx1= cx+i;
            int cy1= cy+j;

            if(valid(cx1,cy1) and maze[cx1][cy1]=='.' and visited[cx1][cy1]!=2)
            {
                dist[cx1][cy1]=min(dist[cx1][cy1],1+dist[cx][cy]);
                visited[cx1][cy1]=1;
                dq.push_back({cx1,cy1});
            }

        }

        REP(i,0,3)
        {
            int cx1=cx+adjx[i];
            int cy1=cy+adjy[i];

            if(valid(cx1,cy1) and maze[cx1][cy1]=='.' and visited[cx1][cy1]!=2)
            {
                dist[cx1][cy1]=min(dist[cx1][cy1],dist[cx][cy]);
                visited[cx1][cy1]=1;
                dq.push_front({cx1,cy1});
            }
        }
    }
    
    if(dist[dx][dy]==INF)
        dist[dx][dy]=-1;
    
    cout<<dist[dx][dy]<<nl;

    
}
int main()
{
    fast;
        solve();
}


Measured memory_bytes: 12952