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: Time Limit Exceeded)

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
)

const inf = 1000000000

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

type PriorityQueueItem struct {
	value    int
	priority int
}

type PrioirtyQueue struct {
	items       []PriorityQueueItem
	value2Index map[int]int
}

func newPriorityQueue() *PrioirtyQueue {
	q := new(PrioirtyQueue)
	q.value2Index = make(map[int]int, 0)
	return q
}

func (q *PrioirtyQueue) Len() int {
	return len(q.items)
}
func (q *PrioirtyQueue) Less(i, j int) bool {
	return q.items[i].priority < q.items[j].priority
}
func (q *PrioirtyQueue) Swap(i, j int) {
	q.items[i], q.items[j] = q.items[j], q.items[i]
	q.value2Index[q.items[i].value] = i
	q.value2Index[q.items[j].value] = j
}
func (q *PrioirtyQueue) Push(x interface{}) {
	q.items = append(q.items, x.(PriorityQueueItem))
	q.value2Index[x.(PriorityQueueItem).value] = len(q.items) - 1
}
func (q *PrioirtyQueue) Pop() interface{} {
	x := q.items[len(q.items)-1]
	q.items = q.items[0 : len(q.items)-1]
	delete(q.value2Index, x.value)
	return x
}
func (q *PrioirtyQueue) update(x PriorityQueueItem) {
	q.items[q.value2Index[x.value]] = x
	heap.Fix(q, q.value2Index[x.value])
}

type AdjacencyList struct {
	node int
	cost int
	next *AdjacencyList
}

func dijkstra(als []*AdjacencyList, s int) []int {
	ds := make([]int, len(als))
	q := newPriorityQueue()
	for v := 0; v < len(als); v++ {
		if v == s {
			ds[v] = 0
		} else {
			ds[v] = inf
		}
		q.Push(PriorityQueueItem{v, ds[v]})
	}
	heap.Init(q)
	for q.Len() != 0 {
		u := heap.Pop(q).(PriorityQueueItem).value
		for al := als[u]; al != nil; al = al.next {
			if ds[u]+al.cost < ds[al.node] {
				ds[al.node] = ds[u] + al.cost
				q.update(PriorityQueueItem{al.node, ds[al.node]})
			}
		}
	}
	return ds
}

func main() {
	var H, W int
	fmt.Scanf("%d%d", &H, &W)
	var Ch, Cw int
	fmt.Scanf("%d%d", &Ch, &Cw)
	Ch--
	Cw--
	var Dh, Dw int
	fmt.Scanf("%d%d", &Dh, &Dw)
	Dh--
	Dw--
	reader := bufio.NewReader(os.Stdin)
	bytes, _ := reader.ReadBytes(0)
	bytesIndex := 0
	S := make([][]byte, H)
	for i := 0; i < H; i++ {
		S[i] = make([]byte, W)
		for j := 0; j < W; j++ {
			S[i][j] = bytes[bytesIndex]
			bytesIndex++
		}
		bytesIndex++
	}
	als := make([]*AdjacencyList, H*W)
	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			if S[i][j] == '#' {
				continue
			}
			for k := -2; k <= 2; k++ {
				for l := -2; l <= 2; l++ {
					if i+k < 0 || i+k >= H || j+l < 0 || j+l >= W || (k == 0 && l == 0) || S[i+k][j+l] == '#' {
						continue
					}
					al := new(AdjacencyList)
					al.node = (i+k)*W + j + l
					if abs(k)+abs(l) == 1 {
						al.cost = 0
					} else {
						al.cost = 1
					}
					al.next = als[i*W+j]
					als[i*W+j] = al
				}
			}
		}
	}
	s := Ch*W + Cw
	g := Dh*W + Dw
	dsg := dijkstra(als, s)[g]
	if dsg == inf {
		fmt.Printf("-1\n")
	} else {
		fmt.Printf("%d\n", dsg)
	}
	return
}