#include <bits/stdc++.h>
using namespace std;
using lint = long long;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int N, M;
  cin >> N >> M;
  int sx, sy, ex, ey;
  cin >> sx >> sy >> ex >> ey;
  sx--, sy--, ex--, ey--;
  vector<string> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  deque<pair<pair<int, int>, int>> dq;
  dq.push_back({{sx, sy}, 0});
  vector<vector<int>> dist(N, vector<int>(M, -1));
  dist[sx][sy] = 0;
  while (!dq.empty()) {
    int x = dq.front().first.first;
    int y = dq.front().first.second;
    int d = dq.front().second;
    dq.pop_front();
    if (dist[x][y] != d) {
      continue;
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = x + i, ny = y + j;
        if (abs(i) + abs(j) == 1 && 0 <= nx && nx < N && 0 <= ny && ny < M &&
            A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y]) {
            dist[nx][ny] = dist[x][y];
            dq.push_front({{nx, ny}, dist[nx][ny]});
          }
        }
        if (0 <= nx && nx < N && 0 <= ny && ny < M && A[nx][ny] != '#') {
          if (dist[nx][ny] == -1 || dist[nx][ny] > dist[x][y] + 1) {
            dist[nx][ny] = dist[x][y] + 1;
            dq.push_back({{nx, ny}, dist[nx][ny]});
          }
        }
      }
    }
  }
  cout << dist[ex][ey] << "\n";
  return 0;
}
