//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
#include <x86intrin.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <assert.h>
#include <string>
#include <bitset> 
#include <vector>
#include <cmath>                                    
#include <deque>                                    
#include <queue>
#include <iomanip>
#include <ctime>
#include <random>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
//#include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
 
//using namespace __gnu_pbds;
using namespace std;

 
//template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
     
#define F first
#define S second           
#define lb lower_bound               
#define ub upper_bound
#define pb push_back
#define pf push_front    
#define ppb pop_back
#define mp make_pair                                     
#define bpp __builtin_popcountll                                                                                        
#define sqr(x) ((x) * (x)) 
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define in insert
#define ppf pop_front
#define endl '\n'
//#define int long long
 
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
 
const int mod = (int)1e9 + 7;
const int N = (int)1e6 + 123;
const ll inf = (ll)1e18 + 1;
 
const double pi = acos (-1.0);
const double eps = 1e-9;                  
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};

int n, m;
int sx, sy, fx, fy;
int d[N], cl[1005][1005], col;
char c[1005][1005];
queue <pii> q;
queue <int> s;
vector <int> g[N];
 
inline void boost () {
   ios_base :: sync_with_stdio(0);
   cin.tie(0);
   cout.tie(0);
}

inline void Solve () {
   scanf ("%d%d", &n, &m);
   scanf ("%d%d", &sx, &sy);
   scanf ("%d%d", &fx, &fy);
   for (int i = 1; i <= n; i ++) scanf ("%s", c[i] + 1);
   for (int i = 1; i <= n; i ++) {
      for (int j = 1; j <= m; j ++) {
         if (c[i][j] == '#') continue;
         if (!cl[i][j]) {
            cl[i][j] = ++ col;
            q.push ({i, j});
            while (sz (q)) {
               int x = q.front ().F;
               int y = q.front ().S;
               q.pop ();
               for (int z = 1; z <= 4; z ++) {
                  int X = x + dx[z], Y = y + dy[z];
                  if (X < 1 || Y < 1 || Y > m || X > n || c[X][Y] == '#' || cl[X][Y]) continue;
                  cl[X][Y] = cl[x][y];
                  q.push ({X, Y});
               }
            }
         }
      }
   }        
   for (int i = 1; i <= col; i ++) d[i] = mod;
   d[cl[sx][sy]] = 0;
   for (int i = 1; i <= n; i ++) {
      for (int j = 1; j <= m; j ++) {
         for (int d1 = -2; d1 <= 2; d1 ++) {
            int x = i + d1;
            if (x < 1 || x > n) continue;
            for (int d2 = -2; d2 <= 2; d2 ++) {
               int y = j + d2;
               if (y < 1 || y > m || c[x][y] == '#') continue;
               if (cl[i][j] != cl[x][y]) {
                  g[cl[i][j]].pb (cl[x][y]);
                  g[cl[x][y]].pb (cl[i][j]);
               }
            }
         }
      }
   }
   for (int i = 1; i <= col; i ++) sort (all (g[i])), g[i].resize (unique (all (g[i])) - g[i].begin ());
   s.push (cl[sx][sy]);
   while (sz (s)) {
      int v = s.front ();
      s.pop ();
      for (auto to : g[v]) {
         if (d[to] > d[v] + 1) {
            d[to] = d[v] + 1;
            s.push (to);  
         }
      }
   }
   if (d[cl[fx][fy]] == mod) printf ("-1");
   else printf ("%d", d[cl[fx][fy]]);
}               
 
signed main () {                                       
   #ifdef QWERTY
      freopen("input", "r", stdin);
   #endif
// freopen (".in", "r", stdin);    
// freopen (".out", "w", stdout);              
  // boost ();
   int tt = 1;
   
   //cin >> tt;   
   while (tt --) {
      Solve ();
   }                                               
   return 0;      
}                 


Measured memory_bytes: 111240