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

namespace Tasks
{
    public class D
    {
        static void Main(string[] args)
        {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var (H, W) = Scanner.Scan<int, int>();
            var (CH, CW) = Scanner.Scan<int, int>();
            CH--; CW--;
            var (DH, DW) = Scanner.Scan<int, int>();
            DH--; DW--;
            var S = new string[H];
            for (var i = 0; i < H; i++)
            {
                S[i] = Console.ReadLine();
            }

            var inf = (long)1e18;

            var G = new long[H][].Select(x => Enumerable.Repeat(inf, W).ToArray()).ToArray();
            G[CH][CW] = 0;
            var Q = new Queue<(int h, int w)>();
            Q.Enqueue((CH, CW));
            var dy = new[] { 0, 1, 0, -1 };
            var dx = new[] { -1, 0, 1, 0 };

            while (Q.Any())
            {
                var c = Q.Dequeue();
                var ch = c.h;
                var cw = c.w;
                var count = G[ch][cw];

                var wQ = new Queue<(int h, int w)>();
                wQ.Enqueue(c);
                while (wQ.Any())
                {
                    var wc = wQ.Dequeue();
                    var wh = wc.h;
                    var ww = wc.w;
                    for (var i = 0; i < 4; i++)
                    {
                        var nh = wh + dy[i];
                        var nw = ww + dx[i];
                        if (nh < 0 || nh >= H) continue;
                        if (nw < 0 || nw >= W) continue;
                        if (S[nh][nw] == '#') continue;
                        if (G[nh][nw] <= count) continue;

                        G[nh][nw] = count;
                        Q.Enqueue((nh, nw));
                        wQ.Enqueue((nh, nw));
                    }
                }

                count++;
                for (var dh = -2; dh <= 2; dh++)
                {
                    for (var dw = -2; dw <= 2; dw++)
                    {
                        if (dh == 0 && dw == 0) continue;
                        var nh = ch + dh;
                        var nw = cw + dw;
                        if (nh < 0 || nh >= H) continue;
                        if (nw < 0 || nw >= W) continue;
                        if (S[nh][nw] == '#') continue;
                        if (G[nh][nw] <= count) continue;

                        G[nh][nw] = count;
                        Q.Enqueue((nh, nw));
                    }
                }
            }

            if (G[DH][DW] == inf) G[DH][DW] = -1;

            // Console.WriteLine(string.Join("\n", G.Select(x => string.Join(" ", x))));

            Console.WriteLine(G[DH][DW]);

        }

        public static class Scanner
        {
            private static Queue<string> queue = new Queue<string>();
            public static T Next<T>()
            {
                if (!queue.Any()) foreach (var item in Console.ReadLine().Trim().Split(" ")) queue.Enqueue(item);
                return (T)Convert.ChangeType(queue.Dequeue(), typeof(T));
            }
            public static T Scan<T>() => Next<T>();
            public static (T1, T2) Scan<T1, T2>() => (Next<T1>(), Next<T2>());
            public static (T1, T2, T3) Scan<T1, T2, T3>() => (Next<T1>(), Next<T2>(), Next<T3>());
            public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() => (Next<T1>(), Next<T2>(), Next<T3>(), Next<T4>());
            public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() => (Next<T1>(), Next<T2>(), Next<T3>(), Next<T4>(), Next<T5>());
        }
    }
}