using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder2 {
	class Program {
		static void Main(string[] args) {

			//出力の都度更新をやめる
			var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
			Console.SetOut(sw);


			Ans();

			//更新する
			Console.Out.Flush();
			
		}
		public static void Ans() {

			var inputArray = retreiveLongArray();
			var inputCount = 2;

			// 不正な入力は処理しない
			if (inputArray.Length != inputCount) {
				Console.WriteLine("invalid input");
				return;
			}
			var H = (int)inputArray[0];
			var W = (int)inputArray[1];

			var CArray = retreiveLongArray();
			var ch = (int)CArray[0] - 1;
			var cw = (int)CArray[1] - 1;


			var DArray = retreiveLongArray();
			var dh = (int)DArray[0] - 1;
			var dw = (int)DArray[1] - 1;


			var map = new bool[H, W];
			for (int i = 0; i < H; i++) {
				var a = retreiveStringArray()[0];
				for (int j = 0; j < W; j++) {
					map[i, j] = a[j] == '.';
				}
			}

			var isReached = new bool[H, W];
			for (int i = 0; i < H; i++) {
				for (int j = 0; j < W; j++) {
					isReached[i, j] = false;
				}
			}


			int[][] steps = {
				new int[]{ -1,0},
				new int[]{ 1,0},
				new int[]{ 0,-1},
				new int[]{ 0,1}
			};
			var tohoSearching = new List<Point>();
			var warpSearching = new List<Point>();

			tohoSearching.Add(new Point(ch, cw));

			for (int ans = 0; ans < H*W; ans++) {
				if (tohoSearching.Count()==0 && warpSearching.Count()==0) {
					Console.WriteLine(-1);
					return;
				}

				//まず徒歩圏内でBFS
				for (int i = 0; i < tohoSearching.Count; i++) {

					var h = tohoSearching[i]._h;
					var w = tohoSearching[i]._w;
					foreach (var step in steps) {

						var nexth = h + step[0];
						var nextw = w + step[1];
						if (nexth == dh && nextw == dw) {
							Console.WriteLine(ans);
							return;
						}
						if (0 <= nexth && nexth < H && 0 <= nextw && nextw < W) {

							if (!map[nexth,nextw]) {
								continue;
							}
							if (isReached[nexth, nextw]) {
								continue;
							}
							isReached[nexth, nextw] = true;
							tohoSearching.Add(new Point(nexth, nextw));
						}
					}

				}

				//次にワープで行ける場所を調べる

				foreach (var point in tohoSearching) {
					var hw = point._h;
					var ww = point._w;

					for (int i = Math.Max(0,hw-2); i < Math.Min(H,hw+3); i++) {
						for (int j = Math.Max(0, ww - 2); j < Math.Min(W, ww + 3); j++) {
							if (i==dh && j==dw) {
								Console.WriteLine(ans+1);
								return;
							}


							if (!map[i, j]) {
								continue;
							}
							if (isReached[i, j]) {
								continue;
							}
							isReached[i, j] = true;
							warpSearching.Add(new Point(i, j));
						}
					}
				}
				tohoSearching = warpSearching;
				warpSearching = new List<Point>();
			}

		}



		public  struct Point
		{
			public readonly int _h;
			public readonly int _w;

			public Point(int h,int w) {
				_h=h;
				_w = w;
			}
		}

		/// <summary>
		/// スペースで区切られた文字列配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static string[] retreiveStringArray() {
			var inputArray = Console.ReadLine().Split(' ');
			return inputArray;
		}

		/// <summary>
		/// スペースで区切られた整数配列の入力を受け取る
		/// </summary>
		/// <returns></returns>
		private static long[] retreiveLongArray() {
			var inputArray = Console.ReadLine().Split(' ');
			var longArray = inputArray.Select(s => long.Parse(s));
			return longArray.ToArray();
		}


		/// <summary>
		/// 2組みずつの入力データ(数値)からTuppleListを作成する
		/// </summary>
		/// <param name="N"></param>
		/// <returns></returns>
		private static List<Tuple<long, long>> createLongTuppleList(long N) {
			var inpList = new List<Tuple<long, long>>();

			for (int i = 0; i < N; i++) {
				var inp = retreiveLongArray();
				var item1 = inp[0];
				var item2 = inp[1];
				inpList.Add(new Tuple<long, long>(item1, item2));
			}
			return inpList;
		}
	}
}


Measured memory_bytes: 46900