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

import java.io.IOException
import java.lang.NumberFormatException
import java.lang.StringBuilder
import java.util.ArrayDeque
import kotlin.math.max
import kotlin.math.min

object DMain{
    fun solve(){
        val mmax = 5000000

        val h = nextInt()
        val w = nextInt()
        val map = Array<IntArray>(h + 4){ IntArray(w + 4){-1} }

        val ch = nextInt()
        val cw = nextInt()
        val dh = nextInt()
        val dw = nextInt()

        for (i in 0 until h){
            val s = next()
            for(j in 0 until w){
                if(s[j] == '.') map[i+2][j+2] = mmax
            }
        }

        map[ch+1][cw+1] = 0
        move(map, h, w, ch+1 to cw+1)

        if(map[dh+1][dw+1] == -1 || map[dh+1][dw+1] == mmax){
            println(-1)
        }else{
            println(map[dh+1][dw+1])
        }
    }

    fun move(map: Array<IntArray>, h: Int, w: Int, start: Pair<Int, Int>){
        val que = ArrayDeque<Pair<Int, Int>>()
        val move = Array<Pair<Int, Int>>(4){0 to 0}
        move[0] = 0 to 1
        move[1] = 0 to -1
        move[2] = 1 to 0
        move[3] = -1 to 0

        que.add(start)
        while(!que.isEmpty()){
            val next = que.poll()
            val num = map[next.first][next.second]
            var ismove = false

            for(i in 0 until 4){
                val mx = next.first + move[i].first
                val my = next.second + move[i].second
                if(map[mx][my] > num){
                    map[mx][my] = num
                    que.add(mx to my)
                    ismove = true
                }
            }

            if(ismove) continue
            for (mmx in -2..2){
                for(mmy in -2..2){
                    if(map[next.first+mmx][next.second+mmy] > num + 1){
                        map[next.first+mmx][next.second+mmy] = num + 1
                        que.add(next.first+mmx to next.second+mmy)
                    }
                }
            }
        }
    }





    // Scanner based on uwi-san's Java code
    val stream = System.`in`
    val buffer = ByteArray(1024)
    var ptr = 0
    var buflen = 0

    fun hasNextByte(): Boolean{
        if(ptr < buflen){
            return true
        }else{
            ptr = 0
            try{
                buflen = stream.read(buffer)
            }catch (e: IOException){
                e.printStackTrace()
            }
            if(buflen <= 0){
                return false
            }
        }
        return true
    }

    fun readByte() =
            if(hasNextByte()) buffer[ptr++]
            else -1

    fun isPrintableChar(c: Byte) = c in 33..126

    fun hasNext(): Boolean{
        while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++
        return hasNextByte()
    }

    fun next(): String{
        if(!hasNext()) throw NoSuchElementException()
        val sb = StringBuilder()
        var b = readByte()
        while(isPrintableChar(b)){
            sb.appendCodePoint(b.toInt())
            b = readByte()
        }
        return sb.toString()
    }

    fun nextLong(): Long{
        if(!hasNext()) throw NoSuchElementException()
        var n = 0L
        var minus = false
        var b = readByte()
        if(b == '-'.toByte()){
            minus = true
            b = readByte()
        }
        if(b !in '0'.toByte()..'9'.toByte())
            throw NumberFormatException()
        while(true){
            if(b in '0'.toByte()..'9'.toByte()){
                n *= 10
                n += b - '0'.toByte()
            }else if(!isPrintableChar(b)){
                return if(minus) -n else n
            }else{
                throw NumberFormatException()
            }
            b = readByte()
        }
    }

    fun nextInt(): Int{
        val n1 = nextLong()
        if(n1 !in Int.MIN_VALUE..Int.MAX_VALUE)
            throw NumberFormatException()
        return n1.toInt()
    }

    fun NextDouble() = next().toDouble()
}

fun main(){
    DMain.solve()
}