Problem:
Score : 400 points

Problem Statement

A maze is composed of a grid of H \times W squares - H vertical, W horizontal.

The square at the i-th row from the top and the j-th column from the left - (i,j) - is a wall if S_{ij} is # and a road if S_{ij} is ..

There is a magician in (C_h,C_w). He can do the following two kinds of moves:

Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.

Move B: Use magic to warp himself to a road square in the 5\times 5 area centered at the square he is currently in.

In either case, he cannot go out of the maze.

At least how many times does he need to use the magic to reach (D_h, D_w)?

Constraints

1 \leq H,W \leq 10^3

1 \leq C_h,D_h \leq H

1 \leq C_w,D_w \leq W

S_{ij} is # or ..

S_{C_h C_w} and S_{D_h D_w} are ..

(C_h,C_w) \neq (D_h,D_w)

Input

Input is given from Standard Input in the following format:

H W
C_h C_w
D_h D_w
S_{11}\ldots S_{1W}
\vdots
S_{H1}\ldots S_{HW}

Output

Print the minimum number of times the magician needs to use the magic. If he cannot reach (D_h,D_w), print -1 instead.

Sample Input 1

4 4
1 1
4 4
..#.
..#.
.#..
.#..

Sample Output 1

1

For example, by walking to (2,2) and then using the magic to travel to (4,4), just one use of magic is enough.

Note that he cannot walk diagonally.

Sample Input 2

4 4
1 4
4 1
.##.
####
####
.##.

Sample Output 2

-1

He cannot move from there.

Sample Input 3

4 4
2 2
3 3
....
....
....
....

Sample Output 3

0

No use of magic is needed.

Sample Input 4

4 5
1 2
2 5
#.###
####.
#..##
#..##

Sample Output 4

2

Solution in C# (.NET Core 3.1.201) (judge verdict: Time Limit Exceeded)

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;
				}
			}

			//0番目 h 1番目 w 2番目 ワープの回数
			var michinori = new LinkedList<int[]>();

			michinori.AddFirst(new int[] { ch, cw, 0 });
			isReached[ch, cw] = true;
			while (michinori.Count() > 0) {
				var current = michinori.First();
				michinori.RemoveFirst();

				var h = current[0];
				var w = current[1];
				var c = current[2];
				if (h == dh && w == dw) {
					Console.WriteLine(c);
					return;
				}
				isReached[h,w] = true;
				for (int i = -2; i <= 2; i++) {
					//枠の外に飛び出る場合は処理対象外
					if (h + i < 0) {
						continue;
					}
					if (h + i >= H) {
						continue;
					}
					for (int j = -2; j <= 2; j++) {
						//枠の外に飛び出る場合は処理対象外
						if (w + j < 0) {
							continue;
						}
						if (w + j >= W) {
							continue;
						}
						//mapが通れない場合も対象外
						if (!map[h + i, w + j]) {
							continue;
						}
						//既に通った道も対象外
						if (isReached[h + i, w + j]) {
							continue;
						}

						//徒歩で進める場合
						if ((Math.Abs(i) == 1 && j == 0) | (Math.Abs(j) == 1 && i == 0)) {
							michinori.AddFirst(new int[] { h + i, w + j, c });
							isReached[h + i, w + j] = true;

						}
						else {//ワープで進む場合
							michinori.AddLast(new int[] { h + i, w + j, c + 1 });
							
						}
					}
				}
			}
			Console.WriteLine(-1);

		}

		/// <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;
		}
	}
}