@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}

@ary = [[-2,-2], [-2,-1], [-2,0], [-2,1], [-2,2], [-1,-2], [-1,-1], [-1,1], [-1,2], [0,-2], [0,2], [1,-2], [1,-1], [1,1], [1,2], [2,-2], [2,-1], [2,0], [2,1], [2,2]]

@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
    calc(x-1,y)
    calc(x+1,y)
    calc(x,y-1)
    calc(x,y+1)

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

q = {[ch-1, cw-1] => 0}
@v = {}
while !@d[dh-1][dw-1] && !q.empty?
    q.each{|(x,y),_| calc(x,y)}
    @i += 1
    q = @v
    @v = {}
end

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