#include <bits/stdc++.h>
using namespace std;

// boost
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

//#define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T>
inline void chmax(T& a, T b) {
  a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
  a = std::min(a, b);
}

#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)

const long long INF = 1LL << 60;

// const long long INF = 1LL << 60;

template <typename T>
struct Edge {
  int to;
  T cost;
};

struct UnionFind {
  vector<int> parent;
  vector<int> num_node;

  UnionFind(int N) : parent(N), num_node(N, 1) {
    for (int i = 0; i < N; i++) {
      parent[i] = i;
    }
  }

  // 根を求める
  int root(int x) {
    if (parent[x] == x) {
      return x;
    }

    // 経路圧縮
    parent[x] = root(parent[x]);
    return parent[x];
  }

  // 同一判定
  bool is_same(int x, int y) {
    return this->root(x) == this->root(y);
  }

  // 併合
  void unite(int x, int y) {
    x = this->root(x);
    y = this->root(y);

    // すでに一緒ならなにもしない
    if (x == y) {
      return;
    }
    parent[x] = y;
    num_node[y] += num_node[x];
  }

  // 同一グループのノード数
  int size(int x) {
    return num_node[root(x)];
  }
};

// Dijkstra
template <typename T>
class Dijkstra {
 private:
  vector<vector<Edge<T>>> G;
  int num_v;

 public:
  Dijkstra(int num_v) : G(num_v), num_v(num_v) {
  }

  void add_edge(int from, int to, T cost) {
    G[from].push_back((Edge<T>){to, cost});
  }

  // start: Start point
  // result: result
  void solve(int start, vector<T>& result) {
    vector<int> tmp;
    solve(start, result, tmp);
  }

  void solve(int start, vector<T>& result, vector<int>& prev) {
    result.resize(num_v);
    std::fill(result.begin(), result.end(), INF);

    prev.resize(num_v);
    std::fill(prev.begin(), prev.end(), -1);

    // get<0>(T): cost
    // get<1>(T): index
    // get<2>(T): ... ( for extent)
    using Tup = tuple<T, int>;
    priority_queue<Tup, vector<Tup>, greater<Tup>> que;

    que.emplace(0, start);
    result[start] = 0;

    while (!que.empty()) {
      Tup p = que.top();
      que.pop();

      T t = get<0>(p);
      int v = get<1>(p);

      // queueに入れてから別の最短経路が見つかった場合、探索処理は不要
      if (result[v] != t) {
        continue;
      }

      // vの各辺に対しよりコストの低い経路があるかを確認
      for (auto& e : G[v]) {
        if (result[e.to] > result[v] + e.cost) {
          prev[e.to] = v;
          result[e.to] = result[v] + e.cost;
          que.emplace(result[v] + e.cost, e.to);
        }
      }
    }
  }

  // t: 目的地
  vector<int> get_path(const vector<int>& prev, int t) {
    vector<int> path;

    for (int v = t; v != -1; v = prev[v]) {
      path.push_back(v);
    }

    reverse(path.begin(), path.end());
    return path;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  ll h, w;
  cin >> h >> w;
  ll ch, cw;
  cin >> ch >> cw;
  ll dh, dw;
  cin >> dh >> dw;

  ch--;
  cw--;
  dh--;
  dw--;

  vs s(h);
  rep(i, h) cin >> s[i];

  using tup = tuple<ll, ll, ll>;
  priority_queue<tup, vector<tup>, greater<tup>> q;
  q.emplace(0, ch, cw);

  vvll cost(h, vll(w, INF));
  cost[ch][cw] = 0;

  while (!q.empty()) {
    auto t = q.top();
    q.pop();
    ll c = get<0>(t);
    ll ny = get<1>(t);
    ll nx = get<2>(t);

    for (int y = ny - 1; y <= ny + 1; y++) {
      if (y < 0 || y >= h)
        continue;
      for (int x = nx - 1; x <= nx + 1; x++) {
        if (x < 0 || x >= w)
          continue;
        if (abs(y - ny) + abs(x - nx) > 1)
          continue;
        if (s[y][x] == '.') {
          if (cost[y][x] > c) {
            cost[y][x] = c;
            q.emplace(c, y, x);
          }
        }
      }
    }

    for (int y = ny - 2; y <= ny + 2; y++) {
      if (y < 0 || y >= h)
        continue;
      for (int x = nx - 2; x <= nx + 2; x++) {
        if (x < 0 || x >= w)
          continue;
        if (s[y][x] == '.') {
          if (cost[y][x] > c + 1) {
            cost[y][x] = c + 1;
            q.emplace(c + 1, y, x);
          }
        }
      }
    }
  }

  if (cost[dh][dw] == INF)
    cost[dh][dw] = -1;
  cout << cost[dh][dw] << endl;

  return 0;
}


Measured memory_bytes: 12732