#include <iostream>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <cctype>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <cmath>
#include <stdlib.h>
#include <tuple>
#include <iomanip>
#include <iterator>
#include <stdio.h>
#include <fstream>
#include <time.h>
#include <bit>
#include <bitset>
#include <cstdint>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;
using ll = long long;

struct UnionFind {
    vector<int> parent;

    UnionFind(int N) : parent(N) {
        for (int i = 0; i < N; i++) {
            parent[i] = -1;
        }
    }

    int root(int i) {
        if (parent[i] < 0) {
            return i;
        }
        return (parent[i] = root(parent[i]));
    }

    bool unite(int from, int to) {
        int rx = root(from);
        int ry = root(to);
        if (rx != ry) {
            parent[ry] += parent[rx];
            parent[rx] = ry;

            return true;
        }
        else {
            return false;
        }
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }

    int treeSize(int x) {


        return -parent[root(x)];
    }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
    if (pow == 1) {
        return base;
    }
    else if (pow == 0) {
        return 1;
    }

    if (pow % 2 == 0) {
        auto temp = modpow(base, pow / 2, mod);
        return (temp * temp) % mod;
    }
    else {
        return (base * modpow(base, pow - 1, mod)) % mod;
    }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
    auto fermatDiv = modpow(Y, mod - 2, mod);

    return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
    long long answer = 1;
    if (left > right) {
        for (long long i = 0; i < right; i++) {
            answer = (answer * (left - i)) % mod;
            answer = moddiv(answer, (i + 1), mod);
        }
    }
    return answer;
}

bool IsPrime(long long N) {
    if (N == 1) {
        return false;
    }
    for (long long i = 2; i * i <= N; i++) {
        if (N % i == 0) {
            return false;
        }
    }


    return true;
}


vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0) {
            ++ex;
            N /= a;
            
        }

        // その結果を push
        res.push_back({ a, ex });
    }

    // 最後に残った数について
    if (N != 1) res.push_back({ N, 1 });
    return res;
}

vector<long long> enum_divisors(long long N) {
    vector<long long> res;
    for (long long i = 1; i * i <= N; ++i) {
        if (N % i == 0) {
            res.push_back(i);
            // 重複しないならば i の相方である N/i も push
            if (N / i != i) res.push_back(N / i);
        }
    }
    // 小さい順に並び替える
    sort(res.begin(), res.end());
    return res;
}

long long gcd(long long a, long long b) {
    if (b > a) {
        long long temp = b;
        b = a;
        a = temp;
    }
    //cout << "a:" << a << "b:" << b << endl;
    long long c = a % b;
    if (c == 0) {
        return b;
    }
    else {
        return gcd(b, c);
    }
}



int main() {
    ll H, W;
    cin >> H >> W;

    ll Ch, Cw, Dh, Dw;
    cin >> Ch >> Cw >> Dh >> Dw;
    Ch--;
    Cw--;
    Dh--;
    Dw--;

    vector<char> s(H * W);

    for (ll i = 0; i < H; i++) {
        for (ll j = 0; j < W; j++) {
            char c;
            cin >> c;
            s[i * W + j] = c;
        }
    }

    vector<ll> dist(H * W, -1);

    deque<pair<ll, ll>> dq;

    dq.push_front(make_pair(Ch * W + Cw, 0));
    dist[Ch * W + Cw] = 0;

    while (!dq.empty()) {
        auto pair = dq.front();
        dq.pop_front();
        ll first = pair.first;
        ll second = pair.second;

        if (dist[first] < pair.second) {
            continue;
        }


        for (ll i = -2; i <= 2; i++) {
            for (ll j = -2; j <= 2; j++) {
                ll idx = first + (i * W) + j;
                if (idx < 0 || idx >= H * W || 
                    (first % W) + j < 0 || (first % W) + j >= W ||
                    first + i * W < 0 || first + i * W >= H*W ||
                    s[idx] == '#') {
                    continue;
                }

                ll d = 1;

                if ((i == -1 && j == 0) ||
                    (i == 1 && j == 0) ||
                    (i == 0 && j == -1) ||
                    (i == 0 && j == 1)) {
                    d = 0;
                }


                if (dist[idx] > second + d || dist[idx] == -1) {
                    dist[idx] = second + d;
                    if (d == 0) {
                        dq.push_front(make_pair(idx, dist[idx]));
                    }
                    else {
                        dq.push_back(make_pair(idx, dist[idx]));
                    }

                    /*for (int i = 0; i < H; i++) {
                        for (int j = 0; j < W; j++) {
                            if (dist[i * W + j] == -1) {
                                cout << 3;
                            }
                            else {
                                cout << dist[i * W + j];
                            }
                        }
                        cout << endl;
                    }

                    cout << i << ":" << j << endl;*/
                }
            }
        }
    }


    cout << dist[Dh * W + Dw] << endl;
    
    return 0;
}


Measured memory_bytes: 12240