#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;   
#define ll long long
#define ull unsigned long long
// #define ordered_set tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> 
ll mod=1e9+7;
#define PI 3.1415926535897932385
#define inf 9e18
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
typedef pair<long long, long long> ii;
#define S second
#define F first
ll max(ll a,ll b){if(a>b){return a;}return b;}
ll min(ll a,ll b){if(a<b){return a;}return b;}
#define MAXN 200005
// Comment this out for interactice problem
// #define endl '\n'
// string to integer stoi() Remember: it takes string not character
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
 
// To compile--> g++ -std=c++0x -o output one.cpp
// To run--> ./output
ll n,m;
ll ch,cw;
ll dh,dw;
char a[1005][1005];
ll vis[1005][1005];

bool isSafe(ll r, ll c){
    if(r>=0 && c>=0 && r<n && c<m){
        return true;
    }
    return false;
}

ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0, 0, -1, 1};
ll hdx[24] = {-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,0,0,0,0,1,1,1,1,1,2,2,2,2,2};
ll hdy[24] = {-2,-1,0,1,2,-2,-1,0,1,2,-2,-1,1,2,-2,-1,0,1,2,-2,-1,0,1,2};


int main()
{ 
    fastio;
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    cin>>n>>m>>ch>>cw>>dh>>dw;
    ch--;
    cw--;
    dh--;
    dw--;
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < m; ++j)
        {
            cin>>a[i][j];
            vis[i][j]=false;
        }
    }

    queue<pair<ii, ll>> q;
    ll ans=0;
    q.push({{ch, cw}, ans});
    vis[ch][cw]=1;
    ll fg=0;
    while(true){
        fg=0;
        vector<ii> v1;
        while(!q.empty()){
            ll r = q.front().F.F;
            ll c = q.front().F.S;
            ll jump=q.front().S;
            v1.push_back({r, c});
            q.pop();

            for (int i = 0; i < 4; ++i)
            {
                if(isSafe(r+dx[i], c+dy[i])){
                    if(!vis[r+dx[i]][c+dy[i]]){
                        if(a[r+dx[i]][c+dy[i]]=='.'){
                            vis[r+dx[i]][c+dy[i]]=1;
                            q.push({{r+dx[i], c+dy[i]}, jump});
                        }
                    }
                }   
            }
        }

        if(v1.empty()){
            fg=-1;
            break;
        }
        for (int i = 0; i < v1.size(); ++i)
        {
            if((v1[i].F==dh) && (v1[i].S==dw)){
                fg=1;
                break;
            }
        }
        if(fg==1){
            break;
        }
        ans++;
        for (int i = 0; i < v1.size(); ++i)
        {
            for (int j = 0; j < 24; ++j)
            {
                if(isSafe(v1[i].F+hdx[j], v1[i].S+hdy[j])){
                    if(!vis[v1[i].F+hdx[j]][v1[i].S+hdy[j]]){
                        if(a[v1[i].F+hdx[j]][v1[i].S+hdy[j]]=='.'){
                            vis[v1[i].F+hdx[j]][v1[i].S+hdy[j]]=1;
                            q.push({{v1[i].F+hdx[j], v1[i].S+hdy[j]}, ans});
                        }
                    }       
                }
            }
        }
    }

    if(fg==-1){
        cout<<-1;
    }
    else{
        cout<<ans;
    }

    return 0;
}

Measured memory_bytes: 12780