#include<bits/stdc++.h>
//#pragma GCC optimize ("-O3")

using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//const long double PI = 3.14159265358979323846;

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int h, w;
    cin >> h >> w;
    int sx, sy;
    cin >> sx >> sy;
    int fx, fy;
    cin >> fx >> fy;
    vector<vector<int>> maze(h, vector<int> (w));
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j ++)
        {
            char c;
            cin >> c;
            maze[i][j] = c == '.';
        }
    }
    // fill witch
    vector<vector<int>> which(h, vector<int> (w, -1));
    int comp = 0;
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            if (which[i][j] != -1 or ! maze[i][j])
                continue;
            // bfs
            which[i][j] = comp ++;
            queue<pair<int, int>> q;
            q.push({i, j});
            while (!q.empty())
            {
                auto cur = q.front();
                q.pop();
                int cx = cur.first, cy = cur.second;
                int delx[] = {1, -1, 0, 0};
                int dely[] = {0, 0, -1, 1};
                for (int a = 0; a < 4; a++)
                {
                    int nx = cx + delx[a];
                    int ny = cy +dely[a];

                    if (nx >= 0 and nx < h and ny >= 0 and ny < w and maze[nx][ny] and which[nx][ny] == -1)
                    {
                        which[nx][ny] = which[cx][cy];
                        q.push({nx, ny});
                    }
                }
            }

        }
    }
//    for (int i = 0; i < h; i++)
//    {
//        for (int j = 0; j < w; j++)
//            cout << which[i][j] << " ";
//        cout << "\n";
//    }

    sx --;
    sy --;
    fx --;
    fy --;
    int start = which[sx][sy];
    int enc = which[fx][fy];
    // make graph
    vector<set<int>> adj(comp + 1);
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            if (maze[i][j])
            {
                for (int x = -2; x < 3; x++)
                {
                    for (int y = -2; y < 3; y++)
                    {
                        int nx = i + x, ny = j + y;
                        if (nx >= 0 and nx < h and ny >= 0 and ny < w)
                        {
                            if (which[nx][ny] != -1 and which[nx][ny] != which[i][j])
                            {
                                adj[which[nx][ny]].insert(which[i][j]);
                                adj[which[i][j]].insert(which[nx][ny]);
                            }
                        }
                    }
                }
            }
        }
    }
    // bfs
    vector<int> dist (comp + 1, -1);
    queue<int> q;
    q.push(start);
    dist[start] = 0;
    while (!q.empty())
    {
        int f = q.front();
        q.pop();
        for (int x: adj[f])
        {
            if (dist[x] == -1)
            {
                dist[x] = dist[f] + 1;
                q.push(x);
            }
        }
    }
    cout << dist[enc] << "\n";
    return 0;
}

Measured memory_bytes: 35792