#include<bits/stdc++.h>
using namespace std;
typedef tuple<int,int,int>T;
int main(){
    int h,w;
    cin>>h>>w;
    h+=4,w+=4;
    int sx,sy,gx,gy;
    cin>>sx>>sy>>gx>>gy;
    sx++,sy++,gx++,gy++;
    vector<vector<char>>s(h,vector<char>(w,'#'));
    for(int i=2;i<h-2;i++)for(int j=2;j<w-2;j++)cin>>s[i][j];
    deque<T>q;
    q.push_front(T(sx,sy,0));
    int dx[5]={-2,-1,0,1,2},dy[5]={-2,-1,0,1,2};
    while(q.size()){
        T t=q.front();
        q.pop_front();
        int x=get<0>(t),y=get<1>(t),d=get<2>(t);
        if(s[x][y]=='#')continue;
        s[x][y]='#';
        if(x==gx && y==gy){
            cout<<d<<endl;
            return 0;
        }
        if(s[x+1][y]=='.')q.push_front(T(x+1,y,d));
        if(s[x-1][y]=='.')q.push_front(T(x-1,y,d));
        if(s[x][y+1]=='.')q.push_front(T(x,y+1,d));
        if(s[x][y-1]=='.')q.push_front(T(x,y-1,d));
        for(int i=0;i<5;i++){
            for(int j=0;j<5;j++){
                if(s[x+dx[i]][y+dy[j]]=='.')q.push_back(T(x+dx[i],y+dy[j],d+1));
            }
        }
    }
    cout<<"-1"<<endl;
    return 0;
}