#![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()
    }
}


Measured memory_bytes: 11288