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: Wrong Answer)

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)>();
            var used = new bool[H][].Select(x => new bool[W]).ToArray();
            Q.Enqueue((CH, CW));
            var dy = new[] { 0, 1, 0, -1 };
            var dx = new[] { -1, 0, 1, 0 };
            while (Q.Any())
            {
                var current = Q.Dequeue();
                var ch = current.h;
                var cw = current.w;
                var none = true;
                for (var i = 0; i < 4; i++)
                {
                    var nh = ch + dy[i];
                    var nw = cw + dx[i];
                    if (nh < 0 || nh >= H) continue;
                    if (nw < 0 || nw >= W) continue;
                    if (S[nh][nw] == '#') continue;
                    if (G[nh][nw] > G[ch][cw])
                    {
                        G[nh][nw] = G[ch][cw];
                        Q.Enqueue((nh, nw));
                        none = false;
                    }
                }

                if (none)
                {
                    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 (dh == 0 && Math.Abs(dw) == 1 || Math.Abs(dh) == 1 && dw == 0) continue;
                            if (nh < 0 || nh >= H) continue;
                            if (nw < 0 || nw >= W) continue;
                            if (S[nh][nw] == '#') continue;

                            if (G[nh][nw] > G[ch][cw] + 1)
                            {
                                G[nh][nw] = G[ch][cw] + 1;
                                Q.Enqueue((nh, nw));
                                break;
                            }
                        }
                    }
                }
            }

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