#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::bitset;

#define rep(i,n)    for(int i=0; i<(int)(n); i++)
#define reps(i,s,e) for(int i=s; i<(int)(e); i++)
#define coutd(i) cout << std::fixed << std::setprecision(i)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template<class T>
using Set = std::set<T>;
template<class T, class U>
using Map = std::map<T, U>;

template<class T>
using Graph = vector<vector<T>>;

const int INT_BIG = 1<<28;

bool isIn(int h, int w, int H, int W) {
    if (0 <= h && h < H && 0 <= w && w < W) {
        return true;
    } else {
        return false;
    }
}

int main() {
    int H, W;
    cin >> H >> W;
    int startH, startW, goalH, goalW;
    cin >> startH >> startW >> goalH >> goalW;
    startH--;
    startW--;
    goalH--;
    goalW--;
    Graph<int> mapdata(H, vint(W, 0));
    rep(h, H) {
        string s;
        cin >> s;
        rep(w, W) {
            if (s[w] == '.') {
                mapdata[h][w] = 1;
            } else {
                mapdata[h][w] = 0;
            }
        }
    }
    vector<vector<std::pair<int, pii>>> dest(H*W);
    rep(h, H) {
        rep(w, W) {
            int destKey = h*W + w;
            if (mapdata[h][w] == 1) {
                if (isIn(h, w-1, H, W)) {
                    if (mapdata[h][w-1] == 1)
                        dest[destKey].push_back(std::make_pair(0, pii(h, w-1)));
                }
                if (isIn(h, w+1, H, W)) {
                    if (mapdata[h][w+1] == 1)
                        dest[destKey].push_back(std::make_pair(0, pii(h, w+1)));
                }
                if (isIn(h-1, w, H, W)) {
                    if (mapdata[h-1][w] == 1)
                        dest[destKey].push_back(std::make_pair(0, pii(h-1, w)));
                }
                if (isIn(h+1, w, H, W)) {
                    if (mapdata[h+1][w] == 1)
                        dest[destKey].push_back(std::make_pair(0, pii(h+1, w)));
                }
                reps(x, -2, 3) {
                    reps(y, -2, 3) {
                        if (isIn(h+x, w+y, H, W)) {
                            if (mapdata[h+x][w+y] == 1)
                                dest[destKey].push_back(std::make_pair(1, pii(h+x, w+y)));
                        }
                    }
                }
            }
            
            
        }
    }
    
    vint cost(H*W, -1);
    cost[startH*W + startW] = 0;
    vector<bool> visited(H*W, false);
    int curNode = startH*W + startW;

    std::priority_queue<pii, vector<pii>, std::greater<pii>> queue;

    queue.push(std::make_pair(0, curNode));

    while (queue.size() > 0) {
        pii nowParam = queue.top();
        queue.pop();
        if (visited[nowParam.second]) {
            continue;
        }
        // cout << nowParam.first << endl;
        // cout << nowParam.second / W << " " << nowParam.second % W<< endl;

        for (auto& next : dest[nowParam.second]) {
            int nextNode = next.second.first*W + next.second.second;
            if (cost[nextNode] == -1 || cost[nextNode] > cost[nowParam.second] + next.first) {
                cost[nextNode] = cost[nowParam.second] + next.first;
                queue.push(std::make_pair(cost[nextNode], nextNode));
            }
        }

        visited[nowParam.second] = true;
    }

    cout << cost[goalH*W + goalW] << endl;
    
}

Measured memory_bytes: 164356