//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast,unroll-loops")
 
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i, beg, end) for (int i = (beg); i < (end); i++)
#define rrep(i, beg, end) for (int i = (beg); i >= (end); i--)
#define sp(i) setprecision(i) << fixed
typedef long long ll;
typedef pair<ll, ll> P;
typedef unsigned long long ull;
const int INF = 2e9;
const int mod = 1e9 + 7;
const int N = 1010;
ull p_hash(ull x, ull y) { return (x + y) * (x + y + 1) / 2 + y; } //NxN->N
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
 
int h, w, ch, cw, dh, dw;
char s[N][N];
bool mark[N][N];
int link[N][N];
set<int> g[N * N];
int cnt = 1;
void dfs1(int x, int y)
{
    int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
    link[x][y] = cnt;
    rep(i, 0, 4)
    {
        int xx = x + dx[i], yy = y + dy[i];
 
        if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && s[xx][yy] == '.' && !mark[xx][yy])
        {
            mark[xx][yy] = true;
            dfs1(xx, yy);
        }
    }
}
 
void dfs2(int x, int y)
{
    int dx[] = {1, -1, 1, -1}, dy[] = {1, -1, -1, 1};
    rep(i, 0, 3)
    {
        rep(j, 0, 3)
        {
            rep(k, 0, 4)
            {
                int xx = x + dx[k] * i, yy = y + dy[k] * j;
                if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && s[xx][yy] == '.' && link[xx][yy] != link[x][y])
                {
                    g[link[xx][yy]].insert(link[x][y]);
                    g[link[x][y]].insert(link[xx][yy]);
                }
            }
        }
    }
    int dxx[] = {0, 1, 0, -1}, dyy[] = {1, 0, -1, 0};
 
    rep(i, 0, 4)
    {
        int xx = x + dxx[i], yy = y + dyy[i];
 
        if (xx >= 1 && xx <= h && yy >= 1 && yy <= w && s[xx][yy] == '.' && !mark[xx][yy])
        {
            mark[xx][yy] = true;
            dfs2(xx, yy);
        }
    }
}
 
int dis[N*N];
int bfs(int s)
{
    memset(dis, -1, sizeof dis);
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        for (int v : g[now])
        {
            if (dis[v] == -1)
            {
                q.push(v);
                dis[v] = dis[now] + 1;
                if (v == link[dh][dw])
                    return dis[v];
            }
        }
    }
    return dis[link[dh][dw]];
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> h >> w;
    cin >> ch >> cw;
    cin >> dh >> dw;
    rep(i, 1, h + 1)
    {
        string in;
        cin >> in;
        rep(j, 1, w + 1)
        {
            s[i][j] = in[j - 1];
        }
    }
 
    rep(i, 1, h + 1)
    {
        rep(j, 1, w + 1)
        {
            if (s[i][j] == '.' && !mark[i][j])
            {
                mark[i][j] = true;
                dfs1(i, j);
                cnt++;
            }
        }
    }
 
    memset(mark, 0, sizeof mark);
    rep(i, 1, h + 1)
    {
        rep(j, 1, w + 1)
        {
            if (s[i][j] == '.' && !mark[i][j])
            {
                mark[i][j] = true;
                dfs2(i, j);
            }
        }
    }
 
    cout << bfs(link[ch][cw]) << endl;
 
    return 0;
}

Measured memory_bytes: 79844