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.IO;
using System.Linq;
using System.Collections.Generic;
using static System.Math;
using static System.Console;
using static System.Linq.Enumerable;
using static System.Numerics.BitOperations;

namespace abc176
{
    class SolverD
    {
        static void Main()
        {
            SetOut(new StreamWriter(Console.OpenStandardOutput()){AutoFlush = false});
            new SolverD().Solve();
            Out.Flush();
        }

        public void Solve()
        {
            checked
            {
                (int H, int W) = Get.Tuple<int,int>();
                (int ch, int cw) = Get.Tuple<int,int>();
                (int dh, int dw) = Get.Tuple<int,int>();
                ch--; cw--; dh--; dw--;
                int[,] maze = new int[H,W];
                for(int i=0; i<H; i++){
                    char[] l = Get.Str().ToCharArray();
                    for(int j=0; j<W; j++){
                        maze[i,j] = l[j] == '.' ? -1 : -2;
                    }
                }
                maze[ch,cw] = 0;

                Queue<Coord> queue = new Queue<Coord>(1100000);
                queue.Enqueue(new Coord{h=ch, w=cw});
                bool goalReached = false;
                while(queue.Any()){

                    // Debug(maze,queue);

                    Coord c = queue.Dequeue();
                    int ci = maze[c.h,c.w];

                    if(c.h == dh && c.w == dw){
                        goalReached = true;
                        // break;
                    }

                    bool walk = false;

                    if(c.w > 0 && IsNewPlace(maze,c.h,c.w-1,ci)){
                        maze[c.h,c.w-1] = ci;
                        queue.Enqueue(new Coord{h=c.h, w=c.w-1});
                        walk = true;
                    }
                    if(c.w < W-1 && IsNewPlace(maze, c.h, c.w+1, ci)){
                        maze[c.h, c.w+1] = ci;
                        queue.Enqueue(new Coord{h=c.h, w=c.w+1});
                        walk = true;
                    }
                    if(c.h > 0 && IsNewPlace(maze, c.h-1, c.w, ci)){
                        maze[c.h-1, c.w] = ci;
                        queue.Enqueue(new Coord{h=c.h-1, w=c.w});
                        walk = true;
                    }
                    if(c.h < H-1 && IsNewPlace(maze, c.h+1, c.w, ci)){
                        maze[c.h+1, c.w] = ci;
                        queue.Enqueue(new Coord{h=c.h+1, w=c.w});
                        walk = true;
                    }

                    if(ShouldCheckWarp(maze,c.h,c.w,H,W)){
                        for(int Hdiff=-2; Hdiff<=2; Hdiff++){
                            for(int Wdiff=-2; Wdiff<=2; Wdiff++){
                                int th = c.h + Hdiff;
                                int tw = c.w + Wdiff;
                                if( ! IsInRange(th, tw, H, W)){
                                    continue;
                                }
                                if( IsNewPlace(maze, th, tw, ci) ){
                                    maze[th,tw] = ci+1;
                                    queue.Enqueue(new Coord{h=th, w=tw});
                                }
                            }
                        }
                    }
                    
                }
                // while ここまで
                if(goalReached){
                    WriteLine(maze[dh,dw]);
                }else{
                    WriteLine(-1);
                }
            }
        }

        bool ShouldCheckWarp(int[,] maze, int h, int w, int HL, int WL){

            return (h>0 && maze[h-1,w]==-2 )||
                (h<HL-1 && maze[h+1,w]==-2) ||
                (w>0 && maze[h,w-1]==-2) ||
                (w<WL-1 && maze[h,w+1]==-2 );
        }

        bool IsNewPlace(int[,] maze, int h, int w, int currentI){
            int ti = maze[h,w];
            if(ti == -2){
                return false;
            }else if(ti == -1){
                return true;
            }else if(ti > currentI){
                return true;
            }
            return false;
        }

        bool IsInRange(int th, int tw, int H, int W){
            return th>=0 && tw>=0 && th<H && tw<W;
        }



        struct Coord{
            public int h;
            public int w;
        }

        static void Debug(int[,] maze, Queue<Coord> q){
            Write("queue: ");
            foreach(var qq in q){
                Write($"({qq.h},{qq.w}) ");
            }
            WriteLine();
            for(int i=0; i<maze.GetLength(0); i++){
                for(int j=0; j<maze.GetLength(1); j++){
                    Write(maze[i,j].ToString().PadLeft(2,' ') + ",");
                }
                Write("\n");
            }
            Write("\n");
            ReadLine();
        }



        private static class Get
        {
            public static string Str() => ReadLine().Trim();
            public static int Int() => int.Parse(Str());
            public static long Long() => long.Parse(Str());
            public static double Double() => double.Parse(Str());
            public static string[] Strs() => Str().Split(' ', StringSplitOptions.RemoveEmptyEntries);
            public static int[] Ints() => Strs().Select(int.Parse).ToArray();
            public static long[] Longs() => Strs().Select(long.Parse).ToArray();
            public static double[] Doubles() => Strs().Select(double.Parse).ToArray();
            static bool TypeEq<T,U>() => typeof(T).Equals(typeof(U));
            static T TypeConv<T,U>(U u) => (T)Convert.ChangeType(u, typeof(T));
            static T TypeConv<T>(string s) => TypeEq<T, int>() ?   TypeConv<T, int>(int.Parse(s))
                                        : TypeEq<T, long>() ?       TypeConv<T, long>(long.Parse(s))
                                        : TypeEq<T, double>() ?     TypeConv<T, double>(long.Parse(s))
                                        : TypeConv<T, string>(s);
            public static (T,U) Tuple<T,U>() {string[] strs = Strs(); T t = TypeConv<T>(strs[0]); U u = TypeConv<U>(strs[1]); return(t,u);}
            public static (T,U,V) Tuple<T,U,V>() {string[] strs = Strs(); T t = TypeConv<T>(strs[0]); U u = TypeConv<U>(strs[1]); V v = TypeConv<V>(strs[2]); return(t,u,v);}
        }
    }
}