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

cnt = 1
br = 0

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

if br == n:
    br = -1

print(br)

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

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

print(n - flag + 1 if 1 in a else -1)

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


def solve(nums):
    cnt = 0
    num = 1
    if 1 not in nums:
        return -1

    for i in range(n):
        if nums[i] == num:
            num += 1
        else:
            cnt += 1
    return cnt if cnt > 0 else 0


print(solve(nums))

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

Solution (Python):
def solve(n, A):
    num, count = 0, 0
    for a in A:
        if a == num + 1:
            num += 1
        else:
            count += 1
    return count if num > 0 else -1

_n = int(input())
_A = list(map(int, input().split()))
print(solve(_n, _A))

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

Solution (Python):
N = int(input())
numList = list(map(int, input().split()))
i = 0
temp = 1
counter = 0
while True:
    if numList[i] == temp:
        temp += 1
        counter += 1
    i += 1
    if i == N:
        break
if counter == 0:
    print(-1)
else:
    print(N - counter)

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

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

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

Solution (Python):





def main():
    n = int(input())
    al = list(map(int, input().split()))

    
    

    

    cnt = 1
    bl = []
    for ai in al:
        if ai == cnt:
            bl.append(ai)
            cnt += 1
    b_cnt = len(al) - len(bl)
    if(cnt == 1):
        print(-1)
    else:
        print(b_cnt)

if __name__ == "__main__":
    main()

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

"""考え方
左から、小さい順に数字を取得する。
これで得られる最大の値とレンガの個数の差分が答え。
ただし、「1」が無い場合は「-1」となる。
"""

pre_num = 0
for n in range(0,N,1):
  if A[n] == pre_num+1:
    pre_num = A[n]

if pre_num == 0:
  ans = -1
else:
  ans = N - pre_num

print(ans)

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

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

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

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
cnt=1
for i in A:
  if i==cnt:
    cnt+=1
if cnt==1:
  print(-1)
else:
  print(N-cnt+1)

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

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

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

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

Solution (Python):


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

Solution (Python):
"""TLE
N = int(input())
A_list = list(map(int, input().split()))
i = 0
while(True):
    try:
        if A_list[i] != i+1:
            del A_list[i]
        else:
            i += 1
    except:
        if A_list == []:
            print(-1)
        else:
            print(N-len(A_list))
        exit()
"""

N = int(input())
A_list = list(map(int, input().split()))
iirenga = 1
kudaita = 0
for i in range(N):
    if A_list[i] == iirenga:
        iirenga += 1
    else:
        kudaita += 1
if kudaita == N:
    print(-1)
    exit()
print(kudaita)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
c = 0
for i in range(n):
  if a[i] != i+1-c:
    c += 1
if c < n:
  print(str(c))
else:
  print(-1)

Solution (Python):
N = int(input())
li = [int(zz) for zz in input().split()]

count = 1
ans = 0
ind = 0
while ind < N:
    if li[ind] == count:
        count += 1
        ans += 1
    ind += 1

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

Solution (Python):
n = int(input())
list_a = list(map(int, input().split()))
list_a_complete = []
k = 1
ans = -2

for i in range(n):
    if list_a[i] == k:
        list_a_complete.append(k)
        k += 1

if len(list_a_complete) == 0:
    ans = -1

else:
    ans = len(list_a) - len(list_a_complete)

print(ans)

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

s = 1
ans = 0

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

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

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

start = 1

for num in nums:
    if num != start:
        continue
    start += 1

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
mark = 1
ans = N
pena = 1
for i in range(N):
    if A[i] == mark:
        pena = 0
        mark += 1
        ans -= 1
if pena == 1:
    print(-1)
else:
    print(ans)

Solution (Python):
import sys

stdin = sys.stdin

ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
sys.setrecursionlimit(10 ** 7)

def main():
    n = ni()
    a = na()

    cur = 1
    cnt = 0
    is_ok = False
    for aa in a:
        if aa == cur:
            is_ok = True
            cur += 1
        else:
            cnt += 1
    if is_ok:
        print(cnt)
    else:
        print(-1)
if __name__ == '__main__':
    main()

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

for i in a:
    if d == i:
        d += 1
    else:
        c += 1
print(-1 if c == n else c)

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

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

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

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

i = 1

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

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

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

cnt = 1
for v in a:
    if v == cnt:
        cnt += 1
if cnt == 1:
    print(-1)
else:
    print(n - (cnt - 1))

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

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

target = 1
for i in range(N):
    if A[i] == target:
        target += 1
        continue

print(N - target + 1)

Solution (Python):
N = int(input())
height = list(map(int, input().split()))
count = 0
num = 1

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

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

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

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

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

if 1 not in A:
    print(-1)
    exit()
bef=1
ans=0
for i in A:
    if i==bef:
        bef+=1
    else:
        ans+=1
print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split(" ")))

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

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

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

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

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

Solution (Python):
import os

def main():
    count = int(input())
    list_num = list(map(int, input().split()))
    result_list = []
    if not(1 in list_num):
        print("-1")
    else:
        num = 1
        for i in list_num:
            if num == i:
                result_list.append(num)
                num += 1
        print(len(list_num) - len(result_list))


if __name__ == "__main__":
    main()

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

if count == 0:
  result = -1
else:
  result = N - count
print(result)

Solution (Python):
def t(a):
    b = [False] * len(a)
    c = 1
    for i, j in enumerate(a):
        if j == c:
            b[i] = True
            c += 1

    return b.count(False) if any(b) else -1


if __name__ == '__main__':
    n = input()
    a = list(map(int, input().split()))
    print(t(a))

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

prev = 1
crash = 0

if not 1 in a:
  print("-1")
else:
  for i in range(N):
    if a[i] == prev:
      prev += 1
    else:
      crash += 1
  else:
    print(crash)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import floor, ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect
from fractions import gcd

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()

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

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

Solution (Python):



import sys


input()
n_ls = [int(e) for e in input().split(" ")]


satisfy_n = 0
crush_n = 0
for n in n_ls:
	if n == (satisfy_n+1):
		satisfy_n += 1
	else:
		crush_n += 1


if satisfy_n == 0:
	print(-1)
else:
	print(crush_n)

Solution (Python):

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

search_num = 1
count = 0

for i in range(n):
    if a_list[i] == search_num:
        search_num = search_num + 1
        count = count + 1

if search_num == 1:
    print(-1)
else:
    print(n-count)

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

cnt = 1
break_cnt = 0

for i in range(N):
    if block_list[i] == cnt:
        blocked_list.append(cnt)
        cnt += 1

    else:
        break_cnt += 1


if blocked_list == []:
    print(-1)

else:
    print(break_cnt)

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

number = 1
count = 0
for num in a:
    if num==number:
        number += 1
    else:
        count += 1

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

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

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

res = 0
crr = 1
for a in a_list:
    if a == crr:
        crr += 1
    else:
        res += 1

print(-1 if res == N else res)

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

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

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

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

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

    print(n - cnt + 1)

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

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

count = 0
cur = 1
for i in range(len(A)):
    if A[i] == cur:
        cur += 1
    else:
        count += 1
if count == len(A):
    count = -1
print(count)

Solution (Python):
n = int(input())
seq = [int(x) for x in input().split()]
cur_id = 1
ans = 0
for i in range(len(seq)):
  if seq[i] == cur_id:
    cur_id += 1
  else:
    ans += 1
if cur_id == 1:
  ans = -1
print(ans)

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

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

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

if 1 not in A:
  print(-1);exit()

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

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

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

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
K = 0
p = 0
bricks = [[] for i in range(N)] 

for i in range(N):
  bricks[a[i] - 1].append(i)

for i in range(N):
  if len(bricks[i]) == 0:
    break
  elif bricks[i][-1] < p:
    break
  else:
    for j in bricks[i]:
      if j >= p:
        p = j
        K += 1
        break
if K == 0:
  print(-1)
else:
  print(N - K)

Solution (Python):
import sys
 
def main():
    N = int(input())
    a = list(map(int,input().split()))
    num = 1
 
    for i in a:
        if(i==num):
            num += 1
 
    if(num!=1):
        print(len(a) - num + 1)
    else:
        print('-1')
 
main()

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

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

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

i = 0

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

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

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

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

if cnt + ans == N and cnt != 0:
    print(ans)
else:
    print(-1)

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


for i in range(n):
    if li[i] == check:
        check += 1
    else:
        ans += 1
        
if ans == n:
    print(-1)
else:
    print(ans)

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

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

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

print([ans, -1][i==1])

Solution (Python):
from bisect import bisect_right, bisect_left
from itertools import groupby, permutations, combinations
from fractions import gcd
import numpy as np
from sys import stdin, stderr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def main():
    N = int(input())
    a = [int(x) for x in input().split()]
    cnt = 1
    ret = 0
    for i in range(N):
        if a[i] == cnt:
            cnt += 1
        else:
            ret += 1
    if ret == N:
        ret = -1
    return ret


if __name__ == "__main__":
    print(main())

Solution (Python):
class AC:
    @staticmethod
    def inputStr(char):
        return input().split(char)
    
    @staticmethod
    def inputInt(char):
        a = input().split(char)
        for i in range(len(a)):
            a[i] = int(a[i])
        return a

N  = int(input())
a = AC.inputInt(" ")

counter_1 = 1 
counter_2 = 0 
delete = 0

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

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

Solution (Python):

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

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

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

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

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

tmp = 1
for l in L:
    if l == tmp:
        tmp += 1
if tmp == 1:
    print(-1)
else:
    print(N-tmp+1)

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

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

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

if not 1 in A:
    res = -1
else:
    b = 1
    B = []
    for a in A:
        if a == b:
            B.append(b)
            b += 1
    res = N - len(B)

print(res)

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

num = 1

for a in A:
    if(a == num):
        num += 1

ans = N - num + 1

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

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

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

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

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

c = 1
r = 0
for i in a:
    if i == c:
        c += 1
    else:
        r += 1
if r == n:
    r = -1
print(r)

Solution (Python):
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)

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

cur = 1
ans = 0
for _a in a:
    if _a == cur:
        cur += 1
    else:
        ans += 1

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

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

    count = 1
    ans = 0
 
    for i in range(N):
        if a_list[i] != count:
            ans += 1
        else:
            count += 1
 
    if ans == N:
        print(-1)
    else:
        print(ans)
    
 
if __name__ == "__main__":
    resolve()

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

temp=1
res=[]
for x in a:
  if x == temp:
    temp+=1

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

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

temp=1
res=[]
for x in a:
  if x == temp:
    temp+=1

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

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

start_num = 1
result_list = []
for num in nums:
    if num == start_num:
        result_list.append(num)
        start_num += 1

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

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

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

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

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

break_num = 0
counter = 1

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

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

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:
    print(-1)
else:
    print(ans)

Solution (Python):
n = int(input())
ls = list(map(int, input().split()))
k = 1
lo = 0
ans = 0
for x in range(n):
    if ls[x] == k and k == 1:
        lo = x
        ans = x
        k += 1
      
    elif ls[x] == k:
        ans += x-1 - lo
        lo = x
        k += 1
      
    else:
        pass
if k == 1:
    print(-1)
else:
    print(ans+((n-1)-lo))

Solution (Python):
n = int(input())
cnt = 1
break_cnt = 0
input_brick = map(int, input().split(' '))
flag = False
for i in input_brick:
    if i == 1:
        flag = True
    if cnt == i:
        cnt += 1
        continue
    break_cnt += 1
if flag:
    print(break_cnt)
else:
    print('-1')

Solution (Python):
n = int(input())
lis = list((map(int, input().split())))
flug = 1
for i in lis:
    if i == flug:
        flug += 1
if flug == 1:
    print('-1')
else:
    print(n - flug + 1)

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

p=1
for i in a:
  if i==p:
    p+=1
if p!=1:
  print(n-p+1)
else:
  print(-1)

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

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

now = 1
br = 0
for j, i in enumerate(a):
  if i == now:
    now += 1
    id.append(j)
  else:
    br += 1


if len(id) == 0:
  print(-1)
else:
  print(br)

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

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

c = 0


for i in range(N):
    
    if input_a[i] == c+1:
        c += 1
        

if c == 0:
    print('-1')
else:
    print(N - c)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
brick = [0]
number = 1

import sys

if min(a) > 1:
  print('-1')
  sys.exit()

for i in range(N):
  if a[i] == number:
    brick.append(number)
    number += 1
print(N - max(brick))

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

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

for i in range(len(a)):
  if a[i] != point:
    ans += 1
  else:
    point += 1
if point == 1:
  print("-1")
else:
  print(ans)

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

    cnt =  1
    flag = False
    for i in a_list:
        if i == cnt:
            cnt+=1
            flag = True
            continue

    ans = n - cnt + 1
    if not flag:
        ans = -1
    print(ans)

main()

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

i = 1
count = 0
for e  in blocks:
    if e == i:
        count += 1
        i += 1

if count == 0:
    print(-1)
else:
    print(n-count)

Solution (Python):

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


if not 1 in alist:
    print(-1)

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

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

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

Solution (Python):


renga_count = int(input())
renga_list = [int(i) for i in input().rstrip().split(" ")]

count = 0
for i in renga_list:
    if i == count + 1:
        count += 1

if count == 0:
    print(-1)
else:
    print(renga_count - count)

Solution (Python):
_ = input()
l = list(map(int, input().split()))
br = 0
cn = 1
for i, n in enumerate(l):
	
	if n != cn:
		br += 1
	else:
		cn += 1
if br == len(l):
	print(-1)
else:
	print(br)

Solution (Python):
from functools import reduce
N = int(input())
a = tuple(map(int, input().split()))
b = reduce(lambda i, x: i + 1 if i == x else i, a, 1)
print((N - b + 1) if b > 1 else -1)

Solution (Python):
N = int(input())
nums = [int(s) for s in input().split()]
 
min_breaks = -1
 
count_num = 1
break_index = 1

for i in range(N):
  if nums[i] == count_num:
      if min_breaks == -1:
          min_breaks = 0
      if count_num == 1:
      	 min_breaks += ((i+1) - break_index)
      else:
         min_breaks += ((i+1) - break_index - 1)
      
      break_index = (i+1)
      count_num+=1

if break_index < N and min_breaks != -1:
  min_breaks += ((i+1) - break_index)
  
print(min_breaks)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
x=1
for a in A:
    if a==x:
        x+=1
    else:
        pass
if x==1:
    print(-1)
else:
    print(N-x+1)

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(N: int, a: "List[int]"):
    target = 1
    ret = 0
    for v in a:
        if v != target:
            ret += 1
        else:
            target += 1
    if target == 1:
        ret = -1
    print(ret)
    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):
import sys
input = sys.stdin.readline

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

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

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

Solution (Python):
def main():
  n = int(input())
  a = [int(s) - 1 for s in input().split()]
  res = 0
  for i in range(n):
    if a[i] != (i-res):
      res += 1
  if res == n:
    print(-1)
  else:
    print(res)
    
if __name__ == '__main__':
  main()

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

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

Solution (Python):
n1 = int(input())
n2 = [int(i) for i in input().split()]

t3 = 0

for i4 in range(n1):
    if n2[i4] != i4 - t3 + 1:
        t3 += 1



if t3 == n1:
    print(-1)
else:
    print(t3)

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

Solution (Python):
N = int(input())
an = list(map(int, input().split()))
enable = 0

if not 1 in an:
  enable = 1
  
count = 0
block = 1
for i in range(N):
  if an[i] != block:
    count += 1
  else:
    block += 1
    
if enable == 1:    
  print(-1)
else:
  print(count)

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
a = nl()

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

if(cnt==0):
  print(-1)
  exit()

print(shift)

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

ans = 0
block = 1
no_block = False

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

if no_block is False:
    print(-1)
else:
    print(ans)

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

ans = 0
current = 1
for i in range(n):
    if a[i] != current:
        ans += 1
        continue

    current += 1

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

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

now = 0
for x in a:
    if  x == now + 1:
        now += 1
if now == 0:
    print(-1)
else:
    print(n - now)

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

index = 1
ans = 0
found = False
for i in range(N):
    if A[i] == index:
        found = True
        index += 1
    else:
        ans += 1

print(ans if found else -1)

Solution (Python):

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

cnt = 0
s = 1
for a in al:
    if a==s:
        s += 1
    else:
        cnt += 1

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

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

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

Solution (Python):
A = int(input())
B = list(map(int,input().split(" ")))
memory = 1
for i in range(A):
    if B[i] == memory:
        memory += 1
    else:
        continue
if A-(memory-1) == A:
    print("-1")
else:
    print(A-(memory-1))

Solution (Python):

import sys

read = sys.stdin.buffer.read


def main():
    N, *A = map(int, read().split())
    remaining = 0
    target = 1
    for i in A:
        if i == target:
            remaining += 1
            target += 1
    print(N - remaining if remaining else -1)


if __name__ == "__main__":
    main()