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: Accepted)

package main

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

const Inf = 1 << 60

func main() {
	H, W := ReadInt(), ReadInt()
	Ch, Cw := ReadInt()-1, ReadInt()-1
	Dh, Dw := ReadInt()-1, ReadInt()-1

	grid := make([]string, H)
	for y := 0; y < H; y++ {
		grid[y] = ReadString()
	}

	c := make([][]int, H)
	for y := 0; y < H; y++ {
		c[y] = make([]int, W)
		for x := 0; x < W; x++ {
			c[y][x] = Inf
		}
	}

	h := Heap{}
	h.push(State{X: Cw, Y: Ch, Cost: 0})

	for len(h) > 0 {
		now := h.pop()
		if grid[now.Y][now.X] == '#' {
			continue
		}
		if c[now.Y][now.X] <= now.Cost {
			continue
		}
		c[now.Y][now.X] = now.Cost
		for dx := -2; dx <= 2; dx++ {
			for dy := -2; dy <= 2; dy++ {
				if dx == 0 && dy == 0 {
					continue
				}
				nx := now.X + dx
				ny := now.Y + dy

				if 0 <= nx && nx < W && 0 <= ny && ny < H {
					if grid[ny][nx] == '#' {
						continue
					}
					nc := now.Cost + 1
					if dx*dy == 0 && Abs(dx+dy) == 1 {
						nc = now.Cost
					}
					if c[ny][nx] <= nc {
						continue
					}
					h.push(State{
						X:    nx,
						Y:    ny,
						Cost: nc,
					})
				}
			}
		}
	}

	ans := c[Dh][Dw]
	if ans == Inf {
		fmt.Println(-1)
	} else {
		fmt.Println(ans)
	}
}

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

// h := Heap{}
// h.push(State{Cost: 0, X: 0, Y: 0})
// state := h.pop()

type State struct {
	X, Y, Cost int
}

type HeapElement = State
type Heap []HeapElement

func (h Heap) Less(i, j int) bool    { return h[i].Cost < h[j].Cost }
func (h Heap) Len() int              { return len(h) }
func (h Heap) Swap(i, j int)         { h[i], h[j] = h[j], h[i] }
func (h *Heap) Push(x interface{})   { *h = append(*h, x.(HeapElement)) }
func (h *Heap) Pop() (x interface{}) { *h, x = (*h)[:len(*h)-1], (*h)[len(*h)-1]; return }
func (h *Heap) push(v HeapElement)   { heap.Push(h, v) }
func (h *Heap) pop() HeapElement     { return heap.Pop(h).(HeapElement) }

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadInt() (i int)       { Scan(&i); return }
func ReadString() (s string) { Scan(&s); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}