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

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  for (string &i : s) cin >> i;
  vector<vector<int>> l(h, vector<int>(w, -1));
  deque<tuple<int,int,int>> q;
  q.push_back({ch, cw, 0});
  while (!q.empty()) {
    auto [gh, gw, gl] = q.front();
    if (gh == dh and gw == dw) {
      cout << gl << endl;
      return 0;
    }
    q.pop_front();
    if (l[gh][gw] != -1) continue;
    l[gh][gw] = gl;
    if (gh + 1 < h and s[gh + 1][gw] == '.') q.push_front({gh + 1, gw, gl});
    if (gh - 1 >= 0 and s[gh - 1][gw] == '.') q.push_front({gh - 1, gw, gl});
    if (gw + 1 < w and s[gh][gw + 1] == '.') q.push_front({gh, gw + 1, gl});
    if (gw - 1 >= 0 and s[gh][gw - 1] == '.') q.push_front({gh, gw - 1, gl});
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = gh + i, nw = gw + j;
        if (0 <= nh and nh < h and 0 <= nw and nw < w and s[nh][nw] == '.') q.push_back({nh, nw, gl + 1});
      }
    }
  }
  cout << -1 << endl;
  return 0;
}