#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, 1, 0, -1};

ll h, w;
ll sx, sy, gx, gy;
vector<string> maze;

int main() {
  cin >> h >> w >> sy >> sx >> gy >> gx;
  --sx, --sy, --gx, --gy;
  maze.resize(h);
  rep(i, h) cin >> maze[i];

  deque<pair<ll, ll>> q;
  q.push_back({sy, sx});
  vector<ll> d(h * w, -1);
  d[sy * w + sx] = 0;

  while (q.size()) {
    ll y = q.front().first;
    ll x = q.front().second;
    q.pop_front();

    rep(i, 4) {
      ll ny = y + dy[i], nx = x + dx[i];
      if (ny >= 0 && ny < h && nx >= 0 && nx < w && maze[ny][nx] == '.')
        if (d[ny * w + nx] == -1 || d[ny * w + nx] > d[y * w + x])
          d[ny * w + nx] = d[y * w + x], q.push_front({ny, nx});
    }

    for (ll i = y - 2; i <= y + 2; ++i)
      for (ll j = x - 2; j <= x + 2; ++j)
        if (i >= 0 && i < h && j >= 0 && j < w && maze[i][j] == '.')
          if (d[i * w + j] == -1 || d[i * w + j] > d[y * w + x] + 1)
            d[i * w + j] = d[y * w + x] + 1, q.push_back({i, j});
  }

  cout << d[gy * w + gx] << endl;
  return 0;
}