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 Rust (1.42.0) (judge verdict: Runtime Error)

#![allow(unused_macros)]
#![allow(dead_code)]
#![allow(unused_imports)]

use itertools::Itertools;
use proconio::*;
use smallvec::alloc::collections::VecDeque;
use text_io::*;

const U_INF: usize = 1 << 60;
const I_INF: isize = 1 << 60;

fn main() {
    input! {
        h:usize,
        w:usize,
        ch:usize,
        cw:usize,
        dh:usize,
        dw:usize,
        map:[String;h],
    }

    let ch = ch - 1;
    let cw = cw - 1;
    let dh = dh - 1;
    let dw = dw - 1;

    let map = map
        .into_iter()
        .map(|s| s.chars().collect_vec())
        .collect_vec();

    let mut queue = VecDeque::new();
    queue.push_back((ch, cw));

    let mut warp_queue = VecDeque::new();

    let mut dp = vec![vec![U_INF; w + 10]; h + 10];
    dp[ch][cw] = 0;
    let mut seen = vec![vec![false; w + 10]; h + 10];
    let mut seen_by_walk = vec![vec![false; w + 10]; h + 10];

    while !(queue.is_empty() && warp_queue.is_empty()) {
        if queue.is_empty() {
            queue.append(&mut warp_queue);
        }

        let (i, j) = queue.pop_front().unwrap();
        seen[i][j] = true;

        let next = next_by_walk(i, j, h, w);

        for (k, s) in next {
            if map[k][s] == '.' && !seen[k][s] {
                seen_by_walk[k][s] = true;
                queue.push_back((k, s));
                dp[k][s] = (dp[k][s]).min(dp[i][j]);
            }
        }

        let next = next_by_warp(i, j, h, w);

        for (k, s) in next {
            if map[k][s] == '.' && !seen[k][s] && !seen_by_walk[s][k] {
                warp_queue.push_back((k, s));
                dp[k][s] = (dp[k][s]).min(dp[i][j] + 1);
            }
        }
    }

    if dp[dh][dw] == U_INF {
        println!("{}", -1);
        return;
    }

    println!("{}", dp[dh][dw]);
}

fn next_by_walk(i: usize, j: usize, h: usize, w: usize) -> Vec<(usize, usize)> {
    let i = i as isize;
    let j = j as isize;

    vec![(i - 1, j), (i, j - 1), (i, j + 1), (i + 1, j)]
        .into_iter()
        .filter(|(i, j)| (0..h as isize).contains(i) && (0..w as isize).contains(j))
        .map(|(i, j)| (i as usize, j as usize))
        .collect_vec()
}

fn next_by_warp(i: usize, j: usize, h: usize, w: usize) -> Vec<(usize, usize)> {
    let i = i as isize;
    let j = j as isize;

    let mut vec = Vec::new();
    for a in 0..5 {
        for b in 0..5 {
            if a == 2 && b == 2 {
                continue;
            }

            vec.push((i + a - 2, j + b - 2));
        }
    }

    vec.into_iter()
        .filter(|(i, j)| (0..h as isize).contains(i) && (0..w as isize).contains(j))
        .map(|(i, j)| (i as usize, j as usize))
        .collect_vec()
}