// header {{{
// header {{{
using namespace std;
#include <bits/stdc++.h>

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

#define ASSERT(expr...) assert((expr))

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;
// }}}

constexpr i64 INF = 1'010'000'000'000'000'017LL;

constexpr i64 MOD = 1'000'000'007LL;

constexpr f64 EPS = 1e-12;

constexpr f64 PI = 3.14159265358979323846;

#define M5 100007
#define M9 1000000000

// util {{{
#define FOR(i, start, end) for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end); i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)

#define ALL(f, c, ...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); })(c))
#define ll long long int
#define VI vector<int>
template <typename C>
i64 SIZE(const C &c)
{
    return static_cast<i64>(c.size());
}

template <typename T, size_t N>
i64 SIZE(const T (&)[N]) { return static_cast<i64>(N); }

template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {})
{
    if (comp(xmax, x))
    {
        xmax = x;
        return true;
    }
    return false;
}

template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {})
{
    if (comp(x, xmin))
    {
        xmin = x;
        return true;
    }
    return false;
}
// }}}

// init {{{
struct ProconInit
{
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit()
    {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if (AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;
// }}}

//--------------------------------------------------------------------

bool isOK(int mid, double key)
{
}

int binary_search(double key)
{
    int left = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
    int right = 2000000000;
    /* どんな二分探索でもここの書き方を変えずにできる！ */
    while (right - left > 1)
    {
        int mid = left + (right - left) / 2;

        if (isOK(mid, key))
            right = mid;
        else
            left = mid;
    }

    /* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている */
    return right;
}

bool isKaibun(string st)
{
    bool ans = true;
    int n = st.length();
    REP(i, n / 2)
    {
        if (st[i] != st[n - i - 1])
        {
            return false;
        }
    }
    return true;
}

int toInt(char a)
{
    return a - '0';
}

int gcd(int a, int b)
{
    if (a % b == 0)
    {
        return (b);
    }
    else
    {
        return (gcd(b, a % b));
    }
}

int lcm(int a, int b)
{
    return a * b / gcd(a, b);
}

vector<string> S;
vector<vector<int>> SS;
int dy[4] = {0, 1, 0, -1};
int dx[4] = {1, 0, -1, 0};
int H, W;

void rec(int hh, int ww)
{
    queue<pair<int, int>> Q;
    Q.push({hh, ww});
    while (!Q.empty())
    {
        pair<int, int> nowQ = Q.front();
        Q.pop();
        int h = nowQ.first;
        int w = nowQ.second;

        queue<pair<int, int>> q;
        queue<pair<int, int>> qq;
        q.push({h, w});
        qq.push({h, w});
        int init = SS[h][w];
        //    cout << "HW:" << h << " " << w << endl;
        bool first = false;
        while (!q.empty())
        {
            pair<int, int> now = q.front();
            q.pop();
            int y = now.first;
            int x = now.second;
            if (SS[y][x] == init)
            {
                if (!first)
                {
                    first = true;
                }
                else
                {
                    continue;
                }
            }

            SS[y][x] = init;
            //        cout << "xy:" << x << " " << y << endl;
            REP(i, 4)
            {
                int newx = x + dx[i];
                int newy = y + dy[i];
                //            cout << "newxy:" << newx << " " << newy << endl;
                if (0 <= newx && newx < W && 0 <= newy && newy < H)
                {

                    if (S[newy][newx] == '.')
                    {
                        q.push({newy, newx});
                        qq.push({newy, newx});
                    }
                }
            }
        }

        while (!qq.empty())
        {
            pair<int, int> now = qq.front();
            qq.pop();
            int y = now.first;
            int x = now.second;
            FOR(i, y - 2, y + 3)
            {
                FOR(j, x - 2, x + 3)
                {
                    if (0 <= j && j < W && 0 <= i && i < H)
                        if (SS[i][j] == -1 && S[i][j] == '.')
                        {
                            //                            cout << "ij" << i << " " << j << endl;
                            //                         cout << init << endl;
                            SS[i][j] = init + 1;
                            Q.push({i, j});
                        }
                }
            }
        }
    }
}

void solve()
{

    cin >> H >> W;
    int CH, CW;
    int DH, DW;
    cin >> CH >> CW >> DH >> DW;
    CH--;
    CW--;
    DH--;
    DW--;
    S.resize(H);
    SS.resize(H);
    //    cout << "HW:" << H << " " << W << endl;
    REP(i, H)
    {
        cin >> S[i];
        SS[i].resize(W);
        REP(j, W)
        {
            SS[i][j] = -1;
        }
    }
    SS[CH][CW] = 0;
    rec(CH, CW);
    /*    REP(i, H)
    {
        REP(j, W)
        {
            cout << SS[i][j];
        }
        cout << endl;
    }*/
    cout << SS[DH][DW];
}

signed main()
{

    solve();

    return 0;
}


Measured memory_bytes: 8700