#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()

/* do your best */

/* do your best */

//
//  |-----|
//  |7|0|4|
//  |-+-+-|     int ni = i + di[dir]
//  |3|*|1|     int nj = j + dj[dir]
//  |-+-+-|
//  |6|2|5|
//  |-----|
//
//
//  e.g.
//  int h = 2;
//  int w = 3;
//  int n = h * w;
//
//    |0|1|2|
//    |-+-+-|
//   0|0|1|2|
//  --|-+-+-|
//   1|3|4|5|
//    |-----|
//
//
//
// This is just code for competitive programming
// You can't blame me


long long int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
long long int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

// true だったら範囲外
bool out_of_grid(int i, int j, int h, int w) {
  return !(0 <= i and i < h and 0 <= j and j < w);
}

int coordinate_to_node(int i, int j, int h, int w) {
  return i * w + j;
}

pair<int, int> node_to_coordinate(int node, int h, int w) {
  return {node / h, node % w};
}


// O(E log V)
template<typename T>
vector<pair<T, int>> dijkstra(vector<vector<pair<int, T>>> &g, int s){
     
  int n = g.size();
  // numeric_limits がうまく使えない場合は自分で INF を定義する.
  const auto inf = numeric_limits<T>::max();
  vector<pair<T, int>> dist(n, {inf, -1});

  using P = pair<T, int>;
  // 小さい順に取り出せる
  priority_queue<P, vector<P>, greater<P>> que;
  dist[s] = {0, -1};
  que.emplace(dist[s].first, s);
  while(!que.empty()){
    T cost;
    int node;
    tie(cost, node) = que.top();
    que.pop();
    if(dist[node].first < cost) continue;
    for(auto &e : g[node]){
      auto next_cost = cost + e.second;
      int next_node = e.first;
      if(dist[next_node].first <= next_cost) continue;
      dist[next_node] = {next_cost, node};
      que.emplace(next_cost, next_node);
    }
  }

  return dist;
}


int main() {
  
  int h, w; cin >> h >> w;
  int ci, cj; cin >> ci >> cj;
  ci--;
  cj--;
  int ddi, ddj; cin >> ddi >> ddj;

  ddi--;
  ddj--;

  vector<vector<char>> s(h, vector<char>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> s[i][j];
    }
  }

  vector<vector<int>> toNode(h, vector<int>(w));
  int node = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      toNode[i][j] = node;
      node++;
    }
  }

  vector<vector<pair<int, int>>> g(node);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') continue;
      
      int cur = toNode[i][j];
      // 隣接4
      for (int dir = 0; dir < 4; dir++) {
        int ni = i + di[dir];
        int nj = j + dj[dir];
        if (out_of_grid(ni, nj, h, w)) continue;
        int nxt = toNode[ni][nj];
        int cost = 0;
        g[cur].push_back({nxt, cost});
      }

      // 5 5
      for (int delta_i = -2; delta_i <= 2; delta_i++) {
        for (int delta_j = -2; delta_j <= 2; delta_j++) {
          int ni = i + delta_i;
          int nj = j + delta_j;
          if (out_of_grid(ni, nj, h, w)) continue;
          int nxt = toNode[ni][nj];
          int cost = 1;
          g[cur].push_back({nxt, cost});
        }
      }
    }
  }

  int S = toNode[ci][cj];
  int G = toNode[ddi][ddj];
  lint ans = dijkstra(g, S)[G].first;
  if (ans == numeric_limits<int>::max()) {
    ans = -1;
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 172412