#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <iterator>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <fstream>
#include <numeric>
#include <tuple>

using namespace std;

#define fori(t) for(int i = 0; i < t; ++i)
#define forj(n) for(int j = 0; j < n; ++j)
#define fork(n) for(int k = 0; k < n; ++k)
#define forai(a) for(auto& i : a)
#define foraj(a) for(auto& j : a)
#define forak(a) for(auto& k : a)
#define forp(s, n, d) for(int p = s; p < n; p += d)
#define fast cin.tie(0); cout.tie(0); std::ios_base::sync_with_stdio(0);
#define pb(a) push_back(a);
#define pob(a) pop_back(a);
#define sorti(a) sort(a.begin(), a.end());
#define sortm(a, n) sort(a, a + n);
#define reversi(a) reverse(a.begin(), a.end());
#define fi first
#define se second
#define yes cout << "Yes\n";
#define no cout << "No\n";
#define ans(a) cout << a << '\n';
#define res(a, n) a.resize(n);
#define read(a, n) a.resize(n); fori(n) cin >> a[i];
//#define int long long
//#define double long double

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef pair<long long, long long> llll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<pair<int, int>> vii;
typedef vector<pair<int, pair<int, int>>> viii;
typedef pair<int, pair<int, int>> iii;
typedef vector<pair<long long, long long>> vllll;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvll;
typedef vector<bool> vb;
typedef pair<char, char> cc;
typedef pair<pair<char, char>, char> ccc;
typedef vector<char> vc;
typedef pair<int, char> ic;
typedef pair<ii, char> iic;
typedef vector<bool> vb;
typedef set<int> si;
typedef set<ii> sii;
typedef unsigned long long ull;
typedef pair<ull, ull> ullll;
typedef pair<double, double> dd;
typedef vector<dd> vdd;
typedef vector<pair<ull, ull>> vullll;
typedef pair<bool, bool> bb;
typedef vector<bb> vbb;
typedef vector<vector<ii>> vvii;
typedef vector<vector<iii>> vviii;

const int INF = 2147483647;
const ll LONGINF = 9223372036854775807;
const ll MOD = 1e9 + 7;

vector<string> s;
vector<set<int>> adj;
vvi usn;
queue<int> q;
vi d;

void dfs(int i, int j, int h, int w, int counter)
{
    s[i][j] = ':';
    usn[i][j] = counter;
    if (i > 0 && s[i - 1][j] == '.') dfs(i - 1, j, h, w, counter);
    if (j > 0 && s[i][j - 1] == '.') dfs(i, j - 1, h, w, counter);
    if (i < h - 1 && s[i + 1][j] == '.') dfs(i + 1, j, h, w, counter);
    if (j < w - 1 && s[i][j + 1] == '.') dfs(i, j + 1, h, w, counter);
}

void solve()
{
    int h, w, ch, cw, dh, dw;
    cin >> h >> w >> ch >> cw >> dh >> dw;
    --ch;
    --cw;
    --dh;
    --dw;
    res(s, h)
    res(usn, h)
    fori(h)
    {
        cin >> s[i];
        res(usn[i], w)
    }
    dfs(ch, cw, h, w, 1);
    if (usn[ch][cw] == usn[dh][dw])
    {
        ans(0)
        return;
    }
    dfs(dh, dw, h, w, 2);
    int counter = 3;
    fori(h)
    {
        forj(w)
        {
            if (s[i][j] == '.')
            {
                dfs(i, j, h, w, counter);
                ++counter;
            }
        }
    }
    res(adj, counter)
    fori(h)
    {
        forj(w)
        {
            if (s[i][j] == ':')
            {
                for (int iv = max(i - 2, 0); iv <= min(i + 2, h - 1); ++iv)
                {
                    for (int jv = max(j - 2, 0); jv <= min(j + 2, w - 1); ++jv)
                    {
                        if (usn[iv][jv] != usn[i][j] && s[iv][jv] == ':')
                        {
                            adj[usn[iv][jv]].insert(usn[i][j]);
                            adj[usn[i][j]].insert(usn[iv][jv]);
                        }
                    }
                }
            }
        }
    }
    d = vi(counter, -1);
    d[1] = 0;
    q.push(1);
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        forai(adj[v])
        {
            if (d[i] == -1)
            {
                d[i] = d[v] + 1;
                q.push(i);
            }
        }
    }
    ans(d[2])
}

signed main()
{
    fast
    bool mult = false;
    if (mult)
    {
        int t;
        cin >> t;
        while (t--) solve();
    }
    else solve();
    return 0;
}


Measured memory_bytes: 33032