#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,k,l,x,y,a,b,ans;
char ch[1010][1010];
bool book[1010][1010];
int ne[4][2]={1,0,-1,0,0,1,0,-1};
struct node{
    int x,y,z;
};
void slove(){
    cin>>n>>m>>a>>b>>x>>y;
    ans=1e18;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>ch[i][j];
        }
    }
    queue<node>q;
    q.push({a,b,0});
    book[a][b]=true;
    while(q.size()){
        node t1=q.front();
        q.pop();
        if(t1.x==x && t1.y==y){
            ans=min(ans,t1.z);
        }
        for(int i=0;i<4;i++){
            int xx=t1.x+ne[i][0];
            int yy=t1.y+ne[i][1];
            if(xx>=1 && xx<=n && yy>=1 && yy<=m && !book[xx][yy] && ch[xx][yy]=='.'){
                q.push({xx,yy,t1.z+1});
                book[xx][yy]=true;
            }
        }
    }
    if(ans!=1e18) cout<<ans<<'\n';
    else cout<<-1<<'\n';
}
signed main() {
    int a, b;
    int T=1;
    while(T--){
        slove();
    }
}
// 64 位输出请用 printf("%lld")