Problem:
 Score : 400 points    Problem Statement We have N bricks arranged in a row from left to right.  The i -th brick from the left (1 \leq i \leq N) has an integer a_i written on it.  Among them, you can break at most N-1 bricks of your choice.  Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i  (1 \leq i \leq K) , the i -th of those brick from the left has the integer i written on it.  Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.      Constraints  All values in input are integers.  1 \leq N \leq 200000  1 \leq a_i \leq N         Input Input is given from Standard Input in the following format:  N  a_1  a_2  ...  a_N       Output Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.        Sample Input 1 3
2 1 2      Sample Output 1 1  If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.       Sample Input 2 3
2 2 2      Sample Output 2 -1  In this case, there is no way to break some of the bricks to satisfy Snuke's desire.       Sample Input 3 10
3 1 4 1 5 9 2 6 5 3      Sample Output 3 7       Sample Input 4 1
1      Sample Output 4 0  There may be no need to break the bricks at all.

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n):
    if i-ans!=a[i]-1:
        ans+=1
if ans==n:
    print(-1)
else:
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
num = 1
count = 0
for i in range(N):
    if A[i] == num:
        count += 1
        num += 1
if count == 0:
    print(-1)
else:
    print(N-count)

Solution (Python):
x = int(input())
li = list(map(int, input().split()))
k = 1
s = 0
for i in range(len(li)):
    if k == li[i] :
        k += 1
        s += 1
    if i == len(li)-1 and k == 1:
        s = len(li) + 1
print(len(li)-s)

Solution (Python):
x = int(input())
li = list(map(int, input().split()))
k = 1
s = 0
for i in range(len(li)):
    if k == li[i] :
        k += 1
        s += 1
    if i == len(li)-1 and k == 1:
        s = len(li) + 1
print(len(li)-s)

Solution (Python):
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())

def inpl_strl(): return list(sys.stdin.readline())

n = inp()
s = inpl()
b = 1
if s.count(1) == 0:
    print(-1)
else:
    for i in range(n):
        if s[i] == b:
            b += 1
    print(n-(b-1))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
cnt = 0
if not 1 in a:
    print(-1)
else:
    tmp = 1
    for i in range(N):
        if a[i] == tmp:
            tmp += 1
        else:
            cnt += 1
    print(cnt)

Solution (Python):
import math
import fractions

def input1():
	return int(input())


def input2():
	return map(int,input().split())


def input_array():
	return list(map(int,input().split()))




N=input1()
a=input_array()
num=1
broken=[]
flag=False
for i in range(N):
	if a[i]!=num:
		broken.append(a[i])
	else:
		num+=1
		flag=True

if flag:
	print(len(broken))
else:
	print(-1)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
a = LIST()
cnt = 0
c = 1

for i in range(N):
  if a[i] != c:
    cnt += 1
  else:
    c += 1
    
print(cnt if cnt < N else -1)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    cunt=0
    for a in A:
        if a==cunt+1:cunt+=1
    if 1 in A:
        if n==1 and A[0]==1:print(0)
        else:print(n-cunt)
    else:
        print(-1)
        

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    cunt=0
    for a in A:
        if a==cunt+1:cunt+=1
    if 1 in A:
        if n==1 and A[0]==1:print(0)
        else:print(n-cunt)
    else:
        print(-1)
        

if __name__=='__main__':
    main()

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

is_found = False
current = 1
num_of_broken_bricks = 0
for ai in a:
    if ai == current:
        is_found = True
        current += 1
    else:
        num_of_broken_bricks += 1

if not is_found:
    print(-1)
    exit()

print(num_of_broken_bricks)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in a:
    if i == (ans + 1):
        ans += 1
if ans:
    print(N - ans)
else:
    print(-1)

Solution (Python):
n = int(input())
a =  list(map(int, input().split()))

x = 1
for i in a:
    if i == x:
        x += 1

print(n - x + 1 if x > 1 else -1)

Solution (Python):
n = int(input())
a_li = list(map(int,input().split()))

check = 1
ans = 0
for x in a_li:
    if x == check:
        check+=1
    else:
        ans+=1
        
if check==1:
    print(-1)
else:
    print(ans)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
cnt=1
ans=0
for i in range(N):
    if A[i]==cnt:
        cnt+=1
    else:
        ans+=1
        
if A.count(1)==0:
    print(-1)
else:
    print(ans)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
checker = 1
ans = 0

for a in L:
    if a != checker:
        ans += 1
    else:
        checker += 1

if ans == N:
    print(-1)
else:
    print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]

target = 1
count = 0
for i in range(n):
    if a[i] != target:
        count += 1
    elif a[i] == target:
        target += 1

if target == 1:
    print(-1)
else:
    print(count)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
b = 1
if l.count(1)==0:
  print('-1')
else:
  for i in range(n):
    if l[i] ==b:
      b +=1
  print(n-(b-1))

Solution (Python):
N=int(input())
a=list(map(int,input().strip().split()))

i=0
j=1
exe=False
for n in range(N):
    if a[n]!=j:
        i+=1
            
    else:
        exe=True
        j+=1

if exe==False:
    print(-1)
else:
    print(i)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    A=list(map(int,input().split()))
    flag=False
    cnt=1
    for i in range(N):
        if A[i]==cnt:
            cnt+=1
            flag=True
        else:
            continue
    if not flag:
        print(-1)
    else:
        print(N-cnt+1)





resolve()

Solution (Python):
def main():
    N = int(input())
    a = list(map(int, input().split()))
    next = 1
    b = 0
    for i in range(N):
        if a[i] == next:
            b += 1
            next += 1
    if b == 0:
        print(-1)
    else:
        print(N - b)
main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

cur = 1
cnt = 0
for a in A:
    if (a == cur):
        cur += 1
    else:
        cnt += 1

if cnt == N:
    print(-1)
else:
    print(cnt)

Solution (Python):
from collections import Counter

n = int(input())

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

next = 1
for i in range(n):
  if a[i] != next:
    a[i] = 0
  else:
    next += 1

ans = Counter(a)[0]

print(ans if n != ans else -1)

Solution (Python):
n = int(input())
A = [int(x) for x in input().split()]
k = 1
for a in A:
  if a == k:
    k += 1
if k == 1:
  print(-1)
else:
  print(n - k + 1)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]

nex = 1
ans = []

for i in range(len(a)):
  if nex == a[i]:
    ans.append(a[i])
    nex = (nex + 1)

if len(ans) == 0:
  print(-1)
else:
  print(len(a) - len(ans))

Solution (Python):
N = int(input())
A = list(map(int, input().split())) 


C=1
D=0
for i in range(N):
  if A[i]==C:
    C=C+1
  else:
    D=D+1
if D==N:
  D=-1

print(D)

Solution (Python):
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    l = [int(e) for e in si().split()]

    bn = 1
    count = 0
    for i in range(n):
        if l[i] == bn:
            bn += 1
        else:
            count += 1

    print(count if count != n else -1)


if __name__ == '__main__':
    main()

Solution (Python):




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


wkidx = 0
wk_i = -1


for i in range(1, n+1):
    foundflg = False
    for j in range(wkidx, n):
        if a[j] == i:
            wkidx = j + 1
            foundflg = True
            break
    
    if foundflg == False:
        wk_i = i
        break

if wk_i == -1:
    
    ans = 0
elif wk_i == 1:
    
    ans = -1
else:
    
    ans = n - (wk_i - 1)


print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
find_num = 1
break_num = 0
for i in range(n):
    if a[i] == find_num:
        find_num += 1
    else:
        break_num += 1
if break_num == n:
    print(-1)
else:
    print(break_num)

Solution (Python):
n = int(input())
alis = list(map(int, input().split()))
ans = 0
m = 1
for i in alis:
    if i == m:
        m += 1
    else:
        ans += 1
print(ans if ans != n else -1)

Solution (Python):
"""
int(input())
map(int,input().split())
list(map(int,input().split()))

"""

n=int(input())
a=list(map(int,input().split()))
i=1
for j in range(len(a)):
    if(a[j]==i):
        i+=1
if(1 not in a):
    print(-1)
else:
    print(n-i+1)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

a = 0
for i in range(N):
  if A[i] == a+1:
    a += 1

if a == 0:
  print(-1)
else:
  print(N-a)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]

a = 0
for i in range(N):
  if A[i] == a+1:
    a += 1

if a == 0:
  print(-1)
else:
  print(N-a)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
ans = 0
if 1 in A:
    num = A.index(1)
    ans += num
    A = A[num:]
else:
    print(-1)
    exit()
f = 0

for i in range(2,n+1):
    try:
        d = A.index(i,f)
    except ValueError:
        ans += len(A[f+1:])
        print(ans)
        exit()
    ans += d - f - 1
    f = d
print(ans)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
ans = 0
if 1 in A:
    num = A.index(1)
    ans += num
    A = A[num:]
else:
    print(-1)
    exit()
f = 0

for i in range(2,n+1):
    try:
        d = A.index(i,f)
    except AttributeError:
        ans += len(A[f+1:])
        print(ans)
        exit()
    except ValueError:
        ans += len(A[f+1:])
        print(ans)
        exit()
    ans += d - f - 1
    f = d
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
target = 1
count = 0
for i in range(N):
  if A[i] == target:
    target += 1
  else:
    count += 1
print(count if count != len(A) else -1)

Solution (Python):


N = int(input())
A = list(int(x) for x in input().split())

ans = 0
idx = 1

for a in A:
    if a==idx:
        idx += 1
    else:
        ans += 1

if ans==N:
    ans = -1

print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

now = 1
for ai in a:
    if ai == now:
        now += 1

if now == 1:
    print(-1)
else:
    print(N - now + 1)

Solution (Python):
import bisect
N = int(input())
A = list(map(int, input().split()))

dp = [float('inf') for _ in range(N)]

for i in range(N):
    a = A[i]
    idx = bisect.bisect_left(dp, a)
    dp[idx] = a

s = [i for i, d in enumerate(dp) if i + 1 == d]
if len(s) == 0:
    print(-1)
else:
    print(N - 1 - max(s))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
cnt = 1
for i in a:
  if i == cnt:
    cnt += 1
print(n + 1 - cnt if cnt > 1 else -1)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
counter=0
n=1
for a in A:
    if(a == n):
        n+=1
    else:
        counter+=1

print(counter) if(n != 1) else print(-1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

now = 1
for i in a:
    if(i == now):
        now += 1

if(now == 1):
    print(-1)
else:
    print(n-(now-1))

Solution (Python):
N = int(input())
As = list(map(int, input().split()))

count = 1
for a in As:
    if count == a:
        count += 1

res = N - count + 1
if res == N:
    res = -1
print(res)

Solution (Python):
N = int(input())
A = input().split()
A = [int(x) for x in A]

k = 1
c = 0
for i in range(N):
  if A[i] == k:
    k +=1
  else:
    c +=1
if c == N:
  print(-1)
else:
  print(c)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))


number=1
B=[]
for i in range(N):
    if A[i]==number:
        number+=1
        B.append(A[i])



if len(B)==0:
    print(-1)
else:
    print(N-len(B))

Solution (Python):
N = int(input())
alist = list(map(int, input().split()))

search = -1
for a in alist:
  if a == search + 2:
    search = search + 1

ans = (len(alist) - search - 1) if search >= 0 else -1
print(ans)

Solution (Python):
N=int(input())
lis=list(map(int,input().split()))

check=1
for i in lis:
    if i==check:
        check+=1
    
if check!=1: 
    print(len(lis)-(check)+1)
else:
    print(-1)

Solution (Python):
n=int(input())
*a,=map(int,input().split())
t=1
for i in a:
	if i==t:
		t+=1
print(n-t+1 if t>1 else -1)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
broken = 0

for n in range(N):
    if a[n] != broken + 1:
        continue
    broken += 1

if broken == 0:
    print(-1)
else:
    print(N - broken)

Solution (Python):
n=int(input())

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

d=1
flag=False

cnt=0

for i in range(n):
  if a[i]==d:
    flag=True
    cnt+=1
    d+=1

if not flag:
  print("-1")
else:
  print(n-cnt)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
L = []
n = 0
a = 0
for i in range(1,N+1):
    if n >= N:
        break
    elif a == N-1:
        break
    else:
        for j in range(n,N):
            if A[j] == i:
                L.append(A[j])
                n = j+1
                break
            elif j == N-1:
                a = N-1
                break
if L != []:
    print(len(A)-len(L))
elif len(A) == 1:
    print(0)
else:
    print(-1)

Solution (Python):
n = int(input())
L = list(map(int,input().split()))
i = 0
num = 1
cnt = 0
while i <=n-1:
    if L[i] == num:
        num +=1 
    else:
        cnt +=1
    i+= 1
if num == 1:
    print(-1)
else:
    print(cnt)

Solution (Python):
num=1
n=int(input())
a=list(map(int,input().split()))
if 1 not in a:
    print(-1)
else:    
    for i in range(n):
        if a[i]==num:
            num+=1
    print(n-num+1)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

if 1 not in a:
    print(-1)
    exit()

now = 1
ans = 0
for i in a:
    if i != now:
        ans += 1
    else:
        now += 1

print(ans)

Solution (Python):
n = int(input())
R = list(map(int, input().split()))
c, i = 0, 1
for r in R:
  if r==i: i+=1
  else: c+=1
print(c if c<n else '-1')

Solution (Python):
from collections import deque
n = int(input())
r = deque(map(int, input().split()))
i=0
while True:
    if max(len(r),i)==i:
        break
    if r[i]!=i+1:
        r.popleft()
    else:
        i+=1
print('-1' if len(r)==0 else n-len(r))

Solution (Python):
n,a=open(0);j=0
for b in map(int,a.split()):
    if b==j+1:j+=1
print([-1,int(n)-j][j>0])

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
if 1 in a:
    j = 0
    for ai in a:
        if ai == j + 1:
            j += 1
    print(n - j)
else:
    print(-1)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
c = 0
d = 0
for i in range(n):
    for j in range(c, n):
        if i + 1 == a[j]:
            c = j
            d += 1
            break
    else:
        break
    if c == n - 1:
        break
if 1 not in a:
    print(-1)
else:
    print(n - d)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
num=1
f=False
for i in range(n):
    if a[i]==num:
        num+=1
        f=True
print(n-num+1) if f==True else print(-1)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
a = list(map(int, readline().split()))
ans = 0
cnt = 1
for i in range(n):
    if a[i] == cnt:
        cnt += 1
    else:
        ans += 1
if ans == n:
    print(-1)
else:
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split())) 

nxt = 1
ans = 0

for i in range(N):
    if nxt == A[i]:
        nxt += 1
    else:
        ans += 1

if ans == N: ans = -1

print(ans)

Solution (Python):
N = int(input())
li = list(map(int, input().split()))

try:
  position1 = li.index(1)        
except:
  print(-1)
  exit()  
  
kowasu = position1
target = 2
position = position1 + 1
while True:
  flag = False
  for i in range(position, len(li)):
    if li[i] == target:
      flag = True
      kowasu += i - position


      tekito = i
      break
  if flag:
    target += 1
    if tekito == len(li) - 1:
      break
    position = tekito + 1
  else:
    kowasu += len(li) - position
    break
print(kowasu)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
if a.count(1) == 0:
    print(-1)
    exit()
cnt = 1
for i in range(a.index(1), n):
    if a[i] == cnt + 1:
        cnt += 1
        
print(len(a) - cnt)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]


tmp = 1
ans = 0
for x in a:
    if x == tmp:
        tmp += 1
    else:
        ans += 1

if tmp == 1:
    print(-1)
else:
    print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
tmp = 1
cnt = 0
for x in a:
    if x == tmp:
        tmp += 1
    else:
        cnt += 1
if tmp != 1:
    print(cnt)
else:
    print(-1)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

i = 1


if 1 in set(a):
    
    
    
    
    for a_i in a:
        if a_i == i:
            i += 1
    print(N - i + 1)
else:
    print(-1)

Solution (Python):
n = int(input())

A = list(map(int, input().split()))

if 1 not in A:
  print(-1)
else:
  index = []
  current = 1
  cnt = 0
  for a in A:
    if a == current:
      index.append(cnt)
      current += 1
    cnt += 1
  print(n-len(index))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = []

ind1 = 0

for i in a:
    if i == 1:
        break
    ind1 += 1


if 1 in a:
    b.append(1)
    for i in range(ind1, len(a)):
        if a[i] == b[-1] + 1:
            b.append(a[i])

if len(b) == 0:
    print(-1)
else:
    print(n - len(b))

Solution (Python):

import sys


def solve(N: int, a: "List[int]"):
    currentIndex = 1
    discarded = 0
    for i in range(N):
        if a[i] != currentIndex:
            discarded += 1
        else:
            currentIndex += 1
    if discarded == N:
        print(-1)
        exit()
    print(discarded)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    solve(N, a)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cnt = 0
for i in range(n):
  if a[i] == cnt+1:
    cnt += 1
print(-1 if cnt == 0 else n-cnt)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
j = 1

for i in range(N):
  if j == a[i]:
    j += 1
if j == 1:
  print(-1)
else:
  print(N-j+1)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))

current = 1
broken = 0
for i in range(n):
    if a[i] == current:
        current += 1
    else:
        broken += 1
if current == 1 and broken > 0:
    print('-1')
else:
    print(broken)

Solution (Python):
n = int(input())
num_list = list(map(int,input().split()))
cnt = 0
num = 1
for i in num_list:
    if i != num:
        cnt += 1
    else:
        num += 1

if num == 1:
    print(-1)
    
else:print(cnt)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(N):
    if i+1-ans != a[i]:
        ans += 1
print(-1 if ans==N else ans)

Solution (Python):
import sys
inout = sys.stdin.readline

def main():
    N = int(input())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(N):
        if i+1-ans != a[i]:
            ans += 1
    if ans == N:
        print(-1)
    else:
        print(ans)

if __name__ == '__main__': main()

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
ans = 0
tmp = 1
for i in range(N):
    if a[i] != tmp:
        ans += 1
    else:
        tmp += 1
if ans == N:
    print(-1)
else:
    print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = 1
for i in range(n):
    if a[i] == ans:
        ans += 1
print(n-ans+1 if ans != 1 else -1)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
x=1
for i in range(len(a)):
    if a[i]==x:
        x+=1
if 1 not in a:
    print(-1)
else:
    print(N-(x-1))

Solution (Python):
N = int(input())
*A, = map(int, input().split())
min_v = 1
cnt = 0
l = len(A)
for a in A:
    if min_v == a:
       cnt += 1
       min_v += 1
if cnt == 0:
    print(-1)
else:
    print(l - cnt)

Solution (Python):
N = int(input())
A = list(map(lambda x:int(x), input().split(' ')))

C = 1
H = 0

for i in A:
  if  i==C:
    H += 1
    C += 1

print( N-H if H > 0 else -1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))

cnt = 0
c = 1
for i in range(n):
    if a[i] == c:
        cnt += 1
        c += 1

if cnt == 0:
    print(-1)

else:
    print(n-cnt)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n = int(input())
    a = list(map(int, input().split()))
    cnt = 1
    for i in a:
        if i==cnt:
            cnt += 1
    if cnt == 1:
        print(-1)
    else:
        print(n-cnt+1)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    k = 1
    for i in range(n):
        if a[i] == k:
            k += 1
    k -= 1
    print(n - k) if k else print(-1)

main()

Solution (Python):
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    k = 1
    for i in range(n):
        if a[i] == k:
            k += 1
    k -= 1
    print(n - k) if k else print(-1)

main()

Solution (Python):




def main():
    N = int(input())
    a = list(map(int, input().split()))

    num = 1
    count = 0

    for i in a:
        if i == num:
            num += 1
        else:
            count += 1
    
    if num == 1:
        print(-1)
    else:
        print(count)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

cnt = 0

for i in range(N):
    if a[i] == cnt+1:
        cnt+=1

if cnt>0:
    print(N-cnt)
else:
    print(-1)

Solution (Python):
def resolve():
    N = int(input())
    arr = list(map(int, input().split()))
    exp = 1
    for num in arr:
        if exp == num:
            exp += 1

    if exp == 1:
        print('-1')
    else:
        print(N - (exp - 1))

resolve()

Solution (Python):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
A = lr()
cur = 0
for a in A:
    if a == cur + 1:
        cur += 1

print(N-cur if cur > 0 else -1)

Solution (Python):
n=int(input())
a=[0]+list(map(int,input().split()))
dp=[0]*(n+1)
dp[0]=1
for i in range(1,n+1):
    if dp[a[i]-1]:
        dp[a[i]]=1+dp[a[i]-1]
maxi=max(dp)-1
print(n-maxi) if dp[1] else print(-1)

Solution (Python):
N = int(input())
a = list(map(int, input().split(" ")))

count, now_num = 0, 1
for i in range(N):
  if a[i] == now_num:
    now_num += 1
  else:
    count += 1

print(count if count != N else -1)

Solution (Python):
n = int(input())
A = list(map(lambda x: int(x)-1, input().split()))
ans = 0
now = 0
for i in range(n):
    if A[i] != now:
        ans += 1
    else:
        now += 1

if ans == len(A):
    print(-1)
else:
    print(ans)

Solution (Python):
n=int(input())

al=list(map(int,input().split()))


num=1
ans=0
for i in range(n):
    if al[i]!=num:
        ans+=1
    else:
        num+=1
if ans==n:
    print(-1)
else:
    print(ans)

Solution (Python):
import bisect
N = int(input())
arr = list(map(int,input().split()))
cnt = [[] for i in range(N + 1)]

for i in range(N):
    num = arr[i]
    cnt[num].append(i)

sunuke = []

for i in range(1,N + 1):
    if cnt[i] == []:
        break
    if i == 1:
        sunuke.append(cnt[1][0])
        continue
    else:
        j = bisect.bisect_left(cnt[i],sunuke[-1])
        if j == len(cnt[i]):
            break
        else:
            sunuke.append(cnt[i][j])
print(N - len(sunuke) if len(sunuke) > 0 else -1)

Solution (Python):

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    k = 1
    for i in range(n):
        if a[i] == k:
            k += 1
    k -= 1
    print(n - k) if k else print(-1)  

main()

Solution (Python):
n = int(input())
numbers = list(map(int, input().split()))
count = 0
next_number = 1
if 1 in numbers :
    for i in range(len(numbers)):
        if numbers[i] != next_number:
            count += 1
        else:
            next_number += 1
    print(count)
else:
    print(-1)

Solution (Python):
n = int(input())
a =  list(map(int, input().split()))

now = 1
count = 0
for k in range(n):
    if a[k] == now:
        now += 1
    else:
        count += 1
if count == n:
    print(-1)
else:
    print(count)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
BS = 1
for i in range(N):
  if a[i] == BS:
    BS += 1
if BS == 1:
  print(-1)
else:
  print(N+1-BS)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

brickno = 1
for i in range(N):
    if a[i] == brickno:
        brickno += 1

if brickno == 1:
    print(-1)
else:
    print(N+1-brickno)

Solution (Python):
x=int(input())
a=list(map(int, input().split()))

ans=0
p=0

for i in range(x):
	if a[i]==p+1:
		p=p+1
	else:
		ans=ans+1

if p==0:
	print(-1)
else:
	print(ans)

Solution (Python):
N = int(input())
a = map(int, input().split())
A = 0
C = 1
for i in a:
  if i == C:
    C += 1
  else:
    A += 1
print(-1 if C == 1 else A)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
i = 0
check = 1
satisfy = [False for i in range(n)]
break_block = 0
while i < n:
    if a[i] == check and not satisfy[check - 1]:
        satisfy[check - 1] = True
        check += 1
    else:
        break_block += 1
    i += 1
if not satisfy[0]:
    print(-1)
else:
    print(break_block)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
c=0
for i in a:
    if i==c+1:
        c+=1
print(N-c if c else -1)

Solution (Python):
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
if not 1 in a:
    count = -1
else:
    for j in range(N):

        if a[j] ==searchNum:
            searchNum += 1
            continue
        else:
            count +=1
print(count)

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]

ans = 0
cnt = 0
for i in range(N):
  if a[i] == cnt+1:
    cnt += 1
  else:
    ans += 1

if cnt == 0:
  print(-1)
else:
  print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
K = 0
ans = 0

for i in range(N):
    k = a[i]
    if k == K + 1:
        K = k
    else:
        ans += 1

if K == 0:
    ans = -1

print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
count = 1
brick = 0
for i in range(N):
    if a[i] != count:
        brick += 1
    else:
        count += 1
if brick == N:
    print(-1)
else:
    print(brick)

Solution (Python):
N = int(input())
a_list = list(map(int, input().split()))

ans = 0
current = 0
for a in a_list:
    if a == current + 1:
        current += 1
if current > 0:
    print(N-current)
else:
    print(-1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
cou = 1
ans = 0

for i in range(n):
    if a[i] == cou:
        cou += 1
    elif a[i] != cou:
        ans += 1

if ans == n:
    print("-1")
else:
    print(ans)

Solution (Python):
N=int(input())
a=input().split()
A=[int(b) for b in a]
n=1
for a in A:
    if a==n:
        n+=1
if n==1:
    print(-1)
else:
    print(N-n+1)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
i,k=0,1
while i<n:
  if a[i]==k:
    k+=1
    i+=1
  else:
    i+=1
if k==1:
  print(-1)
else:
  print(n-k+1)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
cnt = 0
target = 1
if 1 not in a:
    print(-1)
else:
    for i in range(len(a)):
        if a[i] == target:
            a[i] = '_'
            target += 1
        
    result = [x for x in a if x != '_']

    print(len(result))

Solution (Python):
n = int(input())
alist = list(map(int,input().split()))
count = 1
broken = 0
flag = False
for i in range(0,n):
    if alist[i] == count:
        count += 1
    else:
        broken += 1

if 1 in alist:
    print(broken)
else:
    print('-1')

Solution (Python):
N = int(input())
a = list(map(int, input().split()))

ans = 0
num = 1
flag = False

for i in range(N):
    if a[i] == num:
        num += 1
        flag = True
    else:
        ans += 1

if flag:
    print(ans)
else:
    print(-1)

Solution (Python):
n = int(input())

tmp = [int(s) for s in input().split()]

target = 1
flag = 0
for i in range(n):
    if(tmp[i] == target):
        flag = 1
        target += 1

if(flag == 0):
    print(-1)
else:
    print(n - target +1)

Solution (Python):
n = int(input())
lst=list(map(int, input().split()))
target=1
for i in range(n):
    if lst[i]==target:
        target+=1
if target==1:
    print(-1)
else:
    print(n-target+1)

Solution (Python):
import sys

N=int(input())

A=list(map(int,input().split()))

ans=0
now=1
old=0

if not 1 in A:
    print(-1)
    sys.exit()

for i in range(N):
    if A[i]==now:
        now+=1
        ans=ans+i-old
        old=i+1
ans=ans+N-old

print(ans)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))



flg = 0

for i in L:
    if i == (flg + 1):
        flg += 1

if flg > 0:
    print(len(L) - flg)
else:
    print(-1)