#include <bits/stdc++.h>
 
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
 
using namespace std;
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int h,w,ch,cw,dh,dw;
    cin >> h >> w;
    cin >> ch >> cw;
    cin >> dh >> dw;
    ch--;cw--;dh--;dw--;

    char maze[h][w];
    int maze_class[h][w];
    map<int, vpi> class_maze;
    pii node;

    forn(i,h) {
        forn(j,w) {
            cin >> maze[i][j];
            maze_class[i][j] = -2;
        }
    }
    int class_cnt = 0;
    int dx,dy;
    forn(i,h) {
        forn(j,w) {
            if (maze_class[i][j] != -2) {
                continue;
            }
            if (maze[i][j] == '#') {
                maze_class[i][j] = -1;
                class_maze[-1].pb({i,j});
                continue;
            }

            deque<pii> deq;
            deq.pb({i,j});
            while (!deq.empty()) {
                node = deq.front();
                // cerr << "NODE (" << node.fi << ',' << node.se << ") with class " << maze_class[node.fi][node.se] << '\n';
                deq.pop_front();
                if (maze_class[node.fi][node.se] > -2) {
                    continue;
                }
                // cerr << "ASSIGN ---- NODE (" << node.fi << ',' << node.se << ") to " << class_cnt<< '\n';
                maze_class[node.fi][node.se] = class_cnt;
                class_maze[class_cnt].pb({node.fi, node.se});
                forn(move,4) {
                    if (move ==0) {
                        dx = 1;
                        dy = 0;
                    }
                    else if (move == 1) {
                        dx = -1;
                        dy = 0;
                    }
                    else if (move == 2) {
                        dx = 0;
                        dy = 1;
                    }
                    else {
                        dx = 0;
                        dy = -1;
                    }

                    if (node.fi + dx >= 0 && node.fi + dx < h && node.se + dy >= 0 && node.se + dy < w) {
                        if (maze[node.fi+dx][node.se+dy] == '.') {
                            deq.pb({node.fi + dx, node.se + dy});
                        }
                    }
                }
            }
            class_cnt++;
        }
    }

    int ans = -1;

    // forn(i,h) {
    //     forn(j,w) {
    //         cerr << maze_class[i][j] << ' ';
    //     }
    //     cerr << '\n';
    // }
    // cerr << '\n';
    
    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
    for (pii node : class_maze[maze_class[ch][cw]]) {
        pq.push({0, {node.fi, node.se}});
    }
    
    pair<int, pair<int, int>> state;
    bool visited[class_cnt];
    bool subvisited[h][w];
    forn(i,class_cnt) {
        visited[i] = false;
    }
    forn(i,h) {
        forn(j,w) {
            subvisited[i][j] = false;
        }
    }
    while (!pq.empty()) {
        state = pq.top();
        pq.pop();
        // cerr << "NODE (" << state.se.fi << ", " << state.se.se << ")\n";
        if (maze_class[state.se.fi][state.se.se] == maze_class[dh][dw]) {
            ans = state.fi;
            break;
        }

        if (subvisited[state.se.fi][state.se.se]) {
            continue;
        }
        subvisited[state.se.fi][state.se.se] = true;


        // bool subvisited[class_cnt];
        // forn(i,class_cnt) {
            // subvisited[i] = false;
        // }

        // subvisited[maze_class[state.se.fi][state.se.se]]=true;

        for (int dx = -2; dx <= 2; ++dx) {
            for (int dy = -2; dy <= 2; ++dy) {
                // cerr << dy << dx << '\n';
                if (state.se.fi + dx >= 0 && state.se.fi + dx < h && state.se.se + dy >= 0 && state.se.se + dy < w) {
                        // cerr << "checkig node " << state.se.fi + dx << ", " << state.se.se + dy << " which is class " << maze_class[state.se.fi+dx][state.se.se+dy] << " and subvis " << subvisited[maze_class[state.se.fi+dx][state.se.se+dy]] << '\n';
                         if (maze_class[state.se.fi+dx][state.se.se+dy] != -1 && visited[maze_class[state.se.fi+dx][state.se.se+dy]] == false) {
                            visited[maze_class[state.se.fi+dx][state.se.se+dy]] = true;
                            for (pii node : class_maze[maze_class[state.se.fi+dx][state.se.se+dy]]) {
                                pq.push({state.fi +1, {node.fi, node.se}});
                            }
                        }
                }
            }
        }
    }


    cout << ans;




    return 0;
} 

Measured memory_bytes: 33900