#include<bits/stdc++.h>
using namespace std;
#define ar array<ll, 3>
#define endl '\n'
#define ll long long
#define in insert
#define pb push_back
#define vt vector
#define P_Q(x) priority_queue<x>
#define p_q(x) priority_queue<x, vector<x>, greater<x>>
#define Rep(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(ll i=a;i>=b;i--)
#define FOR(m) Rep(i,1,m)
#define For(m) Rep(i,0,m-1)
#define Rbl(x,a) for(auto &x : a)
#define FIO ios::sync_with_stdio(0);   cin.tie(0);
#define F first
#define S second
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define vpii vector<pii>
#define vpll vector<pll>
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define mod 1000000007
 
const int mxN = 1e3 + 1;
string s[mxN];
ll n, m, A, B, C, D;
ll d[mxN][mxN];
p_q(ar) pq;
ll xdir[] = {1, 0, -1, 0}, ydir[] = {0, 1, 0, -1};

void bfs(int x, int y)
{
    // cout << x << " " << y << endl;
    queue<array<ll, 2>> q;
    vt<array<ll, 2>> vec;
    q.push({x, y});
    while(!q.empty())
    {
        auto p = q.front(); q.pop();
        x = p[0], y = p[1];
        // cout << x << " " << y << endl;
        vec.pb({x, y});
        For(4)
        {
            int x_ = x + xdir[i], y_ = y + ydir[i];
            if(x_ < 0 || x_ >= n || y_ < 0 || y_ >= m)
                continue;
            if(s[x_][y_] == '#')
                continue;
            if(d[x_][y_] > d[x][y])
            {
                d[x_][y_] = d[x][y];
                q.push({x_, y_});
            }
        }
    }
    // cout << endl;
    for(auto p: vec)
    {
        int x = p[0], y = p[1];
        for(int up = 0; up <= 2; up++)
        {
            for(int left = 0; left <= 2; left++)
            {
                if(up == 0 && left == 0)
                    continue;
                for(int plus1 : {-1, 1})
                {
                    for(int plus2 : {-1, 1})
                    {
                        ll x_ = x + plus1 * up;
                        ll y_ = y + plus2 * left;
                        if(x_ < 0 || x_ >= n || y_ < 0 || y_ >= m)
                            continue;
                        if(s[x_][y_] == '#')
                            continue;
                        if(d[x_][y_] > d[x][y] + 1)
                        {
                            // cout << x_ << " " << y_ << endl;
                            d[x_][y_] = d[x][y] + 1;
                            pq.push({d[x_][y_], x_, y_});
                        }
                    }
                }
            }
        }
    }
}
 
 
int main()
{
FIO
 
    cin >> n >> m;
    cin >> A >> B >> C >> D;
    A--, B--, C--, D--;
    Rep(i,0,n-1) Rep(j,0,m-1) d[i][j] = INF;
    For(n) cin >> s[i];
    d[A][B] = 0;
    pq.push({0, A, B});
    while(!pq.empty())
    {
        auto p = pq.top(); pq.pop();
        ll x = p[1], y = p[2], dis = p[0];
        // cout << x << " " << y << endl;
        if(dis > d[x][y])
            continue;
        bfs(x, y);
    }
    // Rep(i,0,n-1)
    // {
    //     Rep(j,0,m-1) cout << d[i][j] << " ";
    //     cout << endl;
    // }
    if(d[C][D] >= INF)
        d[C][D] = -1;
    cout << d[C][D] << endl;
}

Measured memory_bytes: 12604