#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <climits>

constexpr int SINT_MAX = std::numeric_limits<int>::max();
constexpr int SINT_MIN = std::numeric_limits<int>::min();

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;

template<class T>
T choose(bool b, T t, T f)
{
    if (b) return t;
    else return f;    
}

const char *YesNo(bool b)
{
    return choose(b, "Yes", "No");
}

const char *YESNO(bool b)
{
    return choose(b, "YES", "NO");
}

template<class NumT>
NumT diffabs(NumT l, NumT r)
{
    if (l < r) return r-l;
    else return l-r;    
}

struct myinout_t {} io;

template<class T>
myinout_t &operator >>(myinout_t &my, T &i)
{
    cin >> i;
    return my;
}

myinout_t &operator >>(myinout_t &my, int &i)
{
    int r = scanf("%d", &i);
    if (r != 1) exit(EXIT_FAILURE);
    return my;
}

myinout_t &operator >>(myinout_t &my, ll &i)
{
    int r = scanf("%lld", &i);
    if (r != 1) exit(EXIT_FAILURE);
    return my;
}

template<class T>
myinout_t &operator <<(myinout_t &my, const T &i)
{
    cout << i;
    return my;
}

myinout_t &operator <<(myinout_t &my, int i)
{
    printf("%d", i);
    return my;
}

myinout_t &operator <<(myinout_t &my, ll i)
{
    printf("%lld", i);
    return my;
}

myinout_t &operator <<(myinout_t &my, double i)
{
    printf("%.20f", i);
    return my;
}

constexpr char BR = '\n';


///////////////////////////////////////////////////
#include <queue>

class Problem
{
    int H, W, Ch, Cw, Dh, Dw;
    vvi S;

public:
    void input()
    {
        io >> H >> W >> Ch >> Cw >> Dh >> Dw;
        --Ch; --Cw; --Dh; --Dw;

        S = vvi(H, vi(W));
        for (int i = 0; i < H; ++i)
        {
            for (int j = 0; j < W; ++j)
            {
                char c;
                io >> c;
                int ss = (c == '.') ? -1 : -100;
                S[i][j] = ss;
            }
        }
    }

    void input1000()
    {
        H = 1000;
        W = 1000;
        Ch = 0;
        Cw = 0;
        Dh = 999;
        Dw = 999;
        S = vvi(H, vi(W));
        for (int i = 0; i < H; ++i)
        {
            for (int j = 0; j < W; ++j)
            {
                S[i][j] = ((i+j)%2) ? -100 :-1;
            }
        }
    }

    using pii = pair<int,int>;

    void nuri (vector<pii> &out, int startH, int startW, int cWarp)
    {
        queue<pii> q;
        q.push({startH, startW});

        while (q.size())
        {
            auto [i, j] = q.front(); q.pop();
            if (S[i][j] == -1)
            {
                S[i][j] = cWarp;
                out.push_back({i,j});
                if (i > 0)   q.push({i-1, j});
                if (i < H-1) q.push({i+1, j});
                if (j > 0)   q.push({i, j-1});
                if (j < W-1) q.push({i, j+1});
            }
        }
    }

    bool isin(int h, int w)
    {
        return (h >= 0 && h <= H-1 && w >= 0 && w <= W-1);
    }

    int next(int cWarp, const vector<pii> &po)
    {
        bool found = false;
        vector<pii> o;

        for (auto [i, j] : po)
        {
            for (int di = -2; di <= 2; di++)
            {
                for (int dj = -2; dj <= 2; dj++)
                {
                    int ii = i + di;
                    int jj = j + dj;
                    
                    if (isin(ii, jj) && S[ii][jj] == -1)
                    {
                        found = true;
                        nuri(o, ii, jj, cWarp + 1);
                    }
                }
            }
        }

        if (S[Dh][Dw] >= 0)
            return S[Dh][Dw];
        else if (found)
            return next(cWarp + 1, o);
        else
            return -1;
    }

    int solve()
    {
        input();
        vector<pii> o;
        nuri(o, Ch, Cw, 0);
        return next(0, o);
    }
};



int main()
{
    Problem d;
    int ans = d.solve();
    io << ans<< BR;
}

Measured memory_bytes: 59748