#include<bits/stdc++.h>

using namespace std;

#define fRead(x)        freopen(x,"r",stdin)
#define fWrite(x)       freopen (x,"w",stdout)

#define LL              long long
#define ULL             unsigned long long
#define ff              first
#define ss              second
#define pb              push_back
#define PI              acos(-1.0)
#define mk              make_pair
#define pii             pair<int,int>
#define pll             pair<LL,LL>


#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define SQR(a)          ((a)*(a))
#define FOR(i,a,b)      for(int i=a;i<=b;i++)
#define ROF(i,a,b)      for(int i=a;i>=b;i--)
#define REP(i,b)        for(int i=0;i<b;i++)
#define MEM(a,x)        memset(a,x,sizeof(a))
#define ABS(x)          ((x)<0?-(x):(x))

#define SORT(v)         sort(v.begin(),v.end())
#define REV(v)          reverse(v.begin(),v.end())
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O2")
//#pragma GCC optimization ("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FastRead        ios_base::sync_with_stdio(0);cin.tie(nullptr);
#ifdef VAMP
     #define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template < typename Arg1 >
    void __f(const char* name, Arg1&& arg1){
        cout << name << " = " << arg1 << std::endl;
    }
    template < typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names+1, ',');
        cout.write(names, comma - names) << " = " << arg1 <<" | ";
        __f(comma+1, args...);
    }
#else
    #define debug(...)
#endif

const int N = 1005;
char str[N][N];
int h , w;
int sx , sy;
int fx, fy;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
bool vis[N][N];
int dis[N][N];
queue<pii>Q[N * N];

int main()
{
    #ifdef VAMP
        clock_t tStart = clock();
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    cin >> h >> w;
    cin >> sx >> sy;
    cin >> fx >> fy;
    sx--;sy--;
    fx--;fy--;

    REP(i,h)cin >> str[i];
    REP(i,h)REP(j,w)dis[i][j] = 1e9;
    Q[0].push(mk(sx,sy));
    dis[sx][sy] = 0;
    int ptr = 0;

    while(1){
        if(Q[ptr].empty() and Q[ptr + 1].empty()){
            break;
        }
        if(Q[ptr].empty()){
            ptr++;
            continue;
        }

        pii p = Q[ptr].front();
        Q[ptr].pop();
        for(int i = 0;i < 4;i++){
            int x = p.ff + dx[i];
            int y = p.ss + dy[i];
            if(x >= 0 and y >= 0 and x < h and y < w and str[x][y] == '.' and dis[x][y] > dis[p.ff][p.ss]){
                vis[x][y] = 1;
                dis[x][y] = dis[p.ff][p.ss];
                Q[ptr].push(mk(x,y));
            }
        }
        for(int i = -2;i <= 2;i++){
            for(int j = -2;j <= 2;j++){
                int x = p.ff + i;
                int y = p.ss + j;
                if(x >= 0 and y >= 0 and x < h and y < w and str[x][y] == '.' and dis[x][y] > 1 + dis[p.ff][p.ss]){
                    vis[x][y] = 1;
                    dis[x][y] = 1 + dis[p.ff][p.ss];
                    Q[ptr + 1].push(mk(x,y));
                }
            }
        }
//        for(int i = 0;i < h;i++){
//            for(int j = 0;j < w;j++){
//                cout << dis[i][j];
//            }cout << "\n";
//        }
//        cout << '\n';
    }
    if(dis[fx][fy] == 1e9)dis[fx][fy] = -1;

    cout << dis[fx][fy] << "\n";
    #ifdef VAMP
        fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
    #endif
}


Measured memory_bytes: 688016