#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <utility>
using namespace std;

typedef unsigned long long int u64;
typedef long long int i64;
typedef pair<u64, u64> point;
typedef pair<u64, pair<u64, u64>> que_elem;

int main() {
    u64 h, w, ch, cw, dh, dw;
    vector<string> s;
    cin >> h >> w;
    cin >> ch >> cw;
    cin >> dh >> dw;

    for(u64 i = 0; i < h; i++) {
        string x;
        cin >> x;
        s.push_back(x);
    }

    vector< vector<u64> > added;
    for(u64 i = 0; i < h; i++) {
        vector<u64> x(w, 0);
        added.push_back(x);
    }

    map<u64,vector<point>> groups;
    u64 g_num = 1;

    for(u64 i2 = 0; i2 < h; i2++) {
        for(u64 j2 = 0; j2 < w; j2++) {
            if(s[i2][j2] != '.')
                continue;

            // g_num, i, j
            queue<que_elem> que;
            pair<u64, point> y = make_pair(g_num, make_pair(i2, j2));
            que.push(y);
            
            bool found = false;
            while (!que.empty()) {
                pair<u64, pair<u64, u64>> x = que.front();
                u64 num = x.first;
                u64 i = x.second.first;
                u64 j = x.second.second;
                que.pop();

                if(added[i][j] == 0) {
                    added[i][j] = num;
                    vector<point> empty;
                    groups[num] = empty;
                    found = true;
                }

                if(j > 0 && s[i][j-1] && s[i][j-1] == '.' && added[i][j-1] == 0){
                    added[i][j-1] = num;
                    groups[num].push_back(make_pair(i, j-1));
                    found = true;
                    que.push(make_pair(num, make_pair(i, j-1)));
                }
                if(j < w - 1 && s[i][j+1] && s[i][j+1] == '.' && added[i][j+1] == 0){
                    added[i][j+1] = num;
                    groups[num].push_back(make_pair(i, j+1));
                    found = true;
                    que.push(make_pair(num, make_pair(i, j+1)));
                }
                if(i > 0 && s[i-1][j] && s[i-1][j] == '.' && added[i-1][j] == 0){
                    added[i-1][j] = num;
                    groups[num].push_back(make_pair(i-1, j));
                    found = true;
                    que.push(make_pair(num, make_pair(i-1, j)));
                }
                if(i < h - 1 && s[i+1][j] && s[i+1][j] == '.' && added[i+1][j] == 0){
                    added[i+1][j] = num;
                    groups[num].push_back(make_pair(i+1, j));
                    found = true;
                    que.push(make_pair(num, make_pair(i+1, j)));
                }
            }

            if(found) {
                g_num++;
            }
        }
    }

    vector<vector<u64>> route(g_num);
    for(u64 i = 0; i < g_num; i++) {
        vector<u64> empty;
        route[i] = empty;
    }

    for(i64 i2 = 0; i2 < (i64)h; i2++) {
        for(i64 j2 = 0; j2 < (i64)w; j2++) {
            if(added[i2][j2] == 0)
                continue;

            u64 g = added[i2][j2];

            for(int i = -2; i <= 2; i++) {
                for(int j = -2; j <= 2; j++) {
                    if(i2 + i < 0 || i2 + i >= (i64)h || j2 + j < 0 || j2 + j >= (i64)w) {
                        continue;
                    } 
                    if(added[i2+i][j2+j] == 0) {
                        continue;
                    }

                    if(g != added[i2+i][j2+j]) {
                        route[g].push_back(added[i2+i][j2+j]);
                    }
                }
            }
        }
    }

    u64 st = added[ch-1][cw-1];
    u64 ed = added[dh-1][dw-1];

    vector<u64> visited(g_num, UINT64_MAX);
    queue<pair<u64, u64>> que2;
    que2.push(make_pair(st, 0));

    while (!que2.empty()) {
        pair<u64, u64> x = que2.front();
        que2.pop();

        u64 t = x.first;
        u64 d = x.second;

        if(d >= visited[t]){
            continue;
        }

        visited[t] = d;

        vector<u64> r = route[t];
        for(u64 x: r)  {
            que2.push(make_pair(x, d+1));
        }
    }

    if(visited[ed] == UINT64_MAX) {
        cout << "-1" << endl;
    } else {
        cout << visited[ed];
    }
}


Measured memory_bytes: 52080