#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef vector<pll> vll;
typedef vector<vl> vvl;

#define fori(i, n) for (int i = 0; i < n; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define trav(x, a) for (auto &x : a)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define endl '\n'
#define sz(a) (int)(a).size()
#define fi first
#define se second

clock_t time_p = clock();
void time_taken()
{
    time_p = clock() - time_p;
    cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

const ll mod = 1e9 + 7;
const ll INF = 1e18;

#define TRACE
#ifdef TRACE
#define trace(...) { cerr<<"[ "; __trace__(#__VA_ARGS__, __VA_ARGS__);}
#undef endl
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const pair<Arg1,Arg2> &x) {
    return out<<"("<<x.first<<","<<x.second<<")";
}
 
template <typename Arg1>
ostream& operator << (ostream& out, const vector<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1>
ostream& operator << (ostream& out, const set<Arg1> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1,typename Arg2>
ostream& operator << (ostream& out, const map<Arg1,Arg2> &a) {
    out<<"[";for(const auto &x:a)out<<x<<",";return out<<"]";
}
 
template <typename Arg1>
void __trace__(const string name, Arg1&& arg1){
    cerr << name << " : " << arg1 << " ] " << endl;
}
 
template <typename Arg1, typename... Args>
void __trace__(const string names, Arg1&& arg1, Args&&... args){
    const string name = names.substr(0,names.find(','));
    cerr<<name<<" : "<<arg1<<" | ";
    __trace__(names.substr(1+(int)name.size()), args...);
}
#else
    #define trace(args...)
#endif
 

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr);

    int n,m;
    cin >> n >> m;
    pii src,des;
    cin >> src.fi >> src.se >> des.fi >> des.se;
    src.fi--,des.fi--,src.se--,des.se--;
    vector<string> a(n);
    fori(i,n) cin >> a[i];
    deque<pii> q;
    q.push_back(src);
    auto ok = [&] (int x,int y) -> bool{
        return (x >= 0 and x < n and y >= 0 and y < m and a[x][y] == '.');
    };
    vector<vector<int>> d(n,vector<int>(m,mod));
    d[src.fi][src.se]=0;
    int dx[4] = {-1,+1,0,0};
    int dy[4] = {0,0,+1,-1};
    while(!q.empty()){
        pii p = q.front();
        q.pop_front();
        if(p == des){
            cout << d[des.fi][des.se] << endl;
            return 0;
        }
        int x = p.fi,y=p.se;
        fori(i,4){
            int tox = x+dx[i],toy = y+dy[i];
            if(ok(tox,toy) and d[tox][toy]>d[x][y]){
                d[tox][toy]=d[x][y];
                q.push_front({tox,toy});
            }
        }
        rep(dx,-2,+2){
            rep(dy,-2,+2){
                int tox = x+dx,toy =y+dy;
                if(ok(tox,toy) and d[tox][toy] > d[x][y]+1){
                    d[tox][toy]=d[x][y]+1;
                    q.push_back({tox,toy});
                }
            }
        }
    }
    cout << -1 << endl;

    time_taken();
    return 0;
}

Measured memory_bytes: 8700