#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
/*---------------------DEBUGGING--------------------------------------------*/
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
/*-------------------------------------------------------------------------------------*/
//#define mp make_pair
#define pb push_back
#define ll long long
#define pii pair<int,int>
#define pcc pair<char,char>
#define F first
#define S second
#define int long long
#define pi 3.141592653589793238462643383279502
#define M  1000000007
#define rep(i,a,n) for(int i=a;i<n;i++)
#define INF 10000000000000
#define N 200005
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define endl "\n"
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
char a[1005][1005];
int dp[1005][1005];
int vis[1005][1005];
int n,m;
int dx[4]={1,0,0,-1};
int dy[4]={0,-1,1,0};
int dx1[24]={0,0,0,0,1,1,1,1,1,2,2,2,2,2,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2};
int dy1[24]={1,2,-1,-2,0,1,2,-1,-2,0,1,2,-1,-2,0,1,2,-1,-2,0,1,2,-1,-2};
bool issafe(int i,int j)
{
    if(i>=1 && i<=n && j>=1 && j<=m && a[i][j]=='.' && !vis[i][j])
        return true;
    else
        return false;
}
void bfs(int x,int y)
{
    priority_queue<pair<int,pii>,vector<pair<int,pii>>,greater<pair<int,pii>> >q;
    q.push({0,{x,y}});
    while(!q.empty())
    {
        pair<int,pii>p=q.top();
        q.pop();
        if(vis[p.S.F][p.S.S])continue;
        int i=p.S.F;
        int j=p.S.S;
        vis[i][j]=1;
        dp[i][j]=p.F;
        //debug(i,j,dp[i][j]);
        for(int k=0;k<4;k++)
        {
            if(issafe(i+dx[k],j+dy[k]))
            {
                q.push({p.F,{i+dx[k],j+dy[k]}});
            }
        }
        for(int k=0;k<24;k++)
        {
            if(issafe(i+dx1[k],j+dy1[k]))
            {
                q.push({p.F+1,{i+dx1[k],j+dy1[k]}});
            }
        }
    }
}
signed main()
{
   ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
   cin>>n>>m;
   int x_s,y_s;
   cin>>x_s>>y_s;
   int x_e,y_e;
   cin>>x_e>>y_e;
   for(int i=1;i<=n;i++)
   {
       for(int j=1;j<=m;j++)
        cin>>a[i][j];
   }
   memset(dp,-1,sizeof(dp));
   bfs(x_s,y_s);
   cout<<dp[x_e][y_e]<<endl;
}


Measured memory_bytes: 22900