n = int(readline())
a = list(map(int, readline().split()))
max_l = max(a)
ans = 0
for i in range(n):
    if i == 0 and max_l:
        continue
    elif i < max_l:
        ans += 1
    else:
        ans = 0
print(ans)