#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
 
#define ll long long
#define ld long double
 
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repb(i,a,b) for(ll i=a;i>=b;i--)
 
#define err() cout<<"=================================="<<endl;
#define errA(A) for(auto i:A)   cout<<i<<" ";cout<<endl;
#define err1(a) cout<<#a<<" "<<a<<endl
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<endl
 
#define pb push_back
#define all(A)  A.begin(),A.end()
#define allr(A)    A.rbegin(),A.rend()
#define ft first
#define sd second
 
#define pll pair<ll,ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>
 
#define endl "\n"
 
const ll logN = 20;
const ll N=1005;
const ll M = 1000000007;
const ll INF = 1e12;
#define PI 3.14159265
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
ll pow1(ll a,ll b){
    ll res=1;
    while(b>0){
        if(b&1){
        	res=(res*a)%M;
		}
        a=(a*a)%M;
        b>>=1;
    }
    return res%M;
}

// struct segtree{
//     ll x,y;
//     ll tree[4*N];

//     void build_tree(vector<ll>&v , ll ind,ll l, ll r)
//     {
//         if(l==r)
//         {
//             tree[ind]=v[l];
//             return;
//         }
//         ll mid=(l+r)/2;
//         build_tree(v,2*ind,l,mid);
//         build_tree(v,2*ind+1,mid+1,r);
//         tree[ind]=max(tree[2*ind],tree[2*ind+1]);
//     }

//     ll query(ll ind, ll l, ll r)
//     {
//         // cout<<l<<" "<<r<<" "<<endl;
//         if(x>r || y<l)return -LLONG_MAX;
//         if(l>=x && r<=y)return tree[ind];
//         ll mid=(l+r)/2;
//         return max(query(2*ind,l,mid),query(2*ind+1,mid+1,r));
//     }
// }obj;

ll vis[N][N],cst1[N][N];
char mat[N][N];
ll n,m;

void find_next(ll x, ll y, ll cst, deque<pair<pair<ll,ll>,ll>>&q)
{
    for(ll x_s=-2;x_s<=2;x_s++)
    {
        for(ll y_s=-2;y_s<=2;y_s++)
        {
            ll n_x=x+x_s,n_y=y+y_s;
            if(n_x>=n || n_y>=m || n_x<0 || n_y<0 || mat[n_x][n_y]=='#' || vis[n_x][n_y]==1)
            continue;
            // err2(n_x,n_y);
            if(vis[n_x][n_y]==0)
            {
                ll n_c=cst;
                ll flg=0;
                if(abs(x_s)+abs(y_s)>1)
                {flg=1;n_c+=1;}
                if(cst1[n_x][n_y]<=n_c)
                {
                    continue;
                }
                cst1[n_x][n_y]=n_c;
                if(flg==1)
                {
                    q.push_back({{n_x,n_y},n_c});
                }
                else 
                {
                    q.push_front({{n_x,n_y},n_c});
                }
            }
        }
    }
}


int main()
{
    fast;

    #ifndef ONLINE_JUDGE
    freopen("../input.txt", "r", stdin);
    freopen("../output.txt", "w", stdout);
    #endif
    
    ll T=1;
//     cin>>T;
    while(T--)
    {
        cin>>n>>m;
        ll sx,sy;
        rep(i,0,N)
        rep(j,0,N)
        cst1[i][j]=INT_MAX;
        cin>>sx>>sy;
        ll tx,ty;
        cin>>tx>>ty;
        sx--;sy--;tx--;ty--;
        rep(i,0,n)
        {
            string tmp;
            cin>>tmp;
            rep(j,0,m)
            {
                mat[i][j]=tmp[j];
            }
        }
        deque<pair<pair<ll,ll>,ll>>q;
        q.push_back({{sx,sy},0});
        cst1[sx][sy]=0;
        while(!q.empty())
        {
            pair<pll,ll>cur=q.front();
            q.pop_front();
            ll x=cur.first.first,y=cur.first.second;
            ll cst=cur.second;
            if(x==tx && y==ty)
            {
                cout<<cst<<endl;
                return 0;
            }
            vis[x][y]=1;
            find_next(x,y,cst,q);
        }

        cout<<"-1"<<endl;
        return 0;



    }

}

Measured memory_bytes: 20656