//競技プログラミング用のテンプレート
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <stack>
#include <limits>

#define rep(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define repr(i, n) for(int i = n - 1; i >= 0; i--)

using namespace std;
//エイリアス
using ll = long long int;
using p = pair<int, int>;
using pl = pair<ll, ll>;
using v = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vl = vector<ll>;

//定数
const int intmax = numeric_limits<int>::max();
const ll llmax = numeric_limits<ll>::max();
const ll mod = 1e9 + 7;
const double pi = M_PI;

int h, w;
int ch, cw, dh, dw;

int decomp(vs &grid, vector<v> &result) {
  int id = 0;
  vector<vector<bool>> reached(h, vector<bool>(w, false));
  for(int i = 0; i < h; i++) {
    for(int j = 0; j < w; j++) {
      if(reached[i][j]) continue;
      if(grid[i][j] == '#') continue;
      queue<p> q;
      q.push({i, j});
      reached[i][j] = true;
      result[i][j] = id;
      while(!q.empty()) {
        p cur = q.front(); q.pop();
        if (cur.first > 0 && !reached[cur.first - 1][cur.second] && grid[cur.first - 1][cur.second] == '.' ) {
          reached[cur.first - 1][cur.second] = true;
          q.push({cur.first - 1, cur.second});
          result[cur.first - 1][cur.second] = id;
        }
        if (cur.first < h - 1 && !reached[cur.first + 1][cur.second] && grid[cur.first + 1][cur.second] == '.' ) {
          reached[cur.first + 1][cur.second] = true;
          q.push({cur.first + 1, cur.second});
          result[cur.first + 1][cur.second] = id;
        }
        if (cur.second > 0 && !reached[cur.first][cur.second - 1] && grid[cur.first][cur.second - 1] == '.' ) {
          reached[cur.first][cur.second - 1] = true;
          q.push({cur.first, cur.second - 1});
          result[cur.first][cur.second - 1] = id;
        }
        if (cur.second < w - 1 && !reached[cur.first][cur.second + 1] && grid[cur.first][cur.second + 1] == '.' ) {
          reached[cur.first][cur.second + 1] = true;
          q.push({cur.first, cur.second + 1});
          result[cur.first][cur.second + 1] = id;
        }
      }
      id++;
    }
  } 
  return id;
}

void gen(vector<v> &colored, vector<v> &graph) {
  map<p, int> isExist;
  for(int i = 0; i < h; i++) {
    for(int j = 0; j < w; j++) {
      if(colored[i][j] == -1) continue;
      int sh = i - 2 < 0 ? 0 : i - 2;
      int eh = i + 2 >= h ? h - 1 : i + 2;
      int sw = j - 2 < 0 ? 0 : j - 2;
      int ew = j + 2 >= w ? w - 1 : j + 2;
      for(int hi = sh; hi <= eh; hi++) {
        for(int wi = sw; wi <= ew; wi++) {
          if(hi == i && wi == j) continue;
          int cur = colored[i][j];
          int next = colored[hi][wi];
          if (next == -1 || cur == next) continue;
          if (isExist.count({cur, next}) == 0) {
            graph[cur].push_back(next);
            isExist[{cur, next}] = 1;
          }
        }
      }
    }
  }
}

int bfs(int start, int end, vector<v> &graph) {
  queue<p> q;
  vector<bool> reached(graph.size(), false);
  q.push({start, 0});
  reached[start] = true;
  int pops = 0;
  while(!q.empty()) {
    p cur = q.front(); q.pop();
    for(int i = 0; i < graph[cur.first].size(); i++) {
      int next = graph[cur.first][i];
      if (next == end) {
        return cur.second + 1;
      }
      if (reached[next]) continue;
      reached[next] = true;
      q.push({next, cur.second + 1});
    }
  }
  return -1;
}

int main()
{
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  ch--; cw--; dh--; dw--;
  vs grid(h);
  rep(i, h) cin >> grid[i];
  vector<v> colored(h, v(w, -1));
  int ids = decomp(grid, colored);
  /*rep(i, h)
  {
    rep(j, w)
    {
      cout << colored[i][j] << " ";
    }
    cout << endl;
  }*/

  vector<v> graph(ids, v());
  gen(colored, graph);
  /*rep(i, ids) {
    for (auto e : graph[i]) {
      cout << e << " ";
    }
    cout << endl;
  }*/

  if (colored[ch][cw] == colored[dh][dw]) {
    cout << 0 << endl;
  }else{
    int cnt = bfs(colored[ch][cw], colored[dh][dw], graph);
    cout << cnt << endl;
  }
}


Measured memory_bytes: 40584