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

use proconio::input;

fn main() {
    input! {
        Y: usize,
        X: usize,
        _sy: usize,
        _sx: usize,
        _gy: usize,
        _gx: usize,
        _S: [String; Y],
    }
    let dir = [1i64, 0, -1, 0, 1];
    let sy = _sy - 1;
    let sx = _sx - 1;
    let gy = _gy - 1;
    let gx = _gx - 1;
    let mut S = vec![vec![false; X]; Y];
    for (y, row) in _S.iter().enumerate() {
        for (x, ch) in row.chars().enumerate() {
            S[y][x] = ch == '.';
        }
    }
    let S = S;
    let mut id_map = vec![vec![None; X]; Y];
    let mut islands = Vec::new();
    let mut queue = std::collections::VecDeque::new();
    for y in 0..Y {
        for x in 0..X {
            if !S[y][x] {
                continue;
            }
            match id_map[y][x] {
                Some(_) => continue,
                None => {
                    let id = islands.len() as i32;
                    let mut island = Vec::new();
                    queue.push_back((x, y));
                    loop {
                        match queue.pop_front() {
                            None => break,
                            Some((x, y)) => match id_map[y][x] {
                                Some(_) => continue,
                                None => {
                                    id_map[y][x] = Some(id);
                                    island.push((x, y));
                                    for i in 0..4 {
                                        let x = x as i64 + dir[i];
                                        let y = y as i64 + dir[i + 1];
                                        if 0 <= x
                                            && x < X as i64
                                            && 0 <= y
                                            && y < Y as i64
                                            && S[y as usize][x as usize]
                                        {
                                            queue.push_back((x as usize, y as usize));
                                        }
                                    }
                                }
                            },
                        }
                    }
                    islands.push(island);
                }
            }
        }
    }
    let id_map = id_map;
    let s_id = id_map[sy][sx].unwrap() as usize;
    let g_id = id_map[gy][gx].unwrap() as usize;
    let max_id = islands.len();
    let mut queue = std::collections::VecDeque::new();
    let mut distance = vec![None; max_id];
    queue.push_back((s_id as usize, 0));
    loop {
        match queue.pop_front() {
            None => break,
            Some((id, d)) => {
                if match distance[id] {
                    None => true,
                    Some(_d) => d < _d,
                } {
                    distance[id] = Some(d);
                    if id == g_id {
                        break;
                    }
                    for (x, y) in &islands[id] {
                        let (x, y): (usize, usize) = (*x, *y);
                        for dx in -2..=2 {
                            for dy in -2..=2 {
                                let x = x as i64 + dx;
                                let y = y as i64 + dy;
                                if 0 <= x
                                    && x < X as i64
                                    && 0 <= y
                                    && y < Y as i64
                                    && S[y as usize][x as usize]
                                {
                                    let next_id = id_map[y as usize][x as usize].unwrap() as usize;
                                    if next_id != id {
                                        queue.push_back((next_id, d + 1));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    println!(
        "{}",
        match distance[g_id] {
            None => -1,
            Some(v) => v,
        }
    );
}


Measured memory_bytes: 27544