#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)
template <class T>
inline void eraque(std::vector<T>& vec) {
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  return;
}

template <class T>
inline int lower_position(const std::vector<T>& vec, T val) {
  return static_cast<int>(std::distance(
      vec.begin(), std::lower_bound(vec.begin(), vec.end(), val)));
}

template <class T>
inline int upper_position(const std::vector<T>& vec, T val) {
  return static_cast<int>(std::distance(
      vec.begin(), std::upper_bound(vec.begin(), vec.end(), val)));
}

template <class T>
inline std::string to_binary(T n) {
  assert(n > 0);
  std::string ret = "";
  while (n) ret += (n & 1) ? '1' : '0', n >>= 1;
  std::reverse(ret.begin(), ret.end());
  return ret;
}

template <class T>
inline void println(T val) {
  std::cout << val << '\n';
}

template <class T>
inline void println(const std::vector<T>& vec) {
  int sz = vec.size();
  for (int i = 0; i < sz; ++i) std::cout << vec[i] << " \n"[i == sz - 1];
}

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
  for (T& e : vec) is >> e;
  return is;
}

template <class T, class U>
std::istream& operator>>(std::istream& is, std::pair<T, U>& p) {
  return is >> p.first >> p.second;
}

template <class Tuple, std::size_t... Is>
void tuple_in(std::istream& is, Tuple& tp, std::index_sequence<Is...>) {
  ((is >> std::get<Is>(tp)), ...);
}

template <class... Args>
std::istream& operator>>(std::istream& is, std::tuple<Args...>& tp) {
  tuple_in(is, tp, std::index_sequence_for<Args...>{});
  return is;
}


template <class T>
class graph {
 public:
  graph(int n) : n_(n) { data_.resize(n_); }
  struct edge {
    int from, to;
    T cost;

    edge(int from_, int to_, T cost_) : from(from_), to(to_), cost(cost_) {}
  };
  int n_;
  std::vector<std::vector<int>> data_;
  std::vector<edge> edges_;

  virtual void add(int from, int to, T cost) = 0;
};

template <class T>
class forest : public graph<T> {
 public:
  using graph<T>::n_;
  using graph<T>::data_;
  using graph<T>::edges_;

  forest(int n) : graph<T>(n) {}

  void add(int from, int to, T cost = 1) {
    assert(0 <= from && from < n_ && 0 <= to && to < n_);
    int id = edges_.size();
    assert(id < n_ - 1);
    data_[from].emplace_back(id);
    data_[to].emplace_back(id);
    edges_.emplace_back(from, to, cost);
  }
};

template <class T>
class digraph : public graph<T> {
 public:
  using graph<T>::n_;
  using graph<T>::data_;
  using graph<T>::edges_;

  digraph(int n) : graph<T>(n) {}

  void add(int from, int to, T cost = 1) {
    assert(0 <= from && from < n_ && 0 <= to && to < n_);
    int id = edges_.size();
    data_[from].emplace_back(id);
    edges_.emplace_back(from, to, cost);
  }

  digraph<T> reverse() const {
    digraph<T> rev(n_);
    for (const auto& [from, to, cost] : edges_) {
      rev.add(to, from, cost);
    }
    return rev;
  }
};

template <class T>
class undigraph : public graph<T> {
 public:
  using graph<T>::n_;
  using graph<T>::data_;
  using graph<T>::edges_;

  undigraph(int n) : graph<T>(n) {}

  void add(int from, int to, T cost = 1) {
    assert(0 <= from && from < n_ && 0 <= to && to < n_);
    int id = edges_.size();
    data_[from].emplace_back(id);
    data_[to].emplace_back(id);
    edges_.emplace_back(from, to, cost);
  }
};

digraph<int> grid_to_digraph(const std::vector<std::string>& sv) {
  int h = sv.size(), w = sv[0].size();
  digraph<int> g(h * w);
  constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  auto Out = [&](int x, int y) { return (x < 0 || x >= h || y < 0 || y >= w); };
  auto Compress = [&](int x, int y) { return x * w + y; };
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      for (int k = 0; k < 4; ++k) {
        int nx = i + dx[k], ny = j + dy[k];
        if (Out(nx, ny)) continue;
        if (sv[i][j] == '.' && sv[nx][ny] == '.') {
          g.add(Compress(i, j), Compress(nx, ny), 0);
        }
      }
      for (int k = -2; k <= 2; ++k) {
        for (int l = -2; l <= 2; ++l) {
          int nx = i + k, ny = j + l;
          if (Out(nx, ny)) continue;
          if (abs(k) + abs(l) == 1) continue;
          if (sv[i][j] == '.' && sv[nx][ny] == '.') {
            g.add(Compress(i, j), Compress(nx, ny), 1);
          }
        }
      }
    }
  }
  return g;
}

template <class T>
std::vector<T> dijkstra(const graph<T>& g, int s) {
  assert(0 <= s && s < g.n_);
  std::vector<T> dist(g.n_, std::numeric_limits<T>::max());
  using Pair = std::pair<T, int>;
  std::priority_queue<Pair, std::vector<Pair>, std::greater<Pair>> pq;
  dist[s] = 0;
  pq.emplace(0, s);
  while (!pq.empty()) {
    auto [d, v] = pq.top();
    pq.pop();
    if (dist[v] < d) continue;
    for (int id : g.data_[v]) {
      const auto& [from, to, cost] = g.edges_[id];
      int nxt = from ^ to ^ v;
      if (dist[nxt] > dist[v] + cost) {
        dist[nxt] = dist[v] + cost;
        pq.emplace(dist[nxt], nxt);
      }
    }
  }
  return dist;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w; cin >> h >> w;
  int ch, cw; cin >> ch >> cw; --ch; --cw;
  int dh, dw; cin >> dh >> dw;  --dh; --dw;
  vector<string> s(h); cin >> s;
  digraph<int> g = grid_to_digraph(s);

  vector dist = dijkstra(g, ch * w + cw);

  int ans = dist[dh * w + dw];
  if (ans == numeric_limits<int>::max()) ans = -1;
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 152496