N = int(input()) l = list(map(int, input().split())) l.reverse() sum = 0 while len(l) > 0: for i in range(len(l)): if l[i] == N: sum += i N -= 1 l = l[i + 1:] break else: sum += len(l) break print(sum)