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())
l = [int(i) for i in input().split()]

target = 1
block = 0
sum = 0
for i in range(n):
    block += 1
    if l[i] == target:
        sum += block - 1
        block = 0
        target += 1
sum += block
if target == 1:
    print(-1)
else:
    print(sum)

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


for i in range(N):
    if len(ans)+1==A[i]:
        ans.append(i)
if len(ans)==0:
    print(-1)
    exit()
print(len(A) - len(ans))

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,*a=map(int,open(0).read().split())
now=1
for i in a:
    if i==now:
        now+=1
print(-1 if 1 not in a else n-now+1)

Solution (Python):
import sys

n = int(input())
numbers = list(map(int, sys.stdin.readline().split()))

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

breaks = 0
current = 1
for i in range(n):
    if numbers[i] == current:
        current += 1
    else:
        breaks += 1
print(breaks)

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

seekfor = 1
crack = 0

for i in range(N) :
    if A[i] == seekfor :
       seekfor += 1
    else : 
       crack += 1
    
if crack == N: 
    print( -1 )
else:    
    print(crack)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
j = 1
n = 0
while n <= N-1:
    try:
        n = A.index(j,n)
        n += 1 
        j += 1   
    except ValueError:
        j = j-1
        if j == 0:
            print(-1)
        else:
            print(N-j)
        exit()
print(N-j+1)

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

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


number = 1
ans = 0
for x in L:
    if(x == number):
        number += 1
        ans += 1

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

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

Solution (Python):

import sys


def solve(N: int, a: "List[int]"):
    num = 1
    for aa in a:
        if aa == num:
            num +=1
    
    if num == 1:
        print(-1)
    else:
        print(N-(num-1))
    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()))

target = 1
ans = 0

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

if ans == N:
    ans = -1

print(ans)

Solution (Python):
n = int(input())
k = 0
for x in map(int, input().split()):
  if x == k + 1:
    k += 1
print(n - k if k else -1)

Solution (Python):
N=int(input())
l=list(map(int, input().split()))
c=0
s=1
for i in range(N):
  if l[i]==s:
    c+=1
    s+=1

if c==0:
  print(-1)
else:
  print(len(l)-c)

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

i = 0
err = True
for num in arr:
  if num == i + 1:
    i += 1
    err = False
if err:
  print(-1)
else:
  print(N - i)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
i = 0
j = 0
c = 0
while 1:
    if j >= len(a):
        break
    if a[j] != i+1:
        j += 1
        c += 1
    else:
        j += 1
        i += 1
if len(a) == c:
    print(-1)
else:
    print(c)

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

if 1 not in a:
  print(-1);exit()
from collections import*  
b=deque(a)

i=1
while True:
  if len(b)==0:
    break
  if b[0]==i:
    cnt+=0
    b.popleft()
    i+=1
  else:
    cnt+=1
    b.popleft()
  

print(cnt)

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

for i in range(0,n):
  if a[i] == cnt:
    cnt = cnt + 1
  else:
    res = res + 1
    
if (cnt - 1) == (n - res) and not res == n:
  print(res)
else:
  print(-1)

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

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

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

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

b = []
j = 0
k = 1

for i in range(n):
  if k == int(a[i]):
    b.append(int(a[i]))
    j += 1
    k += 1

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

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
c=0
k=1
f=0
for i in range(n):    
    if l[i]==k:
        f=1
        c+=1
        k+=1   

if not f:
    print(-1)
elif c==n:
    print(0)
else:
    print(n-c)

Solution (Python):
from collections import deque
n = int(input())
a = list(map(int, input().split()))
a = deque(a)
d = 0
i = 1
for _ in range(n):
    b = a.popleft()
    if i == b:
        d +=1
        i += 1
        
print(n-d if d !=0 else -1)

Solution (Python):
def tmpq4(n,alist):
    akouho = []
    for tmpi,ai in enumerate(alist):
        i = tmpi+1
        if(ai<=i):
            akouho.append(ai)
    if(n==len(akouho)):
        return akouho
    return tmpq4(len(akouho),akouho)

def q4(n,alist):
    alis = tmpq4(n,alist)
    alis = list(set(alis))
    anslist=[]
    for i in range(len(alis)):
        if((i+1)==alis[i]):
            anslist.append(alis[i])
        else:
            break
    if(len(anslist)>0):
        return (n-len(anslist))
    else:
        return -1
    
n= int(input())
alisttmp = input().split()
alist = []
for num in alisttmp:
    alist.append(int(num))
    
print(q4(n,alist))

Solution (Python):

from sys import stdin
N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]

cnt = 0
idx = 1

for i in a:
	if i == idx:
		idx = idx + 1
		
	else:
		cnt = cnt + 1
		
			

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

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
if 1 not in a:
  print(-1)
else:
  i = 1
  j = 0
  s = 0
  while j != N:
    if a[j] == i:
      sum = j-i+1
      i += 1
      s = j
    j += 1
  if s != N-1:
    sum += N-s-1
  print(sum)

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

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

limit = max(lst)

num = [[] for _ in range(limit)]
for _ in range(n):
    if lst[_] <= limit:
        num[lst[_] - 1].append(_)

from collections import deque
def comp():
    queue = deque(num)
    try:
        now = min(queue.popleft())
        c = 1
    except:
        return -1
    while queue:
        try:
            nxt = min([_ for _ in queue.popleft() if _ > now])
            if nxt > now:
                c += 1
                now = nxt
            else:
                return n - c
        except:
            return n - c
    return n - c

print(comp())

Solution (Python):
num = int(input())
input_data = input().split(' ')
if("1" in input_data):
    input_data = input_data[input_data.index('1'):]
    ref = 1
    for i in range(len(input_data)):
        if(int(input_data[i]) - ref == 1):
            ref += 1

    print(num - ref)
else:
    print('-1')

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

count = 0
flag = False
hit_flag = False

for a in a_list:
    if a == count + 1:
        count += 1
        flag = True

if not flag:
    print(-1)
else:
    print(n - count)

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

for i in range(N):
  if A[i] == num:
    num += 1
  else:
    count += 1
if count == N:
  print(-1)
else:
  print(count)

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

l = len(A)
f = [0] * l


f[0] = 0
flag=0
if A[0] == 1:
    flag = 1

for i in range(1, l):
    if A[0] == 1 and f[i-1]==0:
        if A[i] == i+1:
            f[i] = 0
        else:
            f[i] = 1
    elif f[i-1] != 0:
        if A[i] == i - f[i-1] + 1:
            f[i] = f[i-1]
        else:
            f[i] = f[i-1] + 1 
    else:
        if A[i] == 1:
            f[i] = i
        else:
            f[i] = 0

if f[l-1]==0 and flag==0:
    print(int(-1))
    exit(0)

print(f[l-1])

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

c = []
j = 0
if 1 not in s:
    print(-1)
elif len(s) == 1:
    print(0)
else:
    for i in s:
        if i - j == 1:
            j = i
            c.append(i)
    print(n - len(c))

Solution (Python):

import sys
import itertools


i=0
N=int(input())




a_list=  list(map(int, input().split(" ")))
j=0
times=0

for i  in range(N):
    if a_list[i]==i-times+1:
       pass
    else:
       times=times+1

if times==len(a_list):
    print(-1)
    sys.exit()
print(times)

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

a=[int(x) for x in input().split()]

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

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

print(N-n+1)

Solution (Python):

N = int(input())
A = [int(x) for x in input().split(" ")]


def search(A, j, N, x):
    for i in range(j, N):
        if A[i] == x:
            return i
    return "n"


k = 0
tempk = -1
for i in range(1, N+1):
    k = search(A, k, N, i)
    if k == "n":
        break
    else:
        tempk = k

if tempk == -1:
    print(tempk)
else:
    print(N - A[tempk])

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

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

n = 1
cnt = 0
for i in a:
  if i==n:
    cnt+=1
    n+=1

if cnt ==0:
  answer = -1
else:
  answer = N - cnt

print(answer)

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

for i in range(n):
    if a[i] == counter:
        counter += 1
    else:
        n_break += 1

if n_break == n:
    n_break = -1

print(n_break)

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

target = 1
count = 0

for a in l:
    if a == target:
        target += 1
    else:
        count += 1
        
if(count == len(l)):
    print(-1)
else:
    print(count)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
total_breaks = 0
position = 0
for i in range(1, n + 1):
    try:
        next_position = a.index(i, position)
        if next_position < position:
            total_breaks = total_breaks + n - position
            break
        total_breaks = total_breaks + next_position - position
        position = next_position + 1
    except ValueError:
        if position == 0:
            total_breaks = -1
            break
        else:
            total_breaks = total_breaks + n - position
            break
print(total_breaks)

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


count = 0
c = 1

for i in range(N):
    
    if c != a[i]:
       
        count += 1
    else:
        c+=1




if c-1==0 and count > 1:
    print(-1)
else:
    print(count)

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

Solution (Python):
def main():

    N = int(input())
    num_input = list(map(int,input().split()))

    if(N==1 and num_input[0]==1):
        print(0)
        return
    if(1 in num_input):
        print(numCount(num_input))
    else:
        print(-1)

def numCount(num_input):
    now = 0
    count = 0
    for i in num_input:
        if(now == (i-1)):
            now = i
        else:
            count += 1
    return count

main()

Solution (Python):

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

block = 0
num = 1

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

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

Solution (Python):
def main():
    n = int(input())
    n_list = list(map(int, input().split()))

    previous = 0
    for i in range(n):
        if n_list[i] == previous + 1:
            previous += 1

    if previous == 0:
        print("-1")
    else:
        print(n - previous)


if __name__ == '__main__':
    main()

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

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

Solution (Python):


N = int(input())
a = [int(i) for i in input().split()]
j=0
for i in range(N):
    b=a[i]
    if b==j+1:
        j+=1
if j ==0:
    s=-1
else:s=N-j
print(s)

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


target = 1
res = 0
flag = False

for x in a:
	if x == target:
		target += 1
		flag = True
	else:
		res += 1

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

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



min_indexlist = []

m = min(a_list)




"""
for i in range(len(a_list)):
    if( a_list[i] == m+1):
        m = a_list[i]
        min_indexlist.append(i)
"""


f = False

for i in range(len(a_list)):

    if(f==False):
        if(a_list[i] == 1):
            min_indexlist.append(i)
            f = True

    if(f==True):
        if( a_list[i] == m+1):
            m = a_list[i]
            min_indexlist.append(i)
            
if(len(min_indexlist)==0):
    min_indexlist.append(1)






crash_list = []
for i in range(len(min_indexlist)):
    crash_list.append(a_list[min_indexlist[i]])




crash = n - len(min_indexlist)


frag=True

for i in range(len(crash_list)):
    if(crash_list[i] != i+1):
        frag = False


if(frag == True):
    print(crash)
elif(frag == False):
    print(-1)

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

c = 0
for a in A:
    if a == c+1:
        c += 1
if c == 0:
    print(-1)
else:
    print(N-c)

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

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

Solution (Python):
num, bricks = int(input()), [int(x) for x in input().split(" ")]
count = 1
smash = 0
for x in bricks:
    if count == x:
        count += 1
    else:
        smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

Solution (Python):
num, bricks, count, smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: count += 1
    else: smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

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

x = [0]*N

cnt = 1

for i in range(N):
	if a[i] == cnt:
		cnt += 1
		continue
	x[i] = 1
	
n = 0
for i in x:
	n += i
	
if n == N:
	n = -1

print(n)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
ans=[[]for i in range(n+1)]
for i in range(n):
    ans[l[i]].append(i+1)

    
c=1
tem=[]
ch=0
while 1:
    if c>n:
        break
    else:
        
        if len(ans[c])==0:
            break
        else:
            flag=0
            for i in ans[c]:
                if i>ch:
                    tem.append(i)
                    ch=i
                    flag=1
                    break
            if not flag:
                break
            else:
                c+=1
                    
                
if len(tem)==0:
    print(-1)
else:
    print(n-len(tem))

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

try:
    idx = a_list.index(1)
except:
    print(-1)
    sys.exit(0)


value = 1

for k, v in enumerate(a_list):
    if idx <= k and value+1 == v:
        value += 1

print(len(a_list) - value)

Solution (Python):

from sys import stdin

n = int(stdin.readline().rstrip())
L = list(map(int,input().split()))

j = 1
for i in range(n):
 if int(L[i]) == j:
  j = j + 1

if (j == 1):
 print(-1)
else:
 print (n - j + 1)

Solution (Python):
num, bricks, count, smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: 
      count += 1
    else: 
      smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

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

evaluate_value = 1
cnt = 0

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

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

Solution (Python):
n = int(input())
lis = list(map(int, input().split()))
search_target = 1
founded = 0
for li in lis:
    
    if li == search_target:
        search_target += 1
        founded = li
        continue
    else:
        continue
if founded == 0:
    print(-1)
else:
    print(n-founded)

Solution (Python):
n = int(input())
list1 = [int(i)for i in input().split()]
pos = 1
cnt = 0
if list1.count(1)==0:
    print(-1)
else:
    for x in range(n):
        if pos==n:
            break
        if list1[x]==pos:
            pos+=1
        else:
            cnt+=1
    print(cnt)

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

ans=-1
inf=0
for i in range(1,n+1):
    flag=0
    for j in range(inf,n):
        if renga[j]==i:
            ans=i
            inf=j+1
            flag=1
            break
    if flag==0:
        break

if ans!=-1:
    ans=n-ans

print(ans)

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

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

Solution (Python):
import sys
def fastio():
    from io import StringIO
    from atexit import register
    global input
    sys.stdin = StringIO(sys.stdin.read())
    input = lambda : sys.stdin.readline().rstrip('\r\n')
    sys.stdout = StringIO()
    register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var):
    print(*var, file=sys.stderr)

INF = 10**20
MOD = 10**9 + 7
I = lambda:list(map(int,input().split()))




n, = I()
a = [i-1 for i in I()]
ans = 1
if 0 in a:
    cur = 0
    ans = 0
    for i in range(n):
        if a[i] == cur:
            ans += 1
            cur += 1
    print(n - ans)
else:
    print(-1)

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

if brk==0:
  print(0)
elif check==1 and brk==N:
  print(-1)
else:
  print(brk)

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

Solution (Python):
n = int(input())
array = list(input().split())
count = 1
breakCount = 0
for s in array:
    if int(s) == count:
        count = count + 1
    else:
        breakCount  = breakCount + 1

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

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

Solution (Python):
num, bricks, count, smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: count += 1
    else: smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

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

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

Solution (Python):
N = int(input())
bricks = [int(x) for x in input().split()]
count = 1
smash = 0
for x in bricks:
    if count == x: 
      count += 1
    else: 
      smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

Solution (Python):
n = int(input())
alist = [int(_) for _ in input().split()]

target = 1
result = 0
for i in range(len(alist)):
  if alist[i] == target:
    target += 1
  else:
    result += 1

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split(' ')]
pt = 1
ans = 0
for i in range(N):
    if pt != A[i]:
        ans += 1
    else:
        pt += 1

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

Solution (Python):

import sys


def solve(N: int, a: "List[int]"):
    
    a_org = a.copy()
    match_cnt = 1
    pop_cnt = 0
    for i in range(N):
        
        if a_org[i] != match_cnt:
            
            pop_cnt = pop_cnt + 1
        else:
            match_cnt = match_cnt + 1
            
    
    if pop_cnt == N:
        pop_cnt = -1
    print(pop_cnt)
    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=[int(i) for i in input().split()]
if a.count(1)==0:
    print(-1)
else:
    ln=[]
    for i in range(n):
        if a[i]==len(ln)+1:
            ln.append(a[i])
    print(n-len(ln))

Solution (Python):
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
count = 0

if 1 in a:
    pass
else:
    print(-1)
    exit()

no = 1
for i in range(len(a)):
    if a[i]==no:
        no += 1
    else:
        count += 1

print(count)

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



c = 1
b = [1]
while c!=0 and (len(a) != 0) :
    b = []
    c = 0
    for i in enumerate(a):
        if not (i[0]+1 < i[1]):
            b.append(i[1])
        else:
            c = 1
    a = b
 

if len(a)==0:
    print(-1)
else:
    a = sorted(list(set(a)))
    b = []
    for i in enumerate(a):
        if i[0]+1 == i[1]:
            b.append(i[1])
    print(N-len(b))

Solution (Python):
import sys

N = int(input())
A = input().split(" ")

for i in range(N):
    if A[i] == "1":
        break
    elif i == N-1:
        print("-1")
        sys.exit()

counter = "1"
ans = 0
for i in range(N):
    if A[i] != counter:
        ans +=1
    else:
        counter = str(int(counter) + 1)

print(str(ans))

Solution (Python):
renga_num = int(input())
renga_line = list(map(int,input().split()))

num = 1
break_num = 0
for i in range(0,renga_num):
    if renga_line[i] != num:
        break_num += 1
    else:
        num += 1

if break_num == renga_num:
    print(-1)
else:
    print(break_num)

Solution (Python):
import sys

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

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

res = 0
j = 1

for i in a:
    if i != j:
        res += 1
    else:
        j += 1

print(res)

Solution (Python):
"""
左から 1,2,3 .. とどこまで並べられるか調べる
while で探索して行き，できなくなったら終わり
全体数(N)　- 残った数 = 砕いた数になる
N .. レンガの個数
a .. レンガに書かれている数
position .. 現在探している数
ans .. ここまでは整数揃ったよっていう値 (最終的にN- ans で答え出す)
"""

N = int(input())
a = list(map(int,input().split()))
position = 0
ans = 0
while True:
  if position >= N:
    print(-1) if ans == 0 else print(N-ans)
    break
  for i in range(position,N):
    if a[i] == ans + 1:
      
      position = i
      ans += 1
      break
  else: 
    position = N

Solution (Python):
def main():
    N =int(input())
    list_a = list(map(int,input().split()))
    i = 1
    j = 0
    count = 0
    while N > j :
        if list_a[j] == i:
            i += 1
            j += 1
        else :
            count += 1
            j += 1
    if i == 1:
        print(-1)
    else :
        print(count)

main()

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

save = 1
ans = -1
tmp = []
for i in range(1,200000):
    tmp.append(i)

flag = False

for i in a:
    if save == i:
        if i == 1:
            ans += 1
            flag = True
        save += 1
    else:
        ans += 1
if a == tmp[:len(a)]:
    print(0)
else:
    if flag:
        print(ans)
    else:
       print(-1)

Solution (Python):
num, bricks, count, smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: count += 1
    else: smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

Solution (Python):
num, bricks, count, smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: count += 1
    else: smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

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

for i in range(n):
    if a[i] == cur:
        flag = 1
        cur += 1
        
if flag == 1:
    print(n-(cur-1))
else:
    print(-1)

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

ans = -1
tmp_nums = []
check_num = 1

for i in range(len(A)):
  if A[i] == 1:
    check_num = 2
    tmp_nums = [1]
    ans = max(ans,len(tmp_nums))
  elif A[i] == check_num:
    check_num += 1
    tmp_nums.append(A[i])
    ans = max(ans,len(tmp_nums))

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

Solution (Python):

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

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

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

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

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


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)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
cur = 1
for i in a:
    if i == cur:
        cur += 1

if cur == 1:
    ans =-1
else:
    ans = n - cur + 1
print(ans)

Solution (Python):
N = int(input())
str_numbers = input().split(" ")
numbers = [int(str_num) for str_num in str_numbers]
memo = []
match = 1
count = 0
for i in range(len(numbers)):
  if numbers[i] == match:
    match += 1
    count += 1

if count > 0:
  print(N - count)
elif count == 0:
  print(-1)

Solution (Python):
num, bricks, count, smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: count += 1
    else: smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

Solution (Python):
from collections import deque

n = int(input())
A = deque(map(int, input().split()))
i = 1
count = 0

while A:
  a = A.popleft()
  if i != a:
    count += 1
  else:
    i += 1
    
print(-1 if count == n else count)

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

num=0

j=0

for i in range(1,N+1):
    while(j<N):
        if a[j]==i:
            num+=1
            j+=1
            break
        j+=1

    if j==N:
        break

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

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

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

if ans == len(a):
    print(-1)
    exit()
print(ans)

Solution (Python):
N = int(input())
n = input()    
m = n.split()
p = [int(i) for i in m]
j = 1
for i in range(N):
    if p[i] == j:
        j = j+1
if j == 1:
    print("-1")
else:
    print(N - j + 1)

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

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

Solution (Python):
n=int(input())
t=list(map(int,input().split(' ')))
x=1;z=len(t)+1
for i in range(n):
    if t[i]==x:
        z=x ; x+=1
print(len(t)-z)

Solution (Python):
num, bricks, count, smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: count += 1
    else: smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

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

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


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

print(ans) if ans != len(a) else print(-1)

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

point = 1
count = 0
ans = -1

if N == 1 and a[0] == 1:
  ans = 0
else:
  for i in a:
    if i == point:
      count += 1
      point += 1
      
if count == 0:
  print(ans)
else:
  print(len(a)-count)