import sys

input = sys.stdin.readline



N = int(input().rstrip())

a = list(map(int, input().rstrip().split()))

# print(a)



if not 1 in a:

  print((-1))

  exit()



def check(n):

  for i in range(N):

    if a[-1-i] == n:

      n -= 1

  return n == 0



r = N

left = 1

right = N

for _ in range(N):

  if (right - left) <= 2:

    if check(right):

      r = right

      break

    elif check(right-1):

      r = right - 1

      break

    else:

      r = right - 2

      break

  t = (left + right) // 2

  if check(t):

    left = t

  else:

    right = t



print((N-r))
import sys

input = sys.stdin.readline



N = int(input().rstrip())

a = list(map(int, input().rstrip().split()))

# print(a)



if not 1 in a:

  print((-1))

  exit()



ans = 1

for i in range(N):

  if a[i] == ans:

    ans += 1



print((N-ans+1))

import sys

input = sys.stdin.readline
N = int(input().rstrip())
a = list(map(int, input().rstrip().split()))
# print(a)
if not 1 in a:
    print((-1))
    exit()


def check(n):
    for i in range(N):
        if a[-1 - i] == n:
            n -= 1
    return n == 0


r = N
left = 1
right = N
for _ in range(N):
    if (right - left) <= 2:
        if check(right):
            r = right
            break
        elif check(right - 1):
            r = right - 1
            break
        else:
            r = right - 2
            break
    t = (left + right) // 2
    if check(t):
        left = t
    else:
        right = t
print((N - r))

import sys

input = sys.stdin.readline
N = int(input().rstrip())
a = list(map(int, input().rstrip().split()))
# print(a)
if not 1 in a:
    print((-1))
    exit()
ans = 1
for i in range(N):
    if a[i] == ans:
        ans += 1
print((N - ans + 1))

- - -def check(n): -    for i in range(N): -        if a[-1 - i] == n: -            n -= 1 -    return n == 0 - - -r = N -left = 1 -right = N -for _ in range(N): -    if (right - left) <= 2: -        if check(right): -            r = right -            break -        elif check(right - 1): -            r = right - 1 -            break -        else: -            r = right - 2 -            break -    t = (left + right) // 2 -    if check(t): -        left = t -    else: -        right = t -print((N - r)) +ans = 1 +for i in range(N): +    if a[i] == ans: +        ans += 1 +print((N - ans + 1))