#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long MOD = 1e9 + 7;
const int INF = 1 << 30;
bool seen[60][60];
int hosuu[60][60];
int main() {
  int H, W;
  cin >> H >> W;
  int sy, gy, sx, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  vector<string> G(H);
  for (int i = 0; i < (int)(H); i++) {
    cin >> G[i];
  }
  queue<pair<int, int> > que;
  que.push(make_pair(sy, sx));
  const int dy[4] = {-1, 1, 0, 0};
  const int dx[4] = {0, 0, 1, -1};
  while (!que.empty()) {
    int y, x;
    tie(y, x) = que.front();
    que.pop();
    for (int dir = 0; dir < (int)(4); dir++) {
      int ny = y + dy[dir];
      int nx = x + dx[dir];
      if (0 <= ny && ny < H && nx >= 0 && nx < W && G[ny][nx] == '.' &&
          seen[ny][nx] == 0) {
        hosuu[ny][nx] = hosuu[y][x] + 1;
        seen[ny][nx] = 1;
        que.push(make_pair(ny, nx));
      }
    }
  }
  cout << hosuu[gy][gx] << endl;
}
