#include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;

const int MOD = 1e9 + 7;
const LL MX = 1e3 + 100;
const LL INF = 1e9;

int n,m;
VPII p1, p2;
string g[MX];
int comp[MX][MX];
VI dis;
queue<PII> ord;

bool chk(int x, int y)
{
    if(x >= 0 && x < n && y >= 0 && y < m && g[x][y] == '.') return true;
    else return false;
}

void bfs(int sx, int sy, int c)
{
    dis.PB(-1);
    queue<PII> q;
    comp[sx][sy] = c;
    q.push({sx,sy});
    int ux, uy, vx, vy;

    while(!q.empty())
    {
        ux = q.front().FF; uy = q.front().SS;
        q.pop();
        loop(i,0,p1.size())
        {
            vx = ux + p1[i].FF, vy = uy + p1[i].SS;
            if(chk(vx,vy) && comp[vx][vy] == -1)
            {
                comp[vx][vy] = c; q.push({vx,vy});
            }
        }
    }
}

int vis[MX][MX];

void bfs1(int sx, int sy, int c)
{
    queue<PII> q;
    vis[sx][sy] = 1;
    q.push({sx,sy});
    int ux, uy, vx, vy, cmp_val;

    while(!q.empty())
    {
        ux = q.front().FF; uy = q.front().SS;
        q.pop();

        loop(i,0,p1.size())
        {
            vx = ux + p1[i].FF, vy = uy + p1[i].SS;
            if(chk(vx,vy) && vis[vx][vy] == 0)
            {
                vis[vx][vy] = 1; q.push({vx,vy});
            }
        }

        loop(i,0,p2.size())
        {
            vx = ux + p2[i].FF, vy = uy + p2[i].SS;
            if(chk(vx,vy))
            {
                cmp_val = comp[vx][vy];
                if(dis[cmp_val] == -1)
                {
                    dis[cmp_val] = c; ord.push({vx,vy});
                }
            }
        }
    }
}

int main()
{
    //ofstream out("output.txt");
    //ifstream in("input.txt");
    //ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    int x[5] = {0,1,2,-1,-2}, y[5] = {0,1,2,-1,-2};
    loop(i,0,5)
    {
        loop(j,i,5)
        {
            if(i == 0 && j == 0) continue;
            if((abs(x[i]) + abs(y[j])) == 1)
            {
                p1.PB({x[i],y[j]});
                p1.PB({y[j],x[i]});
            }
            else
            {
                p2.PB({x[i],y[j]});
                if(x[i] != y[j]) p2.PB({y[j],x[i]});
            }
        }
    }

    int sx, sy, dx, dy;
    cin>>n>>m;
    cin>>sx>>sy>>dx>>dy;
    loop(i,0,n) cin>>g[i];

    int nc = 0;
    mset(comp,-1);
    loop(i,0,n)
    {
        loop(j,0,m) if(g[i][j] == '.' && comp[i][j] == -1) {++nc; bfs(i,j,nc-1);}
    }

    int cmp_val = comp[sx-1][sy-1], ux, uy;
    dis[cmp_val] = 0;
    ord.push({sx-1,sy-1});

    while(!ord.empty())
    {
        ux = ord.front().FF; uy = ord.front().SS;
        cmp_val = comp[ux][uy];
        ord.pop();
        bfs1(ux,uy,dis[cmp_val] + 1);
    }

    cmp_val = comp[dx-1][dy-1];
    cout<<dis[cmp_val]<<'\n';

    /*
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        int a[n];
        loop(i,0,n)
        {
            cin>>a[i];
        }
    }
    */

    /*
    int n;
    cin>>n;
    int a[n];
    loop(i,0,n)
    {
        cin>>a[i];
    }
    */

    /*
    cout<<ans<<'\n';

    if(f) cout<<"YES\n";
    else cout<<"NO\n";

    if(f) cout<<"Yes\n";
    else cout<<"No\n";
    */

    return 0;
}


Measured memory_bytes: 14648