#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;

using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) for(int sz=0;sz<int(vec.size());sz++) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');

const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;

using PI = P<int, P<int, int> >;

int h, w;

bool check(int x, int y) {
    return 0 <= x && x < h && 0 <= y && y < w;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> h >> w;
    int ch, cw, dh, dw; cin >> ch >> cw >> dh >> dw; ch--, cw--, dh--, dw--;
    V<string> s(h);
    for (int i = 0; i < h; i++) cin >> s[i];
    V< V<int> > dist(h, V<int>(w, INF));
    V< V<bool> > leaved(h, V<bool>(w, false));
    dist[ch][cw] = 0;
    priority_queue<PI> que; que.push(PI(0, make_pair(ch, cw)));

    V<int> dx = {0, 1, 0, -1}, dy = {1, 0, -1, 0};
    while (!que.empty()) {
        PI q = que.top(); que.pop();
        if (dist[q.second.first][q.second.second] < -q.first) continue;

        // cout << q.first << ' ' << q.second.first << ' ' << q.second.second << '\n';
        leaved[q.second.first][q.second.second] = true;
        for (int i = 0; i < 4; i++) {
            int nx = q.second.first + dx[i], ny = q.second.second + dy[i];
            if (!check(nx, ny) || s[nx][ny] == '#' || leaved[nx][ny]) continue;
            if (chmin(dist[nx][ny], dist[q.second.first][q.second.second])) {
                que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
            }
        }

        {
            int nx = q.second.first + 1, ny = q.second.second + 1;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first + 1, ny = q.second.second - 1;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first - 1, ny = q.second.second + 1;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first - 1, ny = q.second.second - 1;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        // 2 masu
        {
            int nx = q.second.first - 2, ny = q.second.second;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first - 2, ny = q.second.second + 1;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first - 2, ny = q.second.second - 1;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first - 2, ny = q.second.second + 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first - 2, ny = q.second.second - 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first - 1, ny = q.second.second - 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first - 1, ny = q.second.second + 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first, ny = q.second.second - 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first, ny = q.second.second + 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first + 1, ny = q.second.second - 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first + 1, ny = q.second.second + 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first + 2, ny = q.second.second;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first + 2, ny = q.second.second + 1;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first + 2, ny = q.second.second - 1;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first + 2, ny = q.second.second + 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
        {
            int nx = q.second.first + 2, ny = q.second.second - 2;
            if (check(nx, ny) && s[nx][ny] == '.' && !leaved[nx][ny]) {
                if (chmin(dist[nx][ny], dist[q.second.first][q.second.second] + 1)) {
                    que.push(PI(-dist[nx][ny], make_pair(nx, ny)));
                }
            }
        }
    }
    if (dist[dh][dw] == INF) cout << -1 << '\n';
    else cout << dist[dh][dw] << '\n';
    return 0;
}


Measured memory_bytes: 8736