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

// -*- coding:utf-8-unix -*-

#[macro_use]
extern crate lazy_static;
extern crate num_bigint; // 0.2.2
extern crate num_traits; // 0.2.8
use num_bigint::BigInt;
use num_traits::Pow;

// use proconio::derive_readable;
use proconio::fastout;
use proconio::input;
use proconio::marker::Chars;
// use std::convert::TryInto;
use libm::*;
use std::cmp::*;
use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque};
use std::io::*;
use std::ops::Range;
use std::str::FromStr;
use superslice::*;

use lazy_static::lazy_static;
use std::sync::Mutex;

pub fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

lazy_static! {
    static ref H: Mutex<Vec<i32>> = Mutex::default();
    static ref W: Mutex<Vec<i32>> = Mutex::default();
}

#[derive(Copy, Clone, Eq, PartialEq, Debug)]
struct State {
    cost: usize,
    pos: (usize, usize),
}

// The priority queue depends on `Ord`.
// Explicitly implement the trait so the queue becomes a min-heap
// instead of a max-heap.
impl Ord for State {
    fn cmp(&self, other: &State) -> Ordering {
        // Notice that the we flip the ordering on costs.
        // In case of a tie we compare positions - this step is necessary
        // to make implementations of `PartialEq` and `Ord` consistent.
        other.cost.cmp(&self.cost)
        // .reverse()
    }
}

// `PartialOrd` needs to be implemented as well.
impl PartialOrd for State {
    fn partial_cmp(&self, other: &State) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

//abc176-D
// #[fastout]
fn main() {
    input![
        h: usize,
        w: usize,
        ch: usize,
        cw: usize,
        dH: usize,
        dW: usize,
        s: [Chars; h]
    ];

    let ch = ch - 1;
    let cw = cw - 1;
    let dH = dH - 1;
    let dW = dW - 1;

    let mut dp = vec![vec![None; w]; h];
    // dp[ch][cw] = Some(0);

    let mut magic_q = BinaryHeap::new();
    magic_q.push(State {
        cost: 0,
        pos: (ch, cw),
    });

    while dp[dH][dW] == None && !magic_q.is_empty() {
        let min_state = magic_q.pop().unwrap();
        eprintln!("{:?}", min_state);

        if dp[min_state.pos.0][min_state.pos.1].is_some() {
            continue;
        }

        let mut q = VecDeque::new();
        q.push_back(min_state);

        while !dp[dH][dW].is_some() && !q.is_empty() {
            let next = q.pop_front().unwrap();

            let (n_h, n_w) = next.pos;

            if dp[n_h][n_w].is_some() {
                continue;
            }
            eprintln!("next: {:?}", next);

            dp[n_h][n_w] = Some(next.cost);

            let walks = [(-1, 0), (1, 0), (0, -1), (0, 1)];
            for (del_h, del_w) in walks.iter() {
                let new_h = n_h as i64 + del_h;
                let new_w = n_w as i64 + del_w;

                if new_h < 0 || new_w < 0 {
                    continue;
                }

                if new_h >= h as i64 || new_w >= w as i64 {
                    continue;
                }

                if s[new_h as usize][new_w as usize] == '#' {
                    continue;
                }

                if dp[new_h as usize][new_w as usize].is_some() {
                    continue;
                }

                q.push_back(State {
                    cost: next.cost,
                    pos: (new_h as usize, new_w as usize),
                });
            }

            let magics = [
                (-2, -2),
                (-2, -1),
                (-2, 0),
                (-2, 1),
                (-2, 2),
                //
                (-1, -2),
                (-1, -1),
                (-1, 1),
                (-1, 2),
                //
                (0, -2),
                (0, 2),
                //
                (1, -2),
                (1, -1),
                (1, 1),
                (1, 2),
                //
                (2, -2),
                (2, -1),
                (2, 0),
                (2, 1),
                (2, 2),
            ];

            for (del_h, del_w) in magics.iter() {
                let new_h = n_h as i64 + del_h;
                let new_w = n_w as i64 + del_w;

                if new_h < 0 || new_w < 0 {
                    continue;
                }

                if new_h >= h as i64 || new_w >= w as i64 {
                    continue;
                }

                if dp[new_h as usize][new_w as usize].is_some() {
                    continue;
                }

                if s[new_h as usize][new_w as usize] == '#' {
                    continue;
                }

                magic_q.push(State {
                    cost: next.cost + 1,
                    pos: (new_h as usize, new_w as usize),
                });
            }
        }
    }

    // .....
    // #####
    // ####.
    // .####
    // ..#.#
    // println!("{:?}", s);

    let a = dp[dH][dW];
    let ans = if a.is_some() { a.unwrap() as i64 } else { -1 };

    println!("{}", ans);
}

// let mut values = VALUES.lock().unwrap();
// values.extend_from_slice(&[1, 2, 3, 4]);
// assert_eq!(&*values, &[1, 2, 3, 4]);

// -100000000
//  1000000000