// *********************************************************************************
// *                        MURTAZA MUSTAFA KHUMUSI                                *
// *                        NIT-DGP,CSE - 2019-2023                                *
// *********************************************************************************

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

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define MOD 1000000007;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loop1(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloop1(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io                        \
    ios_base::sync_with_stdio(0); \
    cin.tie(NULL);
#define se second
#define fi first

const int inf = 0x3f3f3f3f;
const int maxm = 1001;
int h, w;
int mat[maxm][maxm];
int vis[maxm][maxm];

class Dis
{
public:
    int r, c, d;

    Dis(int a, int b, int q) : r(a), c(b), d(q) {}
};

bool con(int r, int c)
{
    return (0 < r && r <= h && 0 < c && c <= w);
}

void dd(deque<Dis> &de)
{
    cout << "\n\n....DEQUE DISPLAY.....\n";
    for (const auto &v : de)
    {
        cout << v.r << " " << v.c << " " << v.d << "\n";
    }
    cout << "\n....DEQUE DISPLAY.....\n\n";
}
int solve()
{

    cin >> h >> w;

    int sr, sc, dr, dc;
    cin >> sr >> sc >> dr >> dc;

    loop1(i, 1, h)
    {
        loop1(j, 1, w)
        {
            char ch;
            cin >> ch;
            if (ch == '.')
                mat[i][j] = 1;
            else
                mat[i][j] = 0;
            vis[i][j] = 0;
        }
    }

    deque<Dis> val;
    val.push_back(Dis(sr, sc, 0));

    int ans = -1;
    int cv[4] = {-1, 0, 1, 0};
    int rv[4] = {0, 1, 0, -1};

    vector<int> wcv, wrv;

    loop1(i, -2, 2)
    {
        loop1(j, -2, 2)
        {
            if (i == 0 && j == 0)
                continue;

            wcv.pb(i);
            wrv.pb(j);
        }
    }

    while (val.size())
    {

        int cr = val.front().r;
        int cc = val.front().c;
        int dis = val.front().d;
        // dd(val);
        val.pop_front();

        if (vis[cr][cc])
            continue;

        vis[cr][cc] = 1;

        if (cr == dr && cc == dc)
        {
            ans = dis;
            break;
        }
        //move in all four directions
        for (int i = 0; i < 4; i++)
        {
            int nr = cr + rv[i], nc = cc + cv[i];

            if (con(nr, nc) && !vis[nr][nc] && mat[nr][nc])
            {
                val.push_front(Dis(nr, nc, dis));
            }
        }

        //move in all
        for (int i = 0; i < 24; i++)
        {
            int nr = cr + wcv[i], nc = cc + wrv[i];

            if (con(nr, nc) && !vis[nr][nc] && mat[nr][nc])
            {
                val.push_back(Dis(nr, nc, dis + 1));
            }
        }
    }

    cout << ans << "\n";
    return 0;
}
int main()
{

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    io;
    solve();

    return 0;
}


Measured memory_bytes: 12280