INF = 1 << 40
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
queue = [[sx-1, sy-1]]
reach = Array.new(H).map{Array.new(W, INF)}
reach[sy-1][sx-1] = 0
vis = Array.new(H).map{Array.new(W, false)}
until queue.empty?
    cx, cy = queue.shift
    next if vis[cy][cx]
    vis[cy][cx] = true
    break if cx == gx && cy == gy
    [
        [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
        next if maze[y][x] == "#"
        if reach[cy][cx] < reach[y][x] then
            queue.unshift([x, y])
            reach[y][x] = reach[cy][cx]
        end
    end
    (-2..2).each do |i|
        (-2..2).each do |j|
            x, y = cx+j, cy+i
            next if x < 0 || W <= x || y < 0 || H <= y
            next if maze[y][x] == "#"
            if reach[cy][cx] + 1 < reach[y][x] then
                queue << [x, y]
                reach[y][x] = reach[cy][cx] + 1
            end
        end
    end
end

puts reach[gy-1][gx-1] != INF ? reach[gy-1][gx-1] : -1