#include<bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(p) p.begin(), p.end()
#define pop_count(p) __builtin_popcountll(p)
#define len(s, e) ((e)-(s)+1)
#define mid(s, e) ((s)+((e)-(s))/2)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const int MOD = 1e9 + 7;
const int oo = 1e9;
const ll OO = 1e18;
#ifdef ONLINE_JUDGE
#define endl '\n'
#endif
template <typename A, typename B>
std::ostream& operator <<(std::ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
std::ostream& operator <<(std::ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
std::ostream& operator <<(std::ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
std::ostream& operator <<(std::ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
template <typename T>
std::istream& operator >>(std::istream& in, vector<T>& a) {
  for (auto& v : a) in >> v;
  return in;
}
#ifdef ANEEE_LOCAL
template <typename T>
std::ostream& operator <<(std::ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
#else
template <typename T>
std::ostream& operator <<(std::ostream& out, const vector<T>& a) {
  for (const auto& v : a) out << v << ' ';
  return out;
}
#endif
#ifdef ANEEE_LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 7
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " | ";
  __f(comma + 1, args...);
}

class Timer {
	clock_t start;
public:
	Timer() {start = clock();}
	~Timer() {cerr << (double)(clock() - start)/CLOCKS_PER_SEC << "\n";}
};

#ifdef ONLINE_JUDGE
#undef CASE_INFO
#endif

vector<string> grid;
int xgoal, ygoal, n, m;
int dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};
int mdx[] = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2};
int mdy[] = {-2, -1, 0, 1, 2, -2, -1, 0, 1, 2, -2, -1, 1, 2, -2, -1, 0, 1, 2, -2, -1, 0, 1, 2};
void solve() {
	cin >> n >> m;
	int xstart, ystart;
	cin >> xstart >> ystart;
	cin >> xgoal >> ygoal;
	xstart--; ystart--; xgoal--; ygoal--;
	grid.resize(n);
	for(int i = 0; i < n; i++) cin >> grid[i];
	priority_queue<pair<int, pair<int, int>>> q;
	vector<vector<int>> d(n, vector<int>(m, oo));
	q.push(make_pair(0, make_pair(xstart, ystart)));
	d[xstart][ystart] = 0;
	while(q.size()) {
		auto cur = q.top(); q.pop();
		int r = cur.S.F, c = cur.S.S, ma = -cur.F;
		if(ma > d[r][c]) continue;
		for(int k = 0; k < 4; k++) {
			int x = r + dx[k], y = c + dy[k];
			if(x >= 0 and y >= 0 and x < n and y  < m and grid[x][y] != '#') {
				if(ma < d[x][y]) {
					d[x][y] = ma;
					q.push(make_pair(-ma, make_pair(x, y)));
				}
			}
		}
		for(int k = 0; k < 24; k++) {
			int x = r + mdx[k], y = c + mdy[k];
			if(x >= 0 and y >= 0 and x < n and y  < m and grid[x][y] != '#') {
				trace(r, c, x, y);
				if(ma+1 < d[x][y]) {
					d[x][y] = ma+1;
					q.push(make_pair(-ma-1, make_pair(x, y)));
				}
			}
		}
	}
	for(int i = 0; i < n; i++) trace(d[i]);
	cout << (d[xgoal][ygoal] == oo ? -1 : d[xgoal][ygoal]) << endl;
}

signed main() {
	Timer ti;
	std::ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	int T = 1;
	#ifdef TEST_CASES
	cin >> T;
	#endif
	for(int t = 1; t <= T; t++) {
		#ifdef CASE_INFO
		cout << "Case #" << t << ": ";
		#endif
		solve();
	}
	return 0;
}


Measured memory_bytes: 8620