N = gets.to_i P = gets.split.map(&:to_i) idx = P.index(N) + 1 rounds = Hash.new P.each.with_index(1) do |x, i| rounds[x] = i end i = N sum = (1..N).sum while i >= 2 && 2 * rounds[i - 1] <= rounds[i] sum -= rounds[i] i -= 1 end puts [0, sum - 2 * rounds[i]].max