@h, @w = gets.split.map(&:to_i)
ch, cw = gets.split.map(&:to_i)
dh, dw = gets.split.map(&:to_i)
@s = @h.times.map {gets.chomp}

@d = Array.new(@h) {Array.new(@w)}
@i = 1

def calc(x,y)
    return if !x.between?(0, @h-1) || !y.between?(0,@w-1) || @s[x][y] == ?# || @d[x][y]

    @d[x][y] = @i
    (-2..2).each do |xx|
        next if !(x + xx).between?(0, @h-1)
        (-2..2).each do |yy|
            @v << [x+xx, y+yy] if (y + yy).between?(0, @w-1) && @s[x+xx][y+yy] == ?.
        end
    end

    calc(x-1,y)
    calc(x+1,y)
    calc(x,y-1)
    calc(x,y+1)
end

q = [[ch-1, cw-1]]
@v = []
while !@d[dh-1][dw-1] && !q.empty?
    q.each do |x,y|
        next if @d[x][y]
        calc(x,y)
    end
    @i += 1
    q = @v
    @v = []
end

p @d[dh-1][dw-1] ? @d[dh-1][dw-1] - 1 : -1