#include<bits/stdc++.h>
using namespace std;
typedef long long ll; 
typedef pair<int, int> pii; 
typedef pair<ll, ll> pll; 
typedef pair<string, string> pss; 
typedef vector<int> vi; 
typedef vector<vi> vvi; 
typedef vector<pii> vii; 
typedef vector<pll> vll; 
typedef vector<ll> vl; 
typedef vector<vl> vvl; 
int s,e,qs,qe, n,m;
vector<vector<bool>>a;
vector<vector<bool>>mark;
vii final;
vii grid;
vii directions;
bool isValid(int x, int y){
    if(x<1 || x>n || y<1 || y>m)return 0;
    else return 1;
}
int gridDfs(){
    deque<pii>q;
    for(int i=0;i<final.size();i++)q.push_back(final[i]);
    final.clear();
    while(q.empty()==false){
        int qss=q.size();
        for(int i=0;i<qss;i++){
            pii p = q.front();
            q.pop_front();
            int l=p.first,r=p.second;
            for(int j=0;j<24;j++){
                int x,y;
                x=l+grid[j].first;
                y=r+grid[j].second;
                if(isValid(x,y)&& mark[x][y]==false && a[x][y]){
                    mark[x][y]=true;
                    // cout<<x<<"->"<<y<<" "<<qs<<"->"<<qe<<" ";
                    if(x==qs&&y==qe)return 0;
                    final.push_back({x,y});
                }
            }
        }
    }
    // cout<<endl;
    // for(int i=0;i<final.size();i++)cout<<final[i].first<<"->"<<final[i].second<<" ";cout<<endl;
    if(final.size()==0)return -1;
    for(int i=0;i<final.size();i++)q.push_back(final[i]);
    while(q.empty()==false){
        pii p = q.front();
        q.pop_front();
        int l=p.first,r=p.second;
        for(int j=0;j<4;j++){
            int x,y;
            x=l+directions[j].first;
            y=r+directions[j].second;
            if(isValid(x,y)&& mark[x][y]==false && a[x][y]){
                mark[x][y]=true;
                if(x==qs&&y==qe)return 0;
                q.push_back({x,y});
                final.push_back({x,y});
            }
        }
    }
    int ans=gridDfs();
    if(ans==-1)return -1;
    else return 1+ans;
}
int dfs(int s, int e){
    deque<pii>q;
    q.push_back({s,e});
    final.push_back({s,e});
    mark[s][e]=true;
    while(q.empty()==false){
        pii p = q.front();
        q.pop_front();
        int l=p.first,r=p.second;
        for(int j=0;j<4;j++){
            int x,y;
            x=l+directions[j].first;
            y=r+directions[j].second;
            // cout<<x<<"->"<<y<<endl;
            // cout<<isValid(x,y)<<endl;
            if(isValid(x,y)&& mark[x][y]==false && a[x][y]){
                mark[x][y]=true;
                // cout<<x<<"->"<<y<<endl;
                if(x==qs&&y==qe)return 0;
                q.push_back({x,y});
                final.push_back({x,y});
            }
        }
    }
    // for(int i=0;i<final.size();i++)cout<<final[i].first<<"->"<<final[i].second<<" ";cout<<endl;
    int ans=gridDfs();
    if(ans==-1)return -1;
    else return 1+ans;
}
int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout); 
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL) ; cout.tie(NULL);
    int i,j,k;
    cin>>n>>m;
    directions.assign(4,{0,0});
    grid.assign(24,{0,0});int count=0;
    for(i=1;i<=5;i++){
        for(j=1;j<=5;j++){
            int p=i-3,q=j-3;
            if(p||q) {
                grid[count]={p,q};
                count++;
            }
        }
    }
    // for(i=0;i<24;i++)cout<<grid[i].first<<"->"<<grid[i].second<<" ";cout<<endl;
    directions[0]={1,0};
    directions[1]={0,1};
    directions[2]={-1,0};
    directions[3]={0,-1};
    a.assign(n+1, vector<bool>(m+1));
    mark.assign(n+1, vector<bool>(m+1, false));
    cin>>s>>e;
    cin>>qs>>qe;
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            char c;
            cin>>c;
            if(c=='.')a[i+1][j+1]=true;
            else a[i+1][j+1]=false;
        }
    }
    cout<<dfs(s,e)<<endl;
    return 0;
}

Measured memory_bytes: 134064