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: Accepted)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using static System.Console;
using static System.Math;
using static Atcoder.IO;

namespace Atcoder
{
    class Program
    {
        static StreamWriter sw = new StreamWriter(OpenStandardOutput()) { AutoFlush = false };
        static void Main(string[] args)
        {
            try
            {
                SetOut(sw);
                Solve.Answer();
                Out.Flush();
            }
            catch (Exception ex)
            {
                WriteLine(ex.ToString());
                Out.Flush();
            }
        }
    }

    public class Solve
    {
        static int[] HW;
        static string[] map;
        static int[][] warp;
        static int warpCnt;

        public static void Answer()
        {
            HW = RSInt;
            var C = RSInt;
            C[0]--;
            C[1]--;
            var D = RSInt;
            D[0]--;
            D[1]--;

            map = new string[HW[0]];
            for (int i = 0; i < HW[0]; i++)
            {
                map[i] = ReadLine();
            }

            warp = new int[HW[0]][];
            for (int i = 0; i < HW[0]; i++)
            {
                warp[i] = new int[HW[1]];
            }
            for (int i = 0; i < HW[0]; i++)
            {
                for (int j = 0; j < HW[1]; j++)
                {
                    warp[i][j] = -1;
                }
            }

            warpCnt = 0;
            warp[C[0]][C[1]] = warpCnt;
            var que = new Queue<int[]>();
            var que2 = new Queue<int[]>();
            que.Enqueue(new int[] { C[0], C[1] });
            que2.Enqueue(new int[] { C[0], C[1] });

            while (Roop(ref que, ref que2))
            { }

            WriteLine(warp[D[0]][D[1]]);
        }

        static bool Roop(ref Queue<int[]> que, ref Queue<int[]> que2)
        {


            while (que.Count != 0)
            {
                int[] now = que.Dequeue();

                if (now[0] - 1 > 0 && map[now[0] - 1][now[1]] == '.')
                {
                    if (warp[now[0] - 1][now[1]] == -1)
                    {
                        warp[now[0] - 1][now[1]] = warpCnt;
                        que.Enqueue(new int[] { now[0] - 1, now[1] });
                        que2.Enqueue(new int[] { now[0] - 1, now[1] });
                    }
                }
                if (now[1] - 1 > 0 && map[now[0]][now[1] - 1] == '.')
                {
                    if (warp[now[0]][now[1] - 1] == -1)
                    {
                        warp[now[0]][now[1] - 1] = warpCnt;
                        que.Enqueue(new int[] { now[0], now[1] - 1 });
                        que2.Enqueue(new int[] { now[0], now[1] - 1 });
                    }
                }
                if (now[0] + 1 < HW[0] && map[now[0] + 1][now[1]] == '.')
                {
                    if (warp[now[0] + 1][now[1]] == -1)
                    {
                        warp[now[0] + 1][now[1]] = warpCnt;
                        que.Enqueue(new int[] { now[0] + 1, now[1] });
                        que2.Enqueue(new int[] { now[0] + 1, now[1] });
                    }
                }
                if (now[1] + 1 < HW[1] && map[now[0]][now[1] + 1] == '.')
                {
                    if (warp[now[0]][now[1] + 1] == -1)
                    {
                        warp[now[0]][now[1] + 1] = warpCnt;
                        que.Enqueue(new int[] { now[0], now[1] + 1 });
                        que2.Enqueue(new int[] { now[0], now[1] + 1 });
                    }
                }
            }
            warpCnt++;
            while (que2.Count != 0)
            {
                int[] now2 = que2.Dequeue();
                for (int i = -2; i <= 2; i++)
                {
                    for (int j = -2; j <= 2; j++)
                    {
                        if (now2[0] + i < 0 || now2[0] + i >= HW[0] || now2[1] + j < 0 || now2[1] + j >= HW[1])
                            continue;
                        if (warp[now2[0] + i][now2[1] + j] >= 0)
                            continue;
                        if (map[now2[0] + i][now2[1] + j] == '.')
                        {
                            warp[now2[0] + i][now2[1] + j] = warpCnt;
                            que.Enqueue(new int[] { now2[0] + i, now2[1] + j });
                        }
                    }
                }
            }

            int cnt = que.Count;
            for (int i = 0; i < cnt; i++)
            {
                var tmp = que.Dequeue();
                que2.Enqueue(tmp);
                que.Enqueue(tmp);
            }

            return que.Count != 0;
        }
    }

    public class IO
    {
        #region Input
        public static int RInt => int.Parse(ReadLine());
        public static long RLong => long.Parse(ReadLine());
        public static double RDouble => double.Parse(ReadLine());

        public static string[] RSString => ReadLine().Split();
        public static int[] RSInt => ReadLine().Split().Select(s => int.Parse(s)).ToArray();
        public static long[] RSLong => ReadLine().Split().Select(s => long.Parse(s)).ToArray();
        public static double[] RSDouble => ReadLine().Split().Select(s => double.Parse(s)).ToArray();
        #endregion

        #region Output
        public static void OutJoinSpace<T>(IEnumerable<T> list) => WriteLine(string.Join(" ", list));
        public static void WriteLineYes_or_No(bool flag) => WriteLine(flag ? "Yes" : "No");
        public static void WriteLineYES_or_NO(bool flag) => WriteLine(flag ? "YES" : "NO");
        #endregion
    }

    public class Mod
    {
        #region
        private readonly static long M = 1000000007;

        public static long Add(long a, long b) => (a * b) % M;
        public static long Sub(long a, long b) => (a - b) % M >= 0 ? (a - b) % M : (a - b) % M + M;
        public static long Multi(long a, long b) => (a * b) % M;
        #endregion
    }
}