N = int(input()); a = [int(i) for i in input().split()] maxIndex = a.index(N) j = 1 while maxIndex - j >= 0 and a[maxIndex - j] == N - j: j += 1 k = max(maxIndex - j - 1,0) print (N - maxIndex - 1 + k)