#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
struct node
{
    int to;
    int val;
    node() {}
    node(int tt, int vv) {
        to = tt;
        val = vv;
    }
    bool operator < (const node & b) const {
        return val > b.val;
    }
};
std::vector<node> e[maxn];
int dis[maxn];
void addedge(int a, int b, int v)
{
    e[a].push_back(node(b, v));
    // e[b].push_back(node(a,v));
}
bool vis[maxn];
void init(int n)
{
    for (int i = 1; i <= n; ++i)
    {
        dis[i] = inf;
        // dis[i/] = 1e18;
        vis[i] = 0;
    }
}
priority_queue<node> heap;
void dijkstra(int strat)
{
    init(maxn - 1);
    dis[strat] = 0;
    heap.push(node(strat, 0));
    node temp;
    while (!heap.empty())
    {
        temp = heap.top();
        heap.pop();
        if (vis[temp.to])
        {
            continue;
        } else
        {
            vis[temp.to] = 1;
        }
        for (auto & x : e[temp.to])
        {
            if (dis[temp.to] + x.val < dis[x.to])
            {
                dis[x.to] = dis[temp.to] + x.val;
                heap.push(node(x.to, dis[x.to]));
            }
        }
    }
}
/*

addedge(u, v, c);

dijkstra(s);

repd(i, 1, n)
{
    printf("%lld ", dis[i] );
}

 */


int n, m;
char s[1004][1004];
inline int getid(int x, int y)
{
    return m * (x - 1) + y;
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
bool in(int x, int y)
{
    return x >= 1 && x <= n && y >= 1 && y <= m;
}
int main()
{
#if DEBUG_Switch
    freopen("C:\\code\\input.txt", "r", stdin);
#endif
    //freopen("C:\\code\\output.txt","w",stdout);
    scanf("%d %d", &n, &m);
    int sx, sy, ex, ey;
    scanf("%d %d", &sx, &sy);
    scanf("%d %d", &ex, &ey);
    repd(i, 1, n)
    {
        scanf("%s", s[i] + 1);
    }
    repd(i, 1, n)
    {
        repd(j, 1, m)
        {
            if (s[i][j] == '#')
                continue;
            repd(k, 0, 3)
            {
                int tx = i + dx[k];
                int ty = j + dy[k];
                if (in(tx, ty) && s[tx][ty] == '.')
                {
                    addedge(getid(i, j), getid(tx, ty), 0);
                }
            }
            repd(ddx, -2, 2)
            {
                repd(ddy, -2, 2)
                {
                    int tx = i + ddx;
                    int ty = j + ddy;
                    if (in(tx, ty) && s[tx][ty] == '.')
                    {
                        addedge(getid(i, j), getid(tx, ty), 1);
                    }
                }
            }
        }
    }
    dijkstra(getid(sx, sy));
    int ans = dis[getid(ex, ey)];

    if (ans != inf)
        printf("%d\n", ans);
    else
        printf("-1\n");
    return 0;
}




Measured memory_bytes: 122108