#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define UM unordered_map
#define MP map
#define ST set
#define US unordered_set
#define V vector
#define S string
#define D deque
#define L list
#define Q queue
#define PQ priority_queue
#define D deque
#define PII pair<int, int>
#define P pair
#define DB double
#define LDB long double
#define FOR(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define FFOR(i, n) for (int(i) = 0; (i) <= (n); ++(i))
#define RFOR(i, n) for (int(i) = (n)-1; (i) >= 0; --(i))
#define all(grid) (grid).begin(), (grid).end()
#define rall(grid) (grid).rbegin(), (grid).rend()

const int dr[] = {0, 1, 0, -1};
const int dc[] = {1, 0, -1, 0};

template <typename T>
void read(T& a) {
  a = 0;
  char c;
  while ((c = cin.peek()) < 48 or c > 57) cin.get();
  while ((c = cin.get()) > 47 and c < 58) a = (a << 3) + (a << 1) + c - 48;
}

template <typename T, typename... Args>
void read(T& a, Args&&... args) {
  read(a);
  read(forward<Args>(args...)...);
}

template <typename T>
void write(const S& sep, const S& end, T n) {
  char c[128] = {0};
  int i = 0;
  for (; n; ++i, n /= 10) c[i] = n % 10;

  for (--i; i >= 0; --i) cout.put(c[i] + 48);

  cout.write(end.c_str(), end.size() * sizeof(char));
}

template <typename T, typename... Args>
void write(const S& sep, const S& end, T n, Args&&... args) {
  char c[128] = {0};
  int i = 0;
  for (; n; ++i, n /= 10) c[i] = n % 10;

  for (--i; i >= 0; --i) cout.put(c[i] + 48);

  cout.write(sep.c_str(), sep.size() * sizeof(char));
  write(sep, end, forward<Args>(args...)...);
}
const int mxN = 1e3 + 5;
int res[mxN][mxN], grid[mxN][mxN];
void grid_dijkstra(int n, int m, int start_r, int start_c) {
  FOR(i, n) {
    FOR(j, m) { res[i][j] = int(1e9); }
  }
  res[start_r][start_c] = 0;
  D<P<int, PII>> q;
  q.push_back({0, {start_r, start_c}});
  while (q.size()) {
    auto [dist, node] = q.front();
    auto [row, col] = node;
    q.pop_front();
    for (int k = 0; k < 4; ++k) {
      int new_row = row + dr[k];
      int new_col = col + dc[k];
      if (new_row >= 0 and new_col >= 0 and new_row < n and new_col < m and
          grid[new_row][new_col] != 1) {
        if (dist < res[new_row][new_col]) {
          res[new_row][new_col] = dist;
          q.push_front({dist, {new_row, new_col}});
        }
      }
    }

    for (int new_row = row - 2; new_row <= row + 2; ++new_row) {
      for (int new_col = col - 2; new_col <= col + 2; ++new_col) {
        if (new_row >= 0 and new_col >= 0 and new_row < n and new_col < m and
            grid[new_row][new_col] != 1) {
          int new_dist = res[row][col] + 1;
          if (new_dist < res[new_row][new_col]) {
            res[new_row][new_col] = new_dist;
            q.push_back({new_dist, {new_row, new_col}});
          }
        }
      }
    }
  }

  for (auto& i : res)
    for (auto& j : i)
      if (j == int(1e9)) j = -1;
}

void solve() {
  int h, w;
  cin >> h >> w;
  int start_r, start_c, end_r, end_c;
  cin >> start_r >> start_c >> end_r >> end_c;
  --start_r, --start_c, --end_r, --end_c;
  FOR(i, h) {
    FOR(j, w) {
      char t;
      cin >> t;
      grid[i][j] = t == '#';
    }
  }
  grid_dijkstra(h, w, start_r, start_c);
  cout << res[end_r][end_c] << "\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}


Measured memory_bytes: 11552