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.Linq;
using System.Collections;
using System.Collections.Generic;

namespace ABC176_D
{
    using static Ex;
    public class D
    {
        public static void Main(string[] args)
        {
            new ABC();
        }
    }

    public class ABC
    {
        int h, w;
        (int, int) st;
        (int, int) goal;
        bool[][] arr;

        int[] vx = { 0, 1, 0, -1 };
        int[] vy = { 1, 0, -1, 0 };

        /// <summary>
        /// # のとき壁であり、.のとき道
        /// 
        /// マス(Ch,Cw) -> (Dh,Dw)まで
        /// 
        /// 移動A：現在いるマスと上下左右に隣接する道のマスへ歩いて移動する。
        /// 移動B：現在いるマスを中心とする5×5の範囲内にある道のマスへワープ魔法で移動する。
        ///
        /// ワープ魔法を使う最小回数
        /// 到達不可能な場合、かわりに -1
        /// </summary>
        public ABC()
        {
            (h, w) = ReadIntArray().ToTpl2();
            st = ReadIntArray().ToTpl2();
            goal = ReadIntArray().ToTpl2();
            arr = new bool[h][];
            for (int i = 0; i < h; i++)
            {
                arr[i] = Read.Select(v => v == '.').ToArray();
            }
            st.Item1--; st.Item2--; goal.Item1--; goal.Item2--;

            var q = new Queue<(int, int, long)>();
            var qq = new Queue<(int, int, long)>();
            q.Enqueue((st.Item1, st.Item2, 0));
            qq.Enqueue((st.Item1, st.Item2, 0));
            while (qq.Any())
            {
                while (q.Any())
                {
                    var now = q.Dequeue();

                    if (now.Item1 == goal.Item1 && now.Item2 == goal.Item2)
                    {
                        now.Item3.Cw();
                        return;
                    }

                    var y = now.Item1;
                    var x = now.Item2;
                    var step = now.Item3;
                    arr[y][x] = false;
                    for (int i = 0; i < 4; i++)
                    {
                        int nx = x + vx[i];
                        int ny = y + vy[i];
                        if ((0 <= nx && nx < w) && (0 <= ny && ny < h) && arr[ny][nx])
                        {
                            arr[ny][nx] = false;
                            q.Enqueue((ny, nx, step));
                            qq.Enqueue((ny, nx, step));
                        }
                    }
                }
                {

                    var now = qq.Dequeue();

                    if (now.Item1 == goal.Item1 && now.Item2 == goal.Item2)
                    {
                        now.Item3.Cw();
                        return;
                    }

                    var y = now.Item1;
                    var x = now.Item2;
                    var step = now.Item3;
                    for (int i = -2; i <= 2; i++)
                        for (int j = -2; j <= 2; j++)
                        {
                            int nx = x + i;
                            int ny = y + j;
                            //if((0 <= nx && nx < w) && (0 <= ny && ny < h)) $" {nx} {ny} {arr[ny][nx]}{ used[ny][nx]}".Cw();
                            if ((0 <= nx && nx < w) && (0 <= ny && ny < h) && arr[ny][nx])
                            {
                                arr[ny][nx] = false;
                                q.Enqueue((ny, nx, step + 1));
                                qq.Enqueue((ny, nx, step + 1));
                            }
                        }
                }
            }
            (-1).Cw();
        }
    }

    public static class Ex
    {
        public static void Cw(this object s) { Console.WriteLine(s); }



        public static bool Mins<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
        public static bool Maxs<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
        public static void Swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
        public static void Swap<T>(List<T> list, int i, int j) { T tmp = list[j]; list[j] = list[i]; list[i] = tmp; }

        public static long Lcm(int x, int y) { return x / Gcd(x, y) * y; }
        public static long Gcd(long m, long n) { if (n == 0) return m; return Gcd(n, m % n); }
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.Ordinal); return s.ToList(); }
        public static void yesno(this bool b) { (b ? "yes" : "no").Cw(); }
        public static void YesNo(this bool b) { (b ? "Yes" : "No").Cw(); }
        public static void YESNO(this bool b) { (b ? "YES" : "NO").Cw(); }

        public static string Read { get { return Console.ReadLine().Trim(); } }
        public static int ReadInt { get { return int.Parse(Read); } }
        public static long ReadLong { get { return long.Parse(Read); } }
        public static double ReadDouble { get { return double.Parse(Read); } }
        public static string[] ReadArray() { return Read.Split(' '); }
        public static int[] ReadIntArray() { return Read.Split(' ').Select(int.Parse).ToArray(); }
        public static long[] ReadLongArray() { return Read.Split(' ').Select(long.Parse).ToArray(); }
        public static string[] ReadArray(long N) { var v = new string[N]; for (long i = 0; i < N; ++i) v[i] = Read; return v; }
        public static int[] ReadIntArray(long N) { var v = new int[N]; for (long i = 0; i < N; ++i) v[i] = ReadInt; return v; }
        public static long[] ReadLongArray(long N) { var v = new long[N]; for (long i = 0; i < N; ++i) v[i] = ReadLong; return v; }

        public static (T, T) ToTpl2<T>(this T[] v) { return (v[0], v[1]); }
        public static (T, T, T) ToTpl3<T>(this T[] v) { return (v[0], v[1], v[2]); }
        public static (T, T, T, T) ToTpl4<T>(this T[] v) { return (v[0], v[1], v[2], v[3]); }
        public static (T, T, T, T, T) ToTpl5<T>(this T[] v) { return (v[0], v[1], v[2], v[3], v[4]); }
    }
}