#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#include <functional>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
    if (ok) {
        cout << yes << endl;
    } else {
        cout << no << endl;
    }
}

struct edge {
    ll to, cost;
};

int N;
VV<edge> G(1000000);
V<ll> d(1000000);

void addedge(VV<edge>& G, int st, int ed, ll co) {
    edge e;
    e.to = ed;
    e.cost = co;
    G[st].push_back(e);
}

void BFS01(int n) {
    fill(d.begin(), d.end(), inf);
    deque<int> que;
    d[n] = 0;
    que.push_front(n);
    while (que.size() > 0) {
        int base = que.front();
        que.pop_front();
        int s = G[base].size();
        for (int i = 0; i < s; i++) {
            edge e = G[base][i];
            if (e.cost == 1 && d[e.to] > d[base] + 1) {
                d[e.to] = d[base] + 1;
                que.push_back(e.to);
            } else if (e.cost == 0 && d[e.to] > d[base]) {
                d[e.to] = d[base];
                que.push_front(e.to);
            }
        }
    }
}

ll bo[1010][1010];
// set<ll> g[1000010];
// ll d[1000010];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll H, W;
    cin >> H >> W;
    ll ch, cw, dh, dw;
    cin >> ch >> cw >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    REP(i, 0, H) {
        string s;
        cin >> s;
        REP(j, 0, W) {
            if (s[j] == '#') {
                bo[i][j] = -2;
            } else {
                bo[i][j] = -1;
            }
        }
    }
    REP(i, 0, H) {
        REP(j, 0, W) {
            if (bo[i][j] == -2) {
                continue;
            }
            REP(k, 0, 4) {
                ll newx = i + dx[k];
                ll newy = j + dy[k];
                if (newx < 0 || newx >= H || newy < 0 || newy >= W) {
                    continue;
                }
                if (bo[newx][newy] == -1) {
                    addedge(G, i * H + j, newx * H + newy, 0);
                }
            }
            REP(k, -2, 3) {
                REP(l, -2, 3) {
                    ll newx = i + k;
                    ll newy = j + l;
                    if (newx < 0 || newx >= H || newy < 0 || newy >= W) {
                        continue;
                    }
                    if (bo[newx][newy] == -1) {
                        addedge(G, i * H + j, newx * H + newy, 1);
                    }
                }
            }
        }
    }
    BFS01(ch * H + cw);
    ll ans = d[dh * H + dw];
    if (ans == inf) {
        cout << -1 << endl;
    } else {
        cout << ans << endl;
    }
    // ll tmp = 0;
    // REP(i, 0, H) {
    //     REP(j, 0, W) {
    //         if (bo[i][j] == -1) {
    //             queue<plglg> que;
    //             que.push(plglg(i, j));
    //             while (!que.empty()) {
    //                 plglg p = que.front();
    //                 que.pop();
    //                 ll x = p.first;
    //                 ll y = p.second;
    //                 bo[x][y] = tmp;
    //                 REP(k, 0, 4) {
    //                     ll newx = x + dx[k];
    //                     ll newy = y + dy[k];
    //                     if (newx < 0 || newx >= H || newy < 0 || newy >= W) {
    //                         continue;
    //                     }
    //                     if (bo[newx][newy] == -1) {
    //                         que.push(plglg(newx, newy));
    //                     }
    //                 }
    //             }
    //             tmp++;
    //         }
    //     }
    // }
    // REP(i, 0, H) {
    //     REP(j, 0, W) {
    //         if (bo[i][j] == -2) {
    //             continue;
    //         }
    //         REP(k, -2, 3) {
    //             REP(l, -2, 3) {
    //                 if (i + k < 0 || i + k >= H || j + l < 0 || j + l >= W) {
    //                     continue;
    //                 }
    //                 if (bo[i + k][j + l] != -2) {
    //                     g[bo[i][j]].insert(bo[i + k][j + l]);
    //                 }
    //             }
    //         }
    //     }
    // }
    // fill(d, d + tmp + 1, -1);
    // ll kind = bo[ch][cw];
    // queue<ll> queb;
    // queb.push(kind);
    // d[kind] = 0;
    // while (!queb.empty()) {
    //     ll now = queb.front();
    //     queb.pop();
    //     for (auto it = g[now].begin(); it != g[now].end(); it++) {
    //         if (d[*it] == -1) {
    //             d[*it] = d[now] + 1;
    //             queb.push(*it);
    //         }
    //     }
    // }
    // cout << d[bo[dh][dw]] << endl;
}


Measured memory_bytes: 212460