#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define endl "\n";
#define deb1(x) cout << #x << ": " << x << endl
#define deb2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define printclock cerr << "Time : " << 1000 * (ld)clock() / (ld)CLOCKS_PER_SEC << "ms\n";
#define vii vector<int>
#define pii pair<int, int>
#define prec(n) fixed << setprecision(n)
#define mp make_pair
#define all(a) a.begin(), a.end()
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define MOD LLONG_MAX
inline void get(vector<int> &arr, int n)
{
    int a;
    for (int i = 0; i < n; i++)
    {
        cin >> a;
        arr.push_back(a);
    }
}
inline void print(vector<int> &arr, int n)
{
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << endl;
}
/*----------------------------------TEMPLATE------------------------------------*/
const int mx = 1005;
const int inf = 1000000007;
vector<pii> dir = {{0ll, 1ll}, {1ll, 0ll}, {0ll, -1}, {-1, 0ll}};
vector<vector<char>> grid(mx, vector<char>(mx));
vector<vii> dist(mx, vii(mx, inf));
vector<vii> vis(mx, vii(mx, false));
int32_t main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    fast;
    /*---------------------------------------------------------------------------*/
    int t;
    t = 1;
    while (t--)
    {
        int h, w;
        cin >> h >> w;
        int sx, sy, ex, ey;
        cin >> sx >> sy >> ex >> ey;
        rep(i, 0, h)
        {
            rep(j, 0, w)
            {
                cin >> grid[i][j];
            }
        }
        dist[sx-1][sy-1] = 0;
        deque<pair<int,int>> q;
        q.push_front({sx-1,sy-1});
        // vis[sx][sy] = true;
        while(!q.empty())
        {
            auto curr = q.front();
            q.pop_front();
            int x = curr.first; int y = curr.second;
            vis[x][y] = true;
            rep(i, 0, 4)
            {
                int nx = x + dir[i].first;
                int ny = y + dir[i].second;
                if (nx >= 0 && ny >= 0 && nx < h && ny < w && !vis[nx][ny] && (grid[nx][ny] == '.'))
                {
                    dist[nx][ny] = min(dist[nx][ny], dist[x][y]);
                        q.push_front({nx,ny});
                }
            }
            for (int i = -2; i <= 2; i++)
            {
                for (int j = -2; j <= 2; j++)
                {
                    if (i == 0 && j == 0)
                        continue;
                    int nx = x + i;
                    int ny = y + j;
                    if (nx >= 0 && ny >= 0 && nx < h && ny < w && !vis[nx][ny] && (grid[nx][ny] == '.'))
                    {
                        dist[nx][ny] = min(dist[nx][ny], dist[x][y] + 1);
                            q.push_back({nx,ny});
                            // vis[nx][ny] = true;
                    }
                }
            }
        }
        if (dist[ex-1][ey-1] != inf)
            cout << dist[ex-1][ey-1] << endl else cout << -1 << endl;
    }
    return 0;
}

Measured memory_bytes: 30652