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 Kotlin (1.3.71) (judge verdict: Accepted)

import java.io.BufferedReader
import java.io.BufferedWriter

@ExperimentalStdlibApi
fun main() = Case().exec(::solve)
//fun main() = TestExecutor.assert(testCase) { Case(it.r, it.w).exec(::solve) }

/** https://atcoder.jp/contests/abc176/tasks/abc176_d **/
@ExperimentalStdlibApi
fun solve(c: Case) {
    val (h, w) = c.intList()
    val (ch, cw) = c.intList().map { it - 1 }
    val (dh, dw) = c.intList().map { it - 1 }
    val maze = (0 until h).map { c.string().toCharArray() }
    val scores = (0 until h).map { arrayOfNulls<Int>(w).also { it.fill(Int.MAX_VALUE) } }

    val deque = ArrayDeque<SearchPos>()
    deque.addFirst(SearchPos(ch, cw, 0))

    fun bfs(): Int? {
        val (y, x, score) = deque.removeFirstOrNull() ?: return -1

        if (y < 0 ||
            y >= h ||
            x < 0 ||
            x >= w ||
            maze[y][x] == '#'
        ) {
            return null // not valid place
        }

        if (scores[y][x] != Int.MAX_VALUE) {
            return null // already searched
        }
        if (y == dh && x == dw) {
            return score // found!
        }
        scores[y][x] = score

        deque.addFirst(SearchPos(y + 1, x, score))
        deque.addFirst(SearchPos(y - 1, x, score))
        deque.addFirst(SearchPos(y, x + 1, score))
        deque.addFirst(SearchPos(y, x - 1, score))

        deque.addLast(SearchPos(y - 2, x - 2, score + 1))
        deque.addLast(SearchPos(y - 2, x - 1, score + 1))
        deque.addLast(SearchPos(y - 2, x, score + 1))
        deque.addLast(SearchPos(y - 2, x + 1, score + 1))
        deque.addLast(SearchPos(y - 2, x + 2, score + 1))

        deque.addLast(SearchPos(y - 1, x - 2, score + 1))
        deque.addLast(SearchPos(y - 1, x - 1, score + 1))
        deque.addLast(SearchPos(y - 1, x + 1, score + 1))
        deque.addLast(SearchPos(y - 1, x + 2, score + 1))

        deque.addLast(SearchPos(y, x - 2, score + 1))
        deque.addLast(SearchPos(y, x + 2, score + 1))

        deque.addLast(SearchPos(y + 1, x - 2, score + 1))
        deque.addLast(SearchPos(y + 1, x - 1, score + 1))
        deque.addLast(SearchPos(y + 1, x + 1, score + 1))
        deque.addLast(SearchPos(y + 1, x + 2, score + 1))

        deque.addLast(SearchPos(y + 2, x - 2, score + 1))
        deque.addLast(SearchPos(y + 2, x - 1, score + 1))
        deque.addLast(SearchPos(y + 2, x, score + 1))
        deque.addLast(SearchPos(y + 2, x + 1, score + 1))
        deque.addLast(SearchPos(y + 2, x + 2, score + 1))

        return null
    }

    while (true) {
        val result = bfs() ?: continue
        c.println(result.toString())
        break
    }
}

data class SearchPos(val y: Int, val x: Int, val score: Int)

class Case(
    private val input: BufferedReader = System.`in`.bufferedReader(),
    private val out: BufferedWriter = System.out.bufferedWriter()
) {
    fun exec(fn: (Case) -> Unit) = input.use { out.use { execInThread(fn) } }
    private fun execInThread(fn: (Case) -> Unit) =
        Thread(null, { fn(this) }, "solve", 64 * 1024 * 1024)
            .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } }
            .apply { start() }.join()

    fun println(c: CharSequence?) = out.appendln(c)
    fun string(): String = input.readLine().trimEnd()
    fun long(): Long = input.readLine().toLong()
    fun intList(): List<Int> = input.readLine().split(" ").map { it.toInt() }
    fun longList(): List<Long> = input.readLine().split(" ").map { it.toLong() }
}