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

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

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}
macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

fn main() {
    let mut sc = Scanner::new();
    let h: usize = sc.cin();
    let w: usize = sc.cin();
    let x0: usize = sc.cin::<usize>() - 1;
    let y0: usize = sc.cin::<usize>() - 1;
    let x1: usize = sc.cin::<usize>() - 1;
    let y1: usize = sc.cin::<usize>() - 1;

    let f: Vec<Vec<bool>> = (0..h)
        .map(|_| sc.chars().iter().map(|&c| c == '.').collect::<Vec<_>>())
        .collect();

    use std::cmp::Reverse;
    use std::collections::BinaryHeap;

    let mut q = BinaryHeap::new();
    q.push((Reverse(0), x0, y0));

    let inf = (h * w + 10) * 2;

    let mut memo = vec![vec![inf; w]; h];
    memo[x0][y0] = 0;

    let neigh = neighbor::Grid4(h, w);

    while let Some((_, x, y)) = q.pop() {
        // walk
        for (u, v) in neigh.iter(x, y) {
            if !f[u][v] {
                continue;
            }
            if memo[u][v] > memo[x][y] {
                memo[u][v] = memo[x][y];
                q.push((Reverse(memo[u][v]), u, v));
            }
        }
        // warp
        for i in 0..5 {
            for j in 0..5 {
                if x + i < 2 || y + j < 2 {
                    continue;
                }
                if x + i - 2 >= h || y + j - 2 >= w {
                    continue;
                }
                if i == 2 && j == 2 {
                    continue;
                }
                let (u, v) = (x + i - 2, y + j - 2);
                if !f[u][v] {
                    continue;
                }
                if memo[u][v] > memo[x][y] + 1 {
                    memo[u][v] = memo[x][y] + 1;
                    q.push((Reverse(memo[u][v]), u, v));
                }
            }
        }
    }

    // for i in 0..h {
    //     for j in 0..w {
    //         print!("{:02} ", memo[i][j]);
    //     }
    //     println!();
    // }

    if memo[x1][y1] == inf {
        put!(-1);
    } else {
        put!(memo[x1][y1]);
    }
}

macro_rules! list {
    ($($t:tt)*) => {{ let mut r = vec![]; list_inner!(r, $($t)*); r }}
}
macro_rules! list_inner {
    ($r:expr) => {};
    ($r:expr, $e:expr) => { $r.push($e) };
    ($r:expr, $e:expr;) => { $r.push($e) };
    ($r:expr, $e:expr; for $k:ident in $range:expr) => { for $k in $range { list_inner!($r, $e) } };
    ($r:expr, $e:expr; if $cond:expr) => { if $cond { list_inner!($r, $e) } };
    ($r:expr, $e:expr; for $k:ident in $range:expr ; $($t:tt)*) => { for $k in $range { list_inner!($r, $e ; $($t)*); } };
    ($r:expr, $e:expr; if $cond:expr ; $($t:tt)*) => { if $cond { list_inner!($r, $e ; $($t)*); } };
}

mod neighbor {
    pub struct Grid4(pub usize, pub usize);
    impl Grid4 {
        pub fn iter(&self, i: usize, j: usize) -> NeighborIter<(usize, usize)> {
            NeighborIter::new(list! {
                (i + s - 1, j + t - 1);
                for s in 0..3;
                for t in 0..3;
                if (s + t) % 2 == 1;
                if (1..self.0 + 1).contains(&(i + s));
                if (1..self.1 + 1).contains(&(j + t));
            })
        }
    }
    pub struct Grid8(pub usize, pub usize);
    impl Grid8 {
        pub fn iter<'a>(&'a self, i: usize, j: usize) -> NeighborIter<(usize, usize)> {
            NeighborIter::new(list! {
                (i + s - 1, j + t - 1);
                for s in 0..3;
                for t in 0..3;
                if s * t != 1;
                if (1..self.0 + 1).contains(&(i + s));
                if (1..self.1 + 1).contains(&(j + t));
            })
        }
    }
    pub struct NeighborIter<T>(Vec<T>, usize);
    impl<T> NeighborIter<T> {
        fn new(data: Vec<T>) -> Self {
            NeighborIter(data, 0)
        }
    }
    impl<T: Copy> Iterator for NeighborIter<T> {
        type Item = T;
        fn next(&mut self) -> Option<Self::Item> {
            if self.0.len() > self.1 {
                self.1 += 1;
                Some(self.0[self.1 - 1])
            } else {
                None
            }
        }
    }
}

use std::collections::VecDeque;
use std::io::{self, Write};
use std::str::FromStr;

struct Scanner {
    stdin: io::Stdin,
    buffer: VecDeque<String>,
}
impl Scanner {
    fn new() -> Self {
        Scanner {
            stdin: io::stdin(),
            buffer: VecDeque::new(),
        }
    }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
    fn chars(&mut self) -> Vec<char> {
        self.cin::<String>().chars().collect()
    }
    fn vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.cin()).collect()
    }
}