#![allow(clippy::needless_range_loop)]
#![allow(unused_macros)]
#![allow(dead_code)]
#![allow(unused_imports)]

use itertools::*;
use proconio::input;
use proconio::marker::*;
use std::cmp::Reverse;

fn color(c: &mut[Vec<usize>], s: &[Vec<u8>], x: isize, y: isize, w: isize, h: isize, col: usize) {
    if x < 0 || y < 0 || x >= w || y >= h || s[y as usize][x as usize] == b'#' || c[y as usize][x as usize] != 1234567 {
        return;
    }
    c[y as usize][x as usize] = col;
    color(c, s, x - 1, y, w, h, col);
    color(c, s, x + 1, y, w, h, col);
    color(c, s, x, y - 1, w, h, col);
    color(c, s, x, y + 1, w, h, col);
}

use std::cmp::*;
use std::collections::binary_heap::BinaryHeap;
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct GraphEdge {
    node_to: usize,
    cost: isize,
}
impl Ord for GraphEdge {
    fn cmp(&self, other: &GraphEdge) -> Ordering {
        other.cost.cmp(&self.cost)
    }
}
impl PartialOrd for GraphEdge {
    fn partial_cmp(&self, other: &GraphEdge) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl GraphEdge {
    pub fn new(node_to: usize, cost: isize) -> Self {
        Self { node_to, cost }
    }
}
pub struct Graph {
    pub edges: Vec<Vec<GraphEdge>>,
    pub nodes: usize,
}
impl Graph {
    pub fn new(nodes: usize) -> Self {
        Self {
            edges: vec![vec![]; nodes],
            nodes,
        }
    }
    pub fn edge_undirected(&mut self, node_a: usize, node_b: usize) {
        self.edges[node_a].push(GraphEdge::new(node_b, 1));
        self.edges[node_b].push(GraphEdge::new(node_a, 1));
    }
    pub fn edge_directed(&mut self, node_from: usize, node_to: usize) {
        self.edges[node_from].push(GraphEdge::new(node_to, 1));
    }
    pub fn edge_undirected_costed(&mut self, node_a: usize, node_b: usize, cost: isize) {
        self.edges[node_a].push(GraphEdge::new(node_b, cost));
        self.edges[node_b].push(GraphEdge::new(node_a, cost));
    }
    pub fn edge_directed_costed(&mut self, node_from: usize, node_to: usize, cost: isize) {
        self.edges[node_from].push(GraphEdge::new(node_to, cost));
    }
    pub fn dijkstra(&self, node_from: usize) -> Vec<isize> {
        let mut costs = vec![std::isize::MAX; self.nodes];
        let mut heap: BinaryHeap<GraphEdge> = BinaryHeap::new();
        heap.push(GraphEdge::new(node_from, 0));
        while let Some(GraphEdge { cost, node_to }) = heap.pop() {
            if cost >= costs[node_to] {
                continue;
            }
            costs[node_to] = cost;
            for edge in &self.edges[node_to] {
                let next = GraphEdge::new(edge.node_to, cost + edge.cost);
                heap.push(next);
            }
        }
        costs
    }
    pub fn warshall_floyd(&self) -> Vec<Vec<isize>> {
        let mut costs = vec![vec![std::isize::MAX; self.nodes]; self.nodes];
        for i in 0..self.nodes {
            costs[i][i] = 0;
        }
        for (node_from, edges) in self.edges.iter().enumerate() {
            for GraphEdge { node_to, cost } in edges.iter() {
                costs[node_from][*node_to] = *cost;
            }
        }
        for k in 0..self.nodes {
            for i in 0..self.nodes {
                for j in 0..self.nodes {
                    costs[i][j] =
                        std::cmp::min(costs[i][j], costs[i][k].saturating_add(costs[k][j]));
                }
            }
        }
        costs
    }
}

fn main() {
    input! {
        h: usize,
        w: usize,
        c: (Usize1, Usize1),
        d: (Usize1, Usize1),
        s: [Bytes; h]
    }

    let mut col = 0;
    let mut colored = vec![vec![1234567; w]; h];
    for (x, y) in iproduct!(0..w, 0..h) {
        if colored[y][x] == 1234567 && s[y][x] == b'.' {
            color(&mut colored, &s, x as isize, y as isize, w as isize, h as isize, col);
            col += 1;
        }
    }
    use std::collections::HashSet;
    use std::cmp::{min, max};
    let mut edges: HashSet<(usize, usize)> = HashSet::new();
    for (x, y) in iproduct!(0..w, 0..h) {
        for (tx, ty) in iproduct!(-2..=2, -2..=2) {
            let tx = x as isize + tx;
            let ty = y as isize + ty;
            if tx < 0 || tx >= w as isize || ty < 0 || ty >= h as isize {
                continue;
            }

            let c = colored[y][x];
            let tc = colored[ty as usize][tx as usize];
            if tc != c && tc != 1234567 && c != 1234567 {
                edges.insert((tc, c));
                edges.insert((c, tc));
                if tc == 0 && c == 0 {
                    eprintln!("{}, {}, {}, {}", x, y, tx, ty);
                }
            }
        }
    }

    let mut graph = Graph::new(col);
    for &(a, b) in edges.iter() {
        graph.edge_directed_costed(a, b, 1);
    }

    let from = colored[c.0][c.1];
    let to = colored[d.0][d.1];
    let costs = graph.dijkstra(from);
    let ans = if costs[to] == std::isize::MAX {
        -1
    } else {
        costs[to]
    };

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


Measured memory_bytes: 34720