gets

p_a = gets.split.map(&:to_i)

target = p_a.select.with_index(1) do |p, i|
  p == i
end

count = 0
target.map { |p|  p_a.index(p) }.sort.each_slice(2) do |from, to|
  count += to - from
end

puts count
