#include <bits/stdc++.h>
namespace Suhaib_Templates {
    using namespace std;
    #define In_The_Name_Of_Allah_The_Merciful ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    #define Alhamdulillah return 0;
    #define debug(n) cerr << "[" << #n << " = " << n << "]" << '\n'
    #define all(v) v.begin(),v.end()
    #define rall(v) v.rbegin(),v.rend()
    #define F_OR(i, a, b, s) for (int i = (a); ((s) > 0 ? i < (b) : i > (b)); i += (s))
    #define F_OR1(e) F_OR(i, 0, e, 1)
    #define F_OR2(i, e) F_OR(i, 0, e, 1)
    #define F_OR3(i, b, e) F_OR(i, b, e, (b)<(e)?1:-1)
    #define F_OR4(i, b, e, s) F_OR(i, b, e, s)
    #define GET5(a, b, c, d, e, ...) e
    #define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
    #define lp(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
    #define each(a,x) for (auto& a: x)
    #define pb push_back
    #define ins insert
    #define sz(v) (int) v.size()
    typedef long long ll;
    typedef long double ld;
    const long double PI = acos(-1);
    const int di[8] = {0 , 0 , 1 , -1 , 1 , 1 , -1 , -1};
    const int dj[8] = {1 , -1 , 0 , 0 , 1 , -1 , 1 , -1};

    ll FirstTrue (ll l,ll r,function<bool(ll)>f){
        while(l<r){
        ll mid=l+(r-l)/2;
        f(mid)?r=mid:l=mid+1;} 
        return l;
    }
    ll LastTrue(ll l,ll r,function<bool(ll)>f){
        while(l<r){
        ll mid=l+(r-l+1)/2;
        f(mid)?l=mid:r=mid-1;} 
        return l;
    }
     
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
 
    template<class T> void re(complex<T>& x);
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);
 
    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    void re(ld& x) { string t; re(t); x = stold(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) { 
        re(first); re(rest...); 
    }
 
    template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { lp(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { lp(i,SZ) re(a[i]); }
 
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);
 
    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { 
        pr(first); pr(rest...); 
    }
 
    template<class T1, class T2> void pr(const pair<T1,T2>& x) { 
        pr(x.first,' ',x.second); 
    }
    template<class T> void prContain(const T& x) {
        each(a,x) pr(a,' '); 
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
    
    void ps() { cout << '\n'; } 
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { 
        pr(first," "); ps(rest...);
    }    
}
using namespace Suhaib_Templates;

const int N = 1e3;
int n , m , st1 , st2 , en1 , en2;
char c[N][N];
bool visited[N][N];
int dis[N][N];
int h , w;

struct yay {
    bool operator ()(const auto& a , const auto& b) const {
        return a[2] > b[2];
    };
};

int bfs (int x , int y){

    lp(h)
        lp(j,w)
            dis[i][j] = 1e9;

    priority_queue <array<int,3> , vector <array<int,3>> , yay> q;
    q.push({x,y,0});
    dis[x][y] = 0;
    visited[x][y] = true;
    int cnt = 0;
    while (!q.empty()){
        int u = q.top()[0] , v = q.top()[1] , cost = q.top()[2];
        q.pop();

        if (dis[u][v] != cost)
            continue;

        if (u == en1 && v == en2)
            return cost;

        lp(d,4){
            int nu = u + di[d] , nv = v + dj[d];
            if (nu >= h || nv >= w || nu < 0 || nv < 0 || c[nu][nv] == '#')
                continue;
            if (cost < dis[nu][nv]){
                q.push({nu,nv,cost});
                dis[nu][nv] = cost;
            }
        }

        lp(i,-2,3){
            lp(j,-2,3){
                int nu = u + i , nv = v + j;
                if (nu >= h || nv >= w || nu < 0 || nv < 0 || c[nu][nv] == '#')
                    continue;
                if (cost + 1 < dis[nu][nv]){
                    q.push({nu,nv,cost + 1});
                    dis[nu][nv] = cost + 1;
                }           
            }
        }
    }

    return -1;

}

void Suhaib_Sawalha (){

    re(h,w,st1,st2,en1,en2);
    --st1 , --st2 , --en1 , --en2;
    lp(h)
        lp(j,w)
            re(c[i][j]);

    pr(bfs(st1,st2));

}

int main(){
                                In_The_Name_Of_Allah_The_Merciful   /* بسم الله الرحمن الرحيم  */
#ifndef ONLINE_JUDGE
    freopen("SuhaibSawalha1.txt","r",stdin);
#endif
// int _;cin>>_;for(int i=1;i<=_;++i,cout<<'\n')
        // cout<<"Case #"<<i<<":\n", 
        Suhaib_Sawalha();
                                         Alhamdulillah                    /* الحمد لله */
}

Measured memory_bytes: 8640