$H, $W = gets.chomp.split.map(&:to_i)
sy, sx = gets.chomp.split.map(&:to_i)
gy, gx = gets.chomp.split.map(&:to_i)
$maze = []
$H.times do
    $maze << gets.chomp.split("")
end
ans = 0
$queue = [[sx-1, sy-1]]
$reach = Array.new($H).map{Array.new($W, nil)}
$reach[sy-1][sx-1] = 0

$H.times do |h|
    $W.times do |w|
        $reach[h][w] = -1 if $maze[h][w] == "#"
    end
end

$tmp = [[sx-1, sy-1]]

def bfs(c)
    until $queue.empty?
        cx, cy = $queue.shift
        [
            [cx-1, cy],
            [cx+1, cy],
            [cx, cy-1],
            [cx, cy+1]
        ].each do |x, y|
            next if x < 0 || $W <= x || y < 0 || $H <= y || $reach[y][x]
            $queue << [x, y]
            $tmp << [x, y]
            $reach[y][x] = c
        end
    end
end

def warp(start_x, start_y, c)
    cx, cy = start_x, start_y
    _range = []
    (-2..2).each do |i|
        (-2..2).each do |j|
            _range << [cx+j, cy+i]
        end
    end
    _range.each do |x, y|
        next if x < 0 || $W <= x || y < 0 || $H <= y || $reach[y][x]
        $queue << [x, y]
        $reach[y][x] = c
    end
end

bfs(0)
cnt = 1

until $tmp.empty?
    until $tmp.empty?
        x, y = $tmp.shift
        warp(x, y, cnt)
    end
    bfs(cnt)
    cnt += 1
end

puts $reach[gy-1][gx-1] ? $reach[gy-1][gx-1] : -1