#include<bits/stdc++.h>
using namespace std;
class IN{
public:
    template<typename T>
    IN& operator >>(T &x){read(x);return *this;}
    IN& operator >>(double &x){scanf("%lf",&x);return *this;}
    IN& operator >>(char *s){scanf("%s",s);return *this;}
    IN& operator >>(char &c){c=getchar();return *this;}
private:
    template<typename T>
    void read(T &x){
        T f=1;int c=getchar();x=0;
        while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
        while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();
        x*=f;
    }
}in;
class NL{}nl;
class BK{}bk;
class OUT{
public:
    template<typename T>
    OUT& operator <<(T x){write(x);return *this;}
    OUT& operator <<(const char *s){printf("%s",s);return *this;}
    OUT& operator <<(char c){putchar(c);return *this;}
    OUT& operator <<(BK x){putchar(' ');return *this;}
    OUT& operator <<(NL x){putchar('\n');return *this;}
private:
    template<typename T>
    void write(T x){
        if(x<0)putchar('-'),x=-x;
        if(x>=10)write(x/10);
        putchar(x%10+48);
    }
}out;

#define pb(x) push_back(x)
#define fi first
#define se second
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define mem(a,x) memset(a,x,sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PL;
typedef vector<int> VI;
typedef vector<ll> VL;
/*****************************************************************************************************/
ll Pow(ll a,ll b,ll mod){ll res=1;a%=mod;while(b){if(b&1)(res*=a)%=mod;(a*=a)%=mod;b>>=1;}return res%mod;}
ll inv(ll a,ll p){if(a==1)return 1;return (p-p/a)*inv(p%a,p)%p;}//p为素数,a<p
ll C(ll n,ll m,ll p){ll res=1;for(ll i=1,j=n-m+1;i<=m;++i,++j){res=res*inv(i,p)%p*j%p;}return res;}
/*****************************************************************************************************/
/*ll ask(int x){ll res=0;while(x)res+=c[x],x-=x&-x;return res;}
void add(ll v,int x){while(x<=n)c[x]+=v,x+=x&-x;}*/
const int p=1e9+7;
const int N=1005;

char s[N][N];

int x,y,xx,yy;
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};

int cnt[N][N];
int vis[N][N];
int h,w;
struct  Node
{
    Node(int x,int y):x(x),y(y){}
    int x,y;
};
int INF=0x3f3f3f3f;
inline bool check(int uu,int vv){
    return (uu>=1&&uu<=h&&vv>=1&&vv<=w);
}
int bfs(){

    memset(cnt,0x3f,sizeof(cnt));
    deque<Node> q;
    cnt[x][y]=0;
    q.push_front(Node(x,y));
    while(!q.empty()){
        int u=q.front().x;
        int v=q.front().y;
        q.pop_front();
        if(vis[u][v])continue;
        vis[u][v]=1;
        rep(i,0,3){
            int uu=u+dx[i];
            int vv=v+dy[i];
            if(!check(uu,vv))continue;
            if(s[uu][vv]=='#')continue;
            if(cnt[uu][vv]>cnt[u][v]){
                cnt[uu][vv]=cnt[u][v];
                q.push_front(Node(uu,vv));
            }
        }
        rep(i,-2,2)
            rep(j,-2,2){
                if(i==0&&j==0)continue;
                int uu=u+i;
                int vv=v+j;
                if(!check(uu,vv))continue;
                if(s[uu][vv]=='#')
                    continue;

                if(cnt[uu][vv]>cnt[u][v]+1){
                    cnt[uu][vv]=cnt[u][v]+1;
                    q.push_back(Node(uu,vv));
                }
            }
    }
    return cnt[xx][yy]==INF?-1:cnt[xx][yy];
}

int main(){
    in>>h>>w;
    in>>x>>y>>xx>>yy;
    rep(i,1,h)in>>(s[i]+1);
    

    out<<bfs();
    /*rep(i,1,h){
        out<<nl;
        rep(j,1,w)out<<cnt[i][j]<<bk;

    }*/
    return 0;
}

Measured memory_bytes: 12548