#pragma GCC diagnostic ignored "-Wunused-variable" 
#include <bits/stdc++.h>
using namespace std;

#define BOOST
#ifdef BOOST
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using namespace boost;
using ml = multiprecision::cpp_int;
using md = multiprecision::cpp_dec_float_100;
#endif

/***** type *****/
using ll = long long;
using ld = long double;
using pll = pair<long long, long long>;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vector<T>>;
template <class T> using vvvt = vector<vector<vector<T>>>;
/***** define *****/
#define all(c) (c).begin(), (c).end()            // begin to end
#define coutld cout << fixed << setprecision(10) // cout double
#define output(x) do { cout << x << endl; exit(0); } while(0)
#define rep(i, b, e) for (ll i = b; i < e; i++)  // repeat
#define repr(i, b, e) for (ll i = b; e < i; i--) // repeat reverse
#define fori(i, ...) if (ll i = -1) for(__VA_ARGS__) if (i++, 1)
#define each(i, e, c) fori (i, auto&& e: c)      // for each
/***** const value *****/
#define llong_max 9223372036854775807            // 9 * 10^18
#define ldbl_max 1.79769e+308                    // 1.7 * 10^308
#define pi 3.1415926535897932                    // 3.14 ...
/***** lambda *****/
auto Ceil = [] // if (a % b != 0) return a / b + 1;
(auto x) { return (ll)ceil(x); };
auto Count = [] // long long count value
(auto b, auto e, auto x) { return (ll)count(b, e, x); };
auto CtoL = [] // char to number
(auto c) { return (ll)c - (ll)'0'; };
auto LtoC = [] // number to char
(auto n) { return (char)('0' + n); };
auto Pow = [] // long long pow
(auto a, auto b) { return (ll)pow(a, b); };
auto Pow2 = [] // long long pow2
(auto n) { return (1LL << n); };
auto Pow10 = [] // long long pow10
(auto n) { return (ll)pow(10, n); };
auto Size = []  // long long collection size
(auto& c) { return (ll)(c).size(); };
auto Sum = [] // long long accumulate
(auto b, auto e) { return accumulate(b, e, 0LL); };

/***** operator *****/
template <class T, class S> pair<T, S> operator + (pair<T, S> l, pair<T, S> r)
{	// pair<T, S> + pair<T, S>
	return { l.first + r.first, l.second + r.second };
}
template <class T, class S> pair<T, S> operator - (pair<T, S> l, pair<T, S> r)
{	// pair<T, S> - pair<T, S>
	return { l.first - r.first, l.second - r.second };
}

/***** template *****/
template <class T> void MakeVVT
(ll ys, ll xs, vvt<T>& v, T fill = T())
{	// vector<vector<T>> resize + fill
	v.resize(ys); rep(y, 0, ys) v[y].resize(xs, fill);
}
template <class T> void MakeVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fill = T())
{	// vector<vector<vector<T>>> resize + fill
	v.resize(zs); rep(z, 0, zs) MakeVVT(ys, xs, v[z], fill);
}
template <class T> void InputVVT
(ll ys, ll xs, vvt<T>& v, T fix = T())
{	// input vector<vector<T>> (T != struct) + fix
	MakeVVT(ys, xs, v, fix);
	rep(y, 0, ys) rep(x, 0, xs) { cin >> v[y][x]; v[y][x] += fix; }
}
template <class T> void InputVVVT
(ll zs, ll ys, ll xs, vvvt<T>& v, T fix = T())
{	// input vector<vector<vector<T>>> (T != struct) + fix
	v.resize(zs); rep(z, 0, zs) InputVVT(ys, xs, v[z], fix);
}

/**************************************/
/********** BEGIN OF NYA LIB **********/
/**************************************/

namespace NyaGadget {}

namespace NyaGadget
{
	/***** グリッドグラフ変換ライブラリ *****/

	struct GT_NyaaGridGraph
	{
		// 頂点indexと座標を対応させた配列
		vector<pair<long long, long long>> gtable;
		// 座標と頂点indexを対応させた配列(頂点にならなかった座標は-1)
		vector<vector<long long>> vtable;
		// 重み付き隣接リスト
		vector<vector<pair<long long, long long>>> wlg;

		/**
		@brief 隣接9マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param graph 出力グラフ
		@param wall 移動不可のグリッド値
		@note
		 中央を含む隣接9マスの座標で値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert9
		(const vector<vector<T>>& grid, vector<vector<long long>>& graph, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			graph.clear();
			graph.resize(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					for (long long y3 = y - 1; y3 < y + 2; y3++)
					{
						if (y3 < 0 || (long long)grid.size() <= y3) continue;
						for (long long x3 = x - 1; x3 < x + 2; x3++)
						{
							if (x3 < 0 || (long long)grid[y3].size() <= x3) continue;
							if (vtable[y3][x3] == -1) continue;
							graph[vtable[y][x]].push_back(vtable[y3][x3]);
						}
					}
				}
			}
		}

		/**
		@brief 隣接9マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param wlg 重み付き隣接リスト
		@param wall 移動不可のグリッド値
		@note
		 中央を含む隣接9マスの座標で値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert9
		(const vector<vector<T>>& grid, vector<vector<pair<long long, long long>>>& wlg, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			vector<vector<long long>> graph(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					for (long long y3 = y - 1; y3 < y + 2; y3++)
					{
						if (y3 < 0 || (long long)grid.size() <= y3) continue;
						for (long long x3 = x - 1; x3 < x + 2; x3++)
						{
							if (x3 < 0 || (long long)grid[y3].size() <= x3) continue;
							if (vtable[y3][x3] == -1) continue;
							graph[vtable[y][x]].push_back(vtable[y3][x3]);
						}
					}
				}
			}

			wlg.clear();
			wlg.resize(graph.size());
			for (long long i = 0; i < (long long)graph.size(); i++)
			{
				for (long long j = 0; j < (long long)graph[i].size(); j++)
				{
					wlg[i].push_back({ graph[i][j],1 });
				}
			}
		}

		/**
		@brief 上下左右5マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param graph 出力グラフ
		@param wall 移動不可のグリッド値
		@note
		 中央を含む上下左右5マスのグリッドで値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert5
		(const vector<vector<T>>& grid, vector<vector<long long>>& graph, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			graph.clear();
			graph.resize(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					if (0 <= y - 1 && vtable[y - 1][x] != -1)
						graph[vtable[y][x]].push_back(vtable[y - 1][x]);
					if (x + 1 < (long long)grid[y].size() && vtable[y][x + 1] != -1)
						graph[vtable[y][x]].push_back(vtable[y][x + 1]);
					if (y + 1 < (long long)grid.size() && vtable[y + 1][x] != -1)
						graph[vtable[y][x]].push_back(vtable[y + 1][x]);
					if (0 <= x - 1 && vtable[y][x - 1] != -1)
						graph[vtable[y][x]].push_back(vtable[y][x - 1]);
				}
			}
		}

		/**
		@brief 上下左右5マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param lg 重み付き出力グラフ
		@param wall 移動不可のグリッド値
		@note
		 中央を含む上下左右5マスのグリッドで値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> void Convert5
		(const vector<vector<T>>& grid, vector<vector<pair<long long, long long>>>& lg, T wall = '#')
		{
			ll vmax = 0;
			vtable.clear();
			vtable.resize(grid.size());
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] != wall)
					{
						vtable[y].push_back(vmax++);
						gtable.push_back({ y,x });
					}
					else
					{
						vtable[y].push_back(-1);
					}
				}
			}

			vector<vector<long long>> graph(vmax);
			for (long long y = 0; y < (long long)grid.size(); y++)
			{
				for (long long x = 0; x < (long long)grid[y].size(); x++)
				{
					if (vtable[y][x] == -1) continue;
					if (0 <= y - 1 && vtable[y - 1][x] != -1)
						graph[vtable[y][x]].push_back(vtable[y - 1][x]);
					if (x + 1 < (long long)grid[y].size() && vtable[y][x + 1] != -1)
						graph[vtable[y][x]].push_back(vtable[y][x + 1]);
					if (y + 1 < (long long)grid.size() && vtable[y + 1][x] != -1)
						graph[vtable[y][x]].push_back(vtable[y + 1][x]);
					if (0 <= x - 1 && vtable[y][x - 1] != -1)
						graph[vtable[y][x]].push_back(vtable[y][x - 1]);
				}
			}

			lg.clear();
			lg.resize(graph.size());
			for (long long i = 0; i < (long long)graph.size(); i++)
			{
				for (long long j = 0; j < (long long)graph[i].size(); j++)
				{
					lg[i].push_back({ graph[i][j],1 });
				}
			}
		}

		/**
		@brief 隣接25マスのグリッドグラフに変換する
		@param grid 入力グリッド
		@param wall 移動不可のグリッド値
		@note
		 中央を含む隣接25マスの座標で値がwall以外の座標を頂点としてグラフを作る。
		**/
		template <class T> decltype(wlg)& Convert25(const vector<vector<T>>& grid, T wall = '#')
		{
			const auto H = (long long)grid.size();
			const auto W = (long long)grid[0].size();

			wlg.resize(H * W);
			for (auto y = 0LL; y < (long long)grid.size(); y++)
			{
				for (auto x = 0LL; x < (long long)grid[y].size(); x++)
				{
					if (grid[y][x] == wall) continue;
					auto self = GetVertex({ y, x }, grid);
					rep(yy, -2, 3) rep(xx, -2, 3)
					{
						if (y + yy < 0 || H <= y + yy) continue;
						if (x + xx < 0 || W <= x + xx) continue;
						if (grid[y + yy][x + xx] == wall) continue;
						auto next = GetVertex({ y + yy, x + xx }, grid);
						if (abs(yy) + abs(xx) == 1) wlg[self].push_back({ next, 0 });
						else wlg[self].push_back({ next, 1 });
					}
				}
			}
			return wlg;
		}

		// 頂点vと対応する座標を取得する関数
		pair<long long, long long> GetGrid(long long v) { return gtable[v]; }
		// 座標(y,x)と対応する頂点indexを取得する関数
		template <class T> long long GetVertex(std::pair<long long, long long> yx, const vector<vector<T>>& grid)
		{	// 頂点が存在しない(移動不可座標)またはグリッド範囲外のとき-1を返す
			if (yx.first < 0 || (long long)grid.size() <= yx.first || yx.second < 0 || (long long)grid[0].size() <= yx.second) return -1;
			return yx.first * grid[0].size() + yx.second;
		}
	};
}

namespace NyaGadget
{
	/*** ダイクストラ法ライブラリ ***/

	struct GT_Dijkstra
	{	// 結果を格納する変数
		std::vector<long long> res;

		/**
		@brief 隣接行列の最短コストを求める
		@param s スタート地点
		@param mg 重みつき隣接行列
		@note
		 計算量O((V+E)log(V))
		 重み付き隣接行列で存在しない辺コストはLLONG_MAXとする。
		 mg[f][t]=cは頂点fから頂点tの辺コストがcを表す
		**/
		decltype(res)& Run(long long s, const std::vector<std::vector<long long>>& mg)
		{
			long long INF = LLONG_MAX;
			long long N = (long long)mg.size(); // 頂点数
			int WHITE = 0; // 最短距離が確定してない
			int GRAY = 1;  // 最短距離が更新されたことを表す(処理には無関係)
			int BLACK = 2; // 最短距離確定

			std::vector<long long> d(N), color(N);
			// pair.first = cost, pair.second = v(to)
			std::priority_queue<std::pair<long long, long long>> PQ;
			for (long long i = 0; i < N; i++)
			{
				d[i] = INF;
				color[i] = WHITE;
			}

			d[s] = 0;
			color[s] = GRAY;
			PQ.push({ 0, s });
			while (!PQ.empty())
			{
				std::pair<long long, long long> e = PQ.top(); PQ.pop();
				long long f = e.second;
				color[f] = BLACK;
				if (d[f] < e.first * (-1)) continue;

				for (long long t = 0; t < N; t++)
				{
					if (color[t] == BLACK) continue;
					if (mg[f][t] == INF) continue;
					if (d[t] > d[f] + mg[f][t])
					{
						d[t] = d[f] + mg[f][t];
						// 逆順で並べるため-1倍する
						PQ.push({ d[t] * (-1), t });
						color[t] = GRAY;
					}
				}
			}
			res.resize((long long)d.size());
			for (long long i = 0; i < (long long)d.size(); i++) res[i] = (d[i] != INF) ? d[i] : -1;
			return res;
		}

		/**
		@brief 隣接リストの最短コストを求める
		@param s スタート地点
		@param lg 重みつき隣接リスト
		@param res 結果を格納する構造体
		@note
		 計算量O((V+E)log(V))
		 G[a][i]=(b,c)は頂点aの辺i(a->b)のコストがcを表す
		**/
		decltype(res)& Run(long long s, const std::vector<std::vector<std::pair<long long, long long>>>& lg)
		{
			long long INF = LLONG_MAX;
			long long N = (long long)lg.size(); // 頂点数
			int WHITE = 0; // 最短距離が確定してない
			int GRAY = 1;  // 最短距離が更新されたことを表す(処理には無関係)
			int BLACK = 2; // 最短距離確定

			std::vector<long long> d(N), color(N);
			// pair.first = cost, pair.second = v(to)
			std::priority_queue<std::pair<long long, long long>> PQ;
			for (long long i = 0; i < N; i++)
			{
				d[i] = INF;
				color[i] = WHITE;
			}

			d[s] = 0;
			color[s] = GRAY;
			PQ.push({ 0, s });
			while (!PQ.empty())
			{
				std::pair<long long, long long> e = PQ.top(); PQ.pop();
				long long f = e.second;
				color[f] = BLACK;
				if (d[f] < e.first * (-1)) continue;

				for (long long i = 0; i < (long long)lg[f].size(); i++)
				{
					long long t = lg[f][i].first;
					if (color[t] == BLACK) continue;
					if (d[t] > d[f] + lg[f][i].second)
					{
						d[t] = d[f] + lg[f][i].second;
						// 逆順で並べるため-1倍する
						PQ.push({ d[t] * (-1), t });
						color[t] = GRAY;
					}
				}
			}
			res.resize((long long)d.size());
			for (long long i = 0; i < (long long)d.size(); i++) res[i] = (d[i] != INF) ? d[i] : -1;
			return res;
		}
	};
}

/**************************************/
/*********** END OF NYA LIB ***********/
/**************************************/

using namespace NyaGadget;
//using mll = NT_ModLL< 1000000007 >;
//using mll = ModLL< 998244353 >;

int main()
{
	ll H, W; cin >> H >> W;
	pll C; cin >> C.first >> C.second; C.first--, C.second--;
	pll D; cin >> D.first >> D.second; D.first--, D.second--;
	vvt<char> grid; InputVVT(H, W, grid);

	GT_NyaaGridGraph gg;
	auto wlg = gg.Convert25(grid);

	GT_Dijkstra gtd;
	auto res = gtd.Run(gg.GetVertex(C, grid), wlg);
	cout << res[gg.GetVertex(D, grid)];

	return 0;
}


Measured memory_bytes: 323672