using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;

namespace AtCoder.ABC
{
	class QuestionD
	{
		class XY<T> : IEqualityComparer<XY<T>>, IEquatable<XY<T>>
		{
			public T X;
			public T Y;

			public XY(T x, T y)
			{
				X = x;
				Y = y;
			}

			public override bool Equals(object obj)
			{
				if (obj is XY<T> xy) {
					return Equals(xy);
				} else {
					return false;
				}
			}

			public bool Equals(XY<T> other)
			{
				return Equals(this, other);
			}

			public bool Equals(XY<T> x, XY<T> y)
			{
				return x.X.Equals(y.X) && x.Y.Equals(y.Y);
			}

			public override int GetHashCode()
			{
				return GetHashCode(this);
			}

			public int GetHashCode(XY<T> obj)
			{
				if (obj is null) {
					throw new NotImplementedException();
				}

				return (X.GetHashCode() * 333) + Y.GetHashCode();
			}
		}

		public static void Main(string[] args)
		{
            var scanner = new Scanner();

			var (h, w) = scanner.Int2();
			var (ch, cw) = scanner.Int2(-1);
			var (dh, dw) = scanner.Int2(-1);

			var map = new string[h];
			var yetreached = new bool[h][];

			for(int i = 0; i < h; i++) {
				map[i] = scanner.NextLine();
				yetreached[i] = new bool[w];
			}

			var hash = new HashSet<XY<int>>();
			var queue = new Queue<XY<int>>();

			queue.Enqueue(new XY<int>(cw, ch));

			hash.Add(new XY<int>(cw, ch));
			yetreached[ch][cw] = true;

			for(int j = 0; true; j++) { 
				if(hash.Count == 0) {
					break;
				}

				if(j != 0) {
					var temp = new HashSet<XY<int>>();

					foreach(var a in hash) {
						for(int k = -2; k <= 2; k++) {
							for(int l = -2; l <= 2; l++) {
								var xi = a.X + k;
								var yi = a.Y + l;


								if (xi < 0 || xi >= w || yi < 0 || yi >= h) {
									continue;
								}

								if (map[yi][xi] == '#' || yetreached[yi][xi]) {
									continue;
								}

								if (xi == dw && yi == dh) {
									Console.WriteLine(j);
									return;
								}

								var newXY = new XY<int>(xi, yi);

								yetreached[yi][xi] = true;
								temp.Add(newXY);
								queue.Enqueue(newXY);
							}
						}
					}

					hash = temp;
				}

				while (queue.Count != 0) {
					var xy = queue.Dequeue();

					var tmp = new (int x, int y)[] {
						(x: 0, y: -1),
						(x: -1, y: 0),
						(x: 0, y: 1),
						(x: 1, y: 0),
					};

					for (int i = 0; i < tmp.Length; i++) {
						var xi = xy.X + tmp[i].x;
						var yi = xy.Y + tmp[i].y;

						if (xi < 0 || xi >= w || yi < 0 || yi >= h) {
							continue;
						}

						if (map[yi][xi] == '#' || yetreached[yi][xi]) {
							continue;
						}

						if (xi == dw && yi == dh) {
							Console.WriteLine(j);
							return;
						}

						var newXY = new XY<int>(xi, yi);

						yetreached[yi][xi] = true;
						hash.Add(newXY);
						queue.Enqueue(newXY);
					}
				}
			}

			Console.WriteLine(-1);
		}

		public class Scanner
		{
			private readonly char[] delimiter_ = new char[] { ' ' };
			private readonly string filePath_;
			private readonly Func<string> reader_;
			private string[] buf_;
			private int index_;

			public Scanner(string file = "")
			{
				if (string.IsNullOrWhiteSpace(file)) {
					reader_ = Console.ReadLine;
				} else {
					filePath_ = file;
					var fs = new StreamReader(file);
					reader_ = fs.ReadLine;
				}
				buf_ = new string[0];
				index_ = 0;
			}

			public string NextLine() => reader_();
			public string Next()
			{
				if (index_ < buf_.Length) {
					return buf_[index_++];
				}

				string st = reader_();
				while (st == "") {
					st = reader_();
				}

				buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
				if (buf_.Length == 0) {
					return Next();
				}

				index_ = 0;
				return buf_[index_++];
			}

			public int Int() => int.Parse(Next());
			public long Long() => long.Parse(Next());
			public double Double() => double.Parse(Next());

			public int[] ArrayInt(int N, int add = 0)
			{
				int[] Array = new int[N];
				for (int i = 0; i < N; i++) {
					Array[i] = Int() + add;
				}
				return Array;
			}

			public long[] ArrayLong(int N, long add = 0)
			{
				long[] Array = new long[N];
				for (int i = 0; i < N; i++) {
					Array[i] = Long() + add;
				}
				return Array;
			}

			public double[] ArrayDouble(int N, double add = 0)
			{
				double[] Array = new double[N];
				for (int i = 0; i < N; i++) {
					Array[i] = Double() + add;
				}
				return Array;
			}

			public (int, int) Int2(int add = 0)
			{
				var array = ArrayInt(2, add);
				return (array[0], array[1]);
			}

			public (int, int, int) Int3(int add = 0)
			{
				var array = ArrayInt(3, add);
				return (array[0], array[1], array[2]);
			}

			public (long, long) Long2(int add = 0)
			{
				var array = ArrayLong(2, add);
				return (array[0], array[1]);
			}

			public (long, long, long) Long3(int add = 0)
			{
				var array = ArrayLong(3, add);
				return (array[0], array[1], array[2]);
			}

			public (double, double) Double2(int add = 0)
			{
				var array = ArrayDouble(2, add);
				return (array[0], array[1]);
			}

			public (double, double, double) Double3(int add = 0)
			{
				var array = ArrayDouble(3, add);
				return (array[0], array[1], array[2]);
			}

			public void Save(string text)
			{
				if (string.IsNullOrWhiteSpace(filePath_)) {
					return;
				}

				File.WriteAllText(filePath_ + "_output.txt", text);
			}
		}
	}
}


Measured memory_bytes: 49852