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# (Mono-csc 3.5.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Math;
namespace A
{
    class Program
    {
        static void Main()
        {
            var HW = CinTAr;
            var C = CinTAr;
            var D = CinTAr;
            string gdata = "";
            AFOR(HW[0], _ => gdata += Cin);
            Queue<int> nowq = new Queue<int>(), nexq = new Queue<int>() ;
            nowq.Enqueue(_GetARXY(C[0] - 1, C[1] - 1, HW[1]));
            int[] flgs = new int[HW[0] * HW[1]];
            flgs[_GetARXY(C[1] - 1, C[0] - 1, HW[1])] = 1;
            int stage = 1;
            while (true) {
                while (nowq.Count > 0)
                {
                    int q = nowq.Dequeue();
                    nexq.Enqueue(q);
                    var txy = _GetXY(q, HW[1]);
                    int xff = txy[0], yff = txy[1];
                    int x, y, arxy;
                    x = xff + 1; y = yff;
                    arxy = _GetARXY(x, y, HW[1]);
                    if (x >= 0 && y >= 0 && x < HW[1] && y < HW[0] && gdata[arxy] == '.' && (flgs[arxy] == 0 || flgs[arxy] > stage)) { flgs[arxy] = stage; nowq.Enqueue(arxy); }
                    x = xff - 1; y = yff;
                    arxy = _GetARXY(x, y, HW[1]);
                    if (x >= 0 && y >= 0 && x < HW[1] && y < HW[0] && gdata[arxy] == '.' && (flgs[arxy] == 0 || flgs[arxy] > stage)) { flgs[arxy] = stage; nowq.Enqueue(arxy); }
                    x = xff; y = yff + 1;
                    arxy = _GetARXY(x, y, HW[1]);
                    if (x >= 0 && y >= 0 && x < HW[1] && y < HW[0] && gdata[arxy] == '.' && (flgs[arxy] == 0 || flgs[arxy] > stage)) { flgs[arxy] = stage; nowq.Enqueue(arxy); }
                    x = xff; y = yff - 1;
                    arxy = _GetARXY(x, y, HW[1]);
                    if (x >= 0 && y >= 0 && x < HW[1] && y < HW[0] && gdata[arxy] == '.' && (flgs[arxy] == 0 || flgs[arxy] > stage)) { flgs[arxy] = stage; nowq.Enqueue(arxy); }
                }
                if (nexq.Count == 0) { break; }//main while
                //magic
                nowq = new Queue<int>();
                stage++;
                while (nexq.Count > 0)
                {
                    int q = nexq.Dequeue();
                    var txy = _GetXY(q, HW[1]);
                    int xff = txy[0], yff = txy[1];
                    int x, y, arxy;
                    for(int i = - 2;i <= 2; i++)
                    {
                        for(int g = -2;g <= 2; g++)
                        {
                            if(i == 0 && i == 0) { continue; }
                            x = xff + i; y = yff + g;
                            arxy = _GetARXY(x, y, HW[1]);
                            if (x >= 0 && y >= 0 && x < HW[1] && y < HW[0] && gdata[arxy] == '.' && (flgs[arxy] == 0 || flgs[arxy] > stage)) { flgs[arxy] = stage; nowq.Enqueue(arxy); }
                        }
                    }
                }
            }
            Cout = flgs[_GetARXY(D[1] - 1, D[0] - 1, HW[1])] - 1;

            Wait();
        } //main

        static int MOD10_9_7 = (1000000000 + 7);
        static string Yes = "Yes", No = "No";
        static int _GetARXY(int x, int y, int wid) { return y * wid + x; }
        static int[] _GetXY(int arxy, int wid) { return new int[] { arxy % wid , arxy / wid}; }
        static int GetDigits(long n) { return (int)Floor(Log10(n)) + 1; }
        static int GetNumByDigit(long n, int d) { return (int)((n / ((int)Pow(10, d - 1))) % 10); }
        static long Loop(long n, long max){ if (n > max) { return n % (max + 1); } else if (n < 0) { return max - (-n - 1) % (max + 1); } else { return n; }}
        static int Loop(int n, int max) { return (int)Loop((long)n, max); }
        static long PW10(int n) { return (long)Pow(10, n); }
        static long GCD(long a, long b) { if (a < b) { return GCD(b, a); } if (a % b == 0) { return b; } else { return GCD(b, a % b); } }
        static long LCM(long a, long b) { return a * b / GCD(a, b); }
        static void Swap<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
        static void SwapArE<T>(T[] a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; }
        static void SwapLiE<T>(List<T> a, int i, int j) { T t = a[i]; a[i] = a[j]; a[j] = t; }
        static bool IsInt(double d) { return (int)d == d; }
        static string Wait() { return Console.ReadLine(); }
        static string Cin { get { return Wait(); } }
        static string[] CinAr { get { return Cin.Split(' ').Where(x => x != "").ToArray(); } }
        static int CinT { get { return S2i(Cin); } }
        static int[] CinTAr { get { return CinAr.Select(x => S2i(x)).ToArray(); } }
        static long CinL { get { return S2L(Cin); } }
        static long[] CinLAr { get { return CinAr.Select(x => S2L(x)).ToArray(); } }
        static double CinD { get { return S2D(Cin); } }
        static double[] CinDAr { get { return CinAr.Select(x => S2D(x)).ToArray(); } }
        static int S2i(string s) { return int.Parse(s); }
        static long S2L(string s) { return long.Parse(s); }
        static double S2D(string s) { return double.Parse(s); }
        static object Cout { set { Console.WriteLine(value); } }
        static object WCout { set { Console.Write(value); } }
        static string RevStr(string s) { return new string(s.Reverse().ToArray()); }
        static void AFOR(int m, Action<int> ActI) { for (int i = 0; i < m; i++) { ActI(i); } }
        static void AFOR(long m, Action<long> ActI) { for (long i = 0; i < m; i++) { ActI(i); } }
        static void AFOR(long m, Action Act) { AFOR(m, i => Act()); }
        static long[] Divisor(long n)
        {
            if (n == 1) { return new long[] { 1 }; } List<long> r = new List<long>() { 1, n };
            for (long i = 2; i <= Sqrt(n); i++) { if (n % i == 0) { r.Add(i); long f = n / i; if (f != i) { r.Add(f); } } }
            return r.ToArray();
        }
        static IEnumerable<decimal> PrimeFactors(decimal n)
        {
            decimal i = 2, t = n; while (i * i <= n) { if (t % i == 0) { t /= i; yield return i; } else { i++; } }
            if (t != 1) { yield return t; }
        }
        static bool IsPrime(long n)
        {
            if (n == 1) { return false; }
            for (long i = 2; i <= Sqrt(n) + 1; i++) { if (n % i == 0) { return false; } } return true;
        }
        static void CoutAr<Type>(IEnumerable<Type> t) { Cout = string.Join(" ", t); }
        static long nCr(int n, int r) { return nPr(n, r) / Factorial(r); }
        static long nPr(int n, int r) { return FactorDiv(n, n - r); }
        static long FactorDiv(int t, int d) { long r = 1; for (int i = t; i > d; i--) { r *= i; } return r; }
        static long Factorial(int i) { if (i <= 1) { return 1; } return i * Factorial(i - 1); }
        static void CoutPaAr<Type>(IEnumerable<Pair<Type>> p) { CoutAr(p.Select(x => $"({x.a}, {x.b}, {x.c})")); }
    }
    public struct Pair<T>
    {
        public T a, b, c;
        public Pair(T _a, T _b, T _c = default(T)) { a = _a; b = _b; c = _c; }
    }
}