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 Go (1.14.1) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	scan_init()
	s := strings.Split(readLine(), "")
	maxIndex := len(s) //次に入れるindex
	q,_ := strconv.Atoi(readLine())
	leftS := []string{}
	rightS := []string{}
	//k := 0
	for i:= (maxIndex/2)-1; i>=0; i-- {
		// k++
		leftS = append(leftS, s[i])
	}
	// k = 0
	for i:= (maxIndex/2); i<maxIndex; i++ {
		rightS = append(rightS, s[i])
	}
	change := false //反転がきたらこれをon
	// scan_init()
	for i:= 0; i<q; i++ {
		get := scanInt()
		if get == 1 {
			change = !change
		} else {
			f := scanInt()
			c := scan()
			if f == 1 && !change || f == 2 && change {
				// 先頭
				leftS = append(leftS, c)
			} else {
				rightS = append(rightS,c)
			}
		}
		// fmt.Println(get, s, change)
	}
	// output := make([]string, len(leftS)+len(rightS))
	if !change {
		k := 0
		for i:= len(leftS)-1;i>=0;i-- {
			// output[k] = leftS[i]
			fmt.Print(leftS[i])
			k++
		}
		for i:= 0; i<len(rightS); i++ {
			// output[k] = rightS[i]
			fmt.Print(rightS[i])
			k++
		}
	} else {
		k := 0
		for i:= len(rightS)-1;i>=0;i-- {
			// output[k] = rightS[i]
			fmt.Print(rightS[i])
			k++
		}
		for i:= 0; i<len(leftS); i++ {
			// output[k] = leftS[i]
			fmt.Print(leftS[i])
			k++
		}
	}
	// fmt.Println(strings.Join(output,""))
}

var sc = bufio.NewScanner(os.Stdin)

func scan_init() {
	sc.Split(bufio.ScanWords)
}
func scanInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}
func scan() string {
	sc.Scan()
	return sc.Text()
}

var rdr = bufio.NewReaderSize(os.Stdin, 200000)

func readLine() string {
	buf := make([]byte, 0, 200000)
	for {
		l, p, e := rdr.ReadLine()
		if e != nil {
			panic(e)
		}
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}
func readLineToNumber() int {
	S := readLine()
	number, _ := strconv.Atoi(S)
	return number
}
func readLineToSlice() []string {
	S := readLine()
	list := strings.Split(S, " ")
	return list
}
func readLineToNumberSlice(memo map[string]int) []int {
	// err時は-1を代入
	S := readLine()
	intList := make([]int, len(S))
	for i := 0; i < len(S); i++ {
		if val, ok := memo[string(S[i])]; ok {
			intList[i] = val
		} else {
			intList[i] = -1
		}
	}
	return intList
}