#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
 
//krishrawat
 
 
//----------------------------MACROS-------------------------------------------------
#define pb insert//                                                                 |
#define eb emplace_back//                                                           |
#define ff first//                                                                  |
#define ss second//                                                                 |
typedef long long int ll;//                                                         |
typedef long double ld;//                                                           |
#define all(a) a.begin(), a.end()//                                                 |
#define show(x) cerr << #x << " is " << x << "\n"//                                 |
#define show2(x, y) cerr << #x << " " << #y << " " << x << " " << y << "\n"//       |
typedef vector<ll> vl;//                                                            |
typedef vector<vl> vvl;//                                                           |
typedef pair<ll, ll> pp;//                                                          |
typedef vector<pp> vp;//                                                            |
typedef map<pp, ll> ipi;//                                                          |
typedef map<pp, char> ipc;//                                                        |
typedef map<ll, ll> ii;//                                                           |
typedef set<ll> sl;//                                                               |
typedef multiset<ll> msl;//                                                         |
typedef map<char, ll> ci;//                                                         |
typedef set<pair<ll, ll>> sp;//                                                     |
const ll mod = 1e9 + 7;//                                                           |
const ll N = 1e3 + 1;//                                                             |
//----------------------------------------------------------------------------------



//solution
void solve()
{   
    
    ll n,m;cin>>n>>m;
    ll a,b;cin>>a>>b;
    char grp[n][m];
    ll dx,dy;cin>>dx>>dy;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            cin>>grp[i][j];
        }
    }
    vp direc;
    direc.eb(-1,0);
    direc.eb(0,-1);
    direc.eb(0,1);
    direc.eb(1,0);
    queue<pp> q;
    int vis[N][N]={0};
    vp magic;
    magic.eb(1,2);
    magic.eb(2,1);
    magic.eb(-1,2);
    magic.eb(1,-2);
    magic.eb(-2,1);
    magic.eb(2,-1);
    magic.eb(-1,-2);
    magic.eb(-2,-1);
    magic.eb(1,1);
    magic.eb(-1,-1);
    magic.eb(2,2);
    magic.eb(-2,-2);
    magic.eb(-2,0);
    magic.eb(2,0);
    magic.eb(0,2);
    magic.eb(0,-2);
    magic.eb(-1,1);
    magic.eb(1,-1);
    magic.eb(-2,2);
    magic.eb(2,-2);
    int f=1;
    vp tmp;
    a--;b--;dx--;dy--;
    tmp.eb(a,b);
    ll ans=-1;
    int flag=0;
    int cnt=0;
    while(f){
        f=0;
        for(auto i:tmp){
            q.push(i);
            vis[i.ff][i.ss]=1;
        }
        tmp.clear();
        vp lol;
        while(!q.empty()){
            pp ft=q.front();q.pop();
            lol.eb(ft);
            if(ft.ff==dx && ft.ss==dy){
                ans=cnt;
                flag=1;
                break;
            }
            for(auto i:direc){
                ll x=i.ff+ft.ff,y=i.ss+ft.ss;
                
                if(x<n && x>=0 && y<m && y>=0 && !vis[x][y] && grp[x][y]=='.'){
                    q.push({x,y});
                    vis[x][y]=1;
                }
            }
        }
        if(flag) break;
        for(auto i:lol){
            for(auto j:magic){
                ll x=i.ff+j.ff,y=i.ss+j.ss;
                if(x<n && x>=0 && y<m && y>=0 && !vis[x][y] && grp[x][y]=='.'){
                    tmp.eb(x,y);
                    vis[x][y]=1;
                }
            }
        }
        if(!tmp.empty()) f=1;
        cnt++;
    }
    cout<<ans<<"\n";
}
 
 
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
    
    ll t; t=1;
    //cin>>t;
    while(t--) solve(); return 0;
}

Measured memory_bytes: 8704