#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <iomanip>
#include <set>
#include <ctime>
#include <tuple>
#include <bitset>
#include <assert.h>
#include <deque>
#include <functional>
using namespace std;
typedef long long ll;

#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define debugA cerr << "AAAAA" << endl
#define debug_ cerr << "-------------" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                   \
    cout << #v << endl;                \
    for (int i = 0; i < v.size(); i++) \
    {                                  \
        cout << v[i] << " ";           \
    }                                  \
    cout << endl;
#define debug_vec2(v)                         \
    cout << #v << endl;                       \
    for (int i = 0; i < v.size(); i++)        \
    {                                         \
        for (int j = 0; j < v[i].size(); j++) \
        {                                     \
            cout << v[i][j] << " ";           \
        }                                     \
        cout << endl;                         \
    }

template <typename T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

template <typename T>
bool chmin(T &a, const T &b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}

template <typename T>
void quit(T a)
{
    cout << a << endl;
    exit(0);
}

using Graph = vector<vector<int>>;
using P = pair<int, int>;
using P1 = pair<int, pair<int, int>>; // クラスカル法とかで、{cost, {from, to}}的に使う。
const ll LINF = 10010020030040056ll;
const int INF = 1001001001;
const double pi = acos(-1);

int main()
{
    int h, w;
    cin >> h >> w;
    int ch, cw, dh, dw;
    cin >> ch >> cw;
    cin >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    vector<vector<char>> s(h, vector<char>(w));
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            cin >> s[i][j];
        }
    }
    vector<vector<int>> ans(h, vector<int>(w, INF));
    queue<pair<int, int>> que;
    vector<int> dx = {1, 0, 0, -1};
    vector<int> dy = {0, 1, -1, 0};
    que.push({ch, cw});
    ans[ch][cw] = 0;
    int tmp = 0;
    while (true)
    {
        queue<pair<int, int>> que1;
        while (!que.empty())
        {
            int x = que.front().fi;
            int y = que.front().se;
            que.pop();
            que1.push({x, y});
            for (int i = 0; i < 4; i++)
            {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '.')
                {
                    if (chmin(ans[nx][ny], ans[x][y]))
                    {
                        que.push({nx, ny});
                        que1.push({nx, ny});
                    }
                }
            }
            if (ans[dh][dw] != INF)
            {
                cout << ans[dh][dw] << endl;
                return 0;
            }
        }
        while (!que1.empty())
        {
            int x = que1.front().fi;
            int y = que1.front().se;
            que1.pop();
            for (int i = -2; i <= 2; i++)
            {
                for (int j = -2; j <= 2; j++)
                {
                    int nx = x + i;
                    int ny = y + j;
                    if (nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '.')
                    {
                        if (chmin(ans[nx][ny], ans[x][y] + 1))
                        {
                            que.push({nx, ny});
                        }
                    }
                }
            }
        }
        if (que.empty())
        {
            break;
        }
    }
    if (ans[dh][dw] == INF)
    {
        cout << -1 << endl;
        return 0;
    }
    cout << ans[dh][dw] << endl;

    return 0;
}


Measured memory_bytes: 8584