N = gets.to_i Goal = ([0]*N).map{ gets.to_i } greeting_count = 0 for i in 0...Goal.size do myPos = i + 1 myGoal = Goal[i] for j in (i+1)...Goal.size do otherPos = j + 1 if (myPos <= Goal[j] && Goal[j] <= myGoal) || (Goal[j] <= myGoal && myGoal <= otherPos) then #puts "Greet myPos: #{myPos}, otherPos: #{otherPos}, Goal[#{j}]: #{Goal[j]}, myGoal: #{myGoal}" greeting_count += 1 else #puts "not Greet myPos: #{myPos}, otherPos: #{otherPos}, Goal[#{j}]: #{Goal[j]}, myGoal: #{myGoal}" end end end puts greeting_count