//{{{
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
// clang-format off
void _print() { cerr << "]\033[0m\n"; }
template <typename T> void __print(T x) { cerr << x; }
template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifdef LOCAL
#define debug(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m["; _print(x)
#define debug_arr(x...) cerr << "\033[1;34m[" << #x << "] = \033[1;32m" << (x) << "\033[0m\n"
#else
#define debug(x...)
#define debug_arr(x...)
#endif
// clang-format on
//}}}

const int N = 1e3 + 10;
const int mod = 1e9 + 7;

const int inf = 0x3f3f3f3f;

char g[N][N];
int h, w;
int ch, cw;
int dh, dw;

int color[N][N];
int vis[N][N];
set<pii> edges;

void dfs(int i, int j, int c)
{
    if (i < 1 || i > h || j < 1 || j > w) return;

    if (vis[i][j]) return;
    if (g[i][j] == '#') return;

    vis[i][j] = true;
    color[i][j] = c;

    dfs(i + 1, j, c);
    dfs(i - 1, j, c);
    dfs(i, j - 1, c);
    dfs(i, j + 1, c);
}

int stc, edc;

const int NN = 4e6;
int head[NN], e;
struct E
{
    int v, next;
} edge[NN << 1];

void addedge(int u, int v)
{
    edge[e].v = v, edge[e].next = head[u];
    head[u] = e++;
}

void add(int x, int y)
{
    int c1 = color[x][y];
    for (int dx = -2; dx <= 2; dx++)
    {
        for (int dy = -2; dy <= 2; dy++)
        {
            if (dx == 0 && 0 == dy) continue;
            int xx = dx + x;
            int yy = dy + y;
            if (xx < 1 || x > h || yy < 1 || yy > w) continue;

            int c2 = color[xx][yy];
            if (c2 != 0 && c1 != c2)
            {
                int u = c1;
                int v = c2;
                if (u > v) swap(u, v);
                if (edges.find({u, v}) != edges.end()) continue;

                edges.insert({u, v});

                addedge(u, v);
                addedge(v, u);
            }
        }
    }
}

int d[NN];
int visd[NN];
void bfs(int st)
{
    clr(d, 0x3f);
    clr(visd, false);

    queue<int> q;
    q.push(stc);
    d[st] = 0;

    while (!q.empty())
    {
        int pos = q.front();
        q.pop();

        if (visd[pos]) continue;

        visd[pos] = 1;
        if (pos == edc)
        {
            break;
        }

        for (int i = head[pos]; ~i; i = edge[i].next)
        {
            int v = edge[i].v;
            if (visd[v] == false && d[v] == inf)
            {
                d[v] = d[pos] + 1;
                q.push(v);
            }
        }
    }
}

int main()
{
#ifdef LOCAL
    freopen("in", "r", stdin);
    // freopen("out", "w", stdout);
#endif

    while (~scanf("%d%d", &h, &w))
    {
        scanf("%d%d", &ch, &cw);
        scanf("%d%d", &dh, &dw);
        for (int i = 1; i <= h; ++i)
        {
            scanf("%s", g[i] + 1);
        }

        clr(vis, false);
        clr(color, 0);

        int c = 1;
        for (int i = 1; i <= h; i++)
        {
            for (int j = 1; j <= w; j++)
            {
                if (g[i][j] == '#' || vis[i][j])
                {
                    continue;
                }

                dfs(i, j, c);
                c++;
            }
        }

        stc = color[ch][cw];
        edc = color[dh][dw];

        if (stc == edc)
        {
            cout << 0 << endl;
            continue;
        }

        clr(head, -1), e = 0;
        edges.clear();
        for (int i = 1; i <= h; i++)
        {
            for (int j = 1; j <= w; j++)
            {
                if (color[i][j] < 1) continue;
                add(i, j);
            }
        }

        bfs(stc);
        int ans = d[edc];
        if (ans == inf) ans = -1;

        cout << ans << endl;
    }

    return 0;
}


Measured memory_bytes: 71868