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