#include <bits/stdc++.h>
using namespace std;
constexpr int INF = 1e9;

int main() {
  int h, w, y1, x1, y2, x2;
  cin >> h >> w >> y1 >> x1 >> y2 >> x2;
  y1--, x1--, y2--, x2--;
  vector<string> s(h);
  for (auto &e : s) cin >> e;

  vector<vector<int>> d(h, vector<int>(w, INF));
  d[y1][x1] = 0;

  priority_queue<tuple<int, int, int>> q;
  q.push(make_tuple(0, y1, x1));
  while (!q.empty()) {
    tuple<int, int, int> t = q.top();
    q.pop();
	int dyx = get<0>(t);
	int y = get<1>(t);
	int x = get<2>(t);
    if (dyx > d[y][x]) continue;

    for (int dy = -2; dy <= 2; dy++) {
      int yt = y + dy;
      if (yt < 0|| yt >= h) continue;
      for (int dx = -2; dx <= 2; dx++) {
        int xt = x + dx;
        if (xt < 0 || xt >= w) continue;
        if (s[yt][xt] == '#') continue;

        int dt = d[y][x] + (abs(dy) + abs(dx) > 1 ? 1 : 0);
        if (dt < d[yt][xt]) {
          d[yt][xt] = dt;
          q.push(make_tuple(-dt, yt, xt));
        }
      }
    }
  }

  cout << (d[y2][x2] != INF ? d[y2][x2] : -1) << endl;
}