#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vi vector<int>
#define vs vector<string>
#define vll vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pcc pair<char, char>
#define pdd pair<double, double>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define flsh fflush(stdout)
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1000000000007

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> o_tree;

const int N = 1005;
ll a[N][N];
ll ans[N][N];
ll vis[N][N];
ll n, m;

bool in(ll x, ll y)
{
    if (x < 0 || y < 0)
    {
        return false;
    }
    if (x > n || y > m)
    {
        return false;
    }
    return true;
}

struct comp
{
    bool operator()(const pair<ll, pll> &a, const pair<ll, pll> &b)
    {
        return a.first < b.first;
    }
};

void bfs(ll x, ll y, ll xx)
{

    vis[x][y] = 1;
    ans[x][y] = xx;
    ll i, j;
    ans[x][y] = 0;
    deque<pll> q;

    q.push_front(mp(x, y));
    vis[x][y] = 1;
    while (!q.empty())
    {

        pll tt = q.front();
        q.pop_front();
        ll posx = tt.first, posy = tt.second;

        ll lol = ans[posx][posy];
        ll dx[] = {1, -1, 0, 0};
        ll dy[] = {0, 0, 1, -1};
        for (i = 0; i < 4; i++)
        {
            ll posxx = posx + dx[i];
            ll posyy = posy + dy[i];
            if (in(posxx, posyy))
            {
                if (ans[posxx][posyy] <= lol)
                    continue;
                if (!a[posxx][posyy])
                    continue;
                q.push_front(mp(posxx, posyy));
                ans[posxx][posyy]=lol;
            }
        }
        for (i = -2; i <= 2; i++)
        {
            for (j = -2; j <= 2; j++)
            {
                ll posxx = posx + i;
                ll posyy = posy + j;
                if (in(posxx, posyy))
                {
                    if (!a[posxx][posyy])
                        continue;
                    if(ans[posxx][posyy]<=lol)
                    {
                        continue;
                    }
                    q.push_back(mp(posxx, posyy));
                    ans[posxx][posyy]=lol+1;
                }
            }
        }
    }
}

void solve()
{
    cin >> n >> m;
    ll i, j;
    ll x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1--, y1--, x2--, y2--;
    string s;
    for (i = 0; i < n; i++)
    {
        cin >> s;
        for (j = 0; j < m; j++)
        {
            ans[i][j] = 1e18;
            if (s[j] == '.')
            {
                a[i][j] = 1;
            }
            else
            {
                a[i][j] = 0;
            }
        }
    }
    bfs(x1, y1, 0);
    ll res = 1e18;
    res = min(res, ans[x2][y2]);
    // for (i = 0; i < n; i++)
    // {
    //     for (j = 0; j < m; j++)
    //     {
    //         // cout << ans[i][j] << " ";
    //     }
    //     // cout << "\n";
    // }
    if (res == 1e18)
    {
        cout << "-1\n";
        return;
    }
    cout << res << "\n";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    if (fopen("input.txt", "r"))
    {
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    }
    cout << setprecision(20);

    ll t = 1;

    // cin>>t;

    while (t--)
    {
        solve();
    }

    return 0;
}


Measured memory_bytes: 20632