#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};
int main() {
  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);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  deque<P> q;
  vector<vector<int> > dist(h, vector<int>(w, 10000));
  q.emplace_back(ch, cw);
  dist[ch][cw] = 0;
  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    q.pop_front();
    int d = dist[i][j];
    for (int v = 0; v < 4; v++) {
      int ni = i + di[v];
      int nj = j + dj[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
      if (s[ni][nj] == '#') continue;
      if (dist[ni][nj] <= d) continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }
    for (int ei = -2; ei <= 2; ei++) {
      for (int ej = -2; ej <= 2; ej++) {
        int ni = i + ei;
        int nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
        if (s[ni][nj] == '#') continue;
        if (dist[ni][nj] <= d + 1) continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(ni, nj);
      }
    }
  }
  int ans = dist[dh][dw];
  if (ans == 10000) {
    ans = -1;
  }
  cout << ans;
  return 0;
}
