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: Wrong Answer)

#![allow(non_snake_case)]
#![allow(unused_imports)]
#![allow(dead_code)]
 
use proconio::{input, fastout};
use proconio::marker::*;
use whiteread::parse_line;
use std::collections::*;
use num::*;
use num_traits::*;
use superslice::*;
use std::ops::*;
 
use itertools::Itertools;
use itertools_num::ItertoolsNum;

struct Graph {
    graph: Vec<Vec<char>>,
    queue: VecDeque<(usize, usize)>,
    dist: Vec<Vec<isize>>,
}

impl Graph {
    fn new(graph: Vec<Vec<char>>) -> Self {
        let height = graph.len();
        let width = graph[0].len();
        Graph {
            graph: graph,
            queue: VecDeque::new(),
            dist: vec![vec![-1; width]; height]
        }
    }

    fn bfs(&mut self, start: (usize, usize)) -> usize {
        let dx = vec![0, 0, 1, -1];
        let dy = vec![1, -1, 0, 0];
        let height = self.graph.len();
        let width = self.graph[0].len();
        let mut loop_num = 0;

        self.queue.push_back(start);
        self.dist[start.1][start.0] = 0;

        loop {
            // println!("{:?}", self.queue);
            if self.queue.is_empty() {
                break;
            }

            let (c_x, c_y): (usize, usize) = self.queue.pop_front().unwrap();

            for i in 0..4 {
                let next_x = c_x as isize + dx[i];
                let next_y = c_y as isize + dy[i];
                
                if next_x < 0 || next_y < 0 || next_x >= width as isize || next_y >= height as isize { continue }
                let next_x = next_x as usize;
                let next_y = next_y as usize;
                // println!("nx: {} ny: {}", next_x, next_y);
                if self.graph[next_y][next_x] == '#' { continue }

                if self.dist[next_y][next_x] == -1 {
                    self.queue.push_back((next_x, next_y));
                    self.dist[next_y][next_x] = self.dist[c_y][c_x] + 1;
                }
            }

            if self.queue.is_empty() {
                let x_l_5 = if c_x < 2 { 0 } else { c_x - 2 };
                let x_h_5 = if c_x + 2 >= width { width - 1 } else { c_x + 2 };
                let y_l_5 = if c_y < 2 { 0 } else { c_y - 2 };
                let y_h_5 = if c_y + 2 >= height { height - 1 } else { c_y + 2 };
    
                for x in x_l_5..=x_h_5 {
                    for y in y_l_5..=y_h_5 {
                        if self.graph[y][x] == '#' { continue };
                        if self.dist[y][x] == -1 { 
                            self.queue.push_back((x, y));
                            self.dist[y][x] = self.dist[c_y][c_x] + 1;
                        }
                    }
                }
                if !self.queue.is_empty() {
                    loop_num += 1;
                }
            }
        }

        loop_num
    }

    fn print(&self) {
        for g in self.dist.iter() {
            println!("{:?}", g)
        }
    }
}


#[fastout]
fn solve() {
    const MOD: usize = 1_000_000_007;
    const INF: usize = std::usize::MAX;

    input!{
        h: usize, _w: usize,
        ch: usize, cw: usize, dh: usize, dw: usize,
        s_vec: [Chars; h],
    }

    let mut g = Graph::new(s_vec);
    // dbg!(&g.graph);
    let ans = g.bfs((cw-1, ch-1));
    
    // println!("dw: {} dh: {}", dw - 1, dh - 1);
    // g.print();

    if g.dist[dh-1][dw-1] != -1 { 
        println!("{}", ans)
    } else {
        println!("-1")
    }
}

fn main() {
    solve();
}