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
num = 1
for i in range(n):
    if a[i] != num:
        ans += 1
    else:
        num += 1
if num == 1:
    print(-1)
else:
    print(ans)

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

N=int(input().rstrip('\n'))
A=[int(x) for x in input().rstrip('\n').split()]
now = 0
for x in range(N):
  if A[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()))
cnt=1
ans=0
for i in range(len(A)):
    if A[i]==cnt:
        cnt+=1
    else:
        ans+=1
if ans==len(A):
    print(-1)
else:
    print(ans)

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

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

target = 1
cnt = 0

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

Solution (Python):
if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    ans = 0
    cnt = 1
    
    if 1 not in a:
        print(-1)
    else:
        for i in a:
            if i == cnt:
                cnt += 1
            else:
                ans += 1
        print(ans)

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

Solution (Python):
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    a_s = set(a)
    cnt = 0
    order = 1
    if not 1 in a_s:
        print(-1)
        exit()
    else:
        for i in range(len(a)):
            if a[i] != order:
                cnt += 1
            if a[i] == order:
                order += 1
    print(cnt)

if __name__ == '__main__':
    main()

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

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

Solution (Python):

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

t = 1
for x in a:
    if x == t:
        t += 1
t -= 1

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

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

Solution (Python):
import sys
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]

from collections import deque
N = ri()
a = deque(ri_())
i = 0
cnt = 0
if not 1 in a:
    print(-1)
    exit()
while i < N:
    if a[i] == i + 1:
        i += 1
    else:
        a.popleft()
        N -= 1
        cnt += 1
print(cnt)

Solution (Python):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))
ans = 0
j = 1
for i in range(N):
    if A[i] == j:
        j+=1
    else:
    	ans += 1
if j == 1:
    print(-1)
else:
    print(ans)

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

Solution (Python):
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

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

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

Solution (Python):
n = int(input())
la = [int(w) for w in input().split()]

ans = 0
i = 1
for a in la:
    if a != i:
        ans += 1
    else:
        i += 1
if ans == n and i == 1:
    ans = -1

print(ans)

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

cnt = 1
ans = 0
for a in alist:
    if a != cnt:
        ans += 1
    else:
        cnt += 1

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

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

n, s, ind, res = int(input()), list(map(int, input().split())), 1, 0
for i in s:
    if i == ind: ind += 1
    else: res += 1
print(-1 if res == n else res)

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

Solution (Python):

n, *a = map(int, open(0).read().split())
c = 1
for i in a:
    if i == c:
        c += 1
print(-1*(c==1) or n - c + 1)

Solution (Python):









import sys
input = sys.stdin.readline

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

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

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

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

x = 1
ans = 0

if not 1 in a:
    ans = -1
else:
    for i, item in enumerate(a):
        if item == x:
            x += 1
        else:
            ans += 1

print(ans)

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

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

cnt = 0

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

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

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

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
c = 0
ans = -1
for num in a:
    if num==c+1:
        c = num
    else:
        pass

if c>0:
    ans = max(ans, c)
if ans>=0:
    print(n-ans)
else:
    print(-1)

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

N = k()
a = l()

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

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

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

Solution (Python):
from sys import exit

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

break_count = 0

for key, a in enumerate(A, 1):
    if not a == key - break_count:
        break_count += 1

if break_count == N:
    print(-1)

else:
    print(break_count)

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

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

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

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

Solution (Python):
N, *A = map(int, open(0).read().split())

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

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

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

Solution (Python):


import copy
import random
import bisect 
import fractions 
import math
import sys
import collections
from decimal import Decimal 

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
a = LI()

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

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

count = 1
if 1 not in a:
    print(-1)
    exit()
for i in range(N):
    if count == a[i]: count += 1
print(N-count+1)

Solution (Python):

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

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

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

ans=0
i=0
j=1
while i<N:
    if A[i]==j:
        j+=1
    else:
        ans+=1
    i+=1

print([-1,ans][1 in A])

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

i = 1
for a in A:
    if a == i:
        i += 1
if i == 1:
    print('-1')
else:
    count = i-1
    print(N-count)

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

Solution (Python):
n = int(input())
alst = list(map(int, input().split()))
if 1 not in alst:
  print(-1)
  exit()

ans = 0
for num in alst:
    if num == ans + 1:
        ans += 1
else:
    print(n - ans)

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

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

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

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

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

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

I=0
for a in A:
    if a==I+1:
        I+=1

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

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

counter = 1
ans = 0

for j in l:
    if j == counter:
        counter += 1
    else:
        ans += 1

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

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

length = len(A)
find_next = 1

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

if find_next == 1:
    print(-1)
else:
    print(length - find_next + 1)

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

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

Solution (Python):
class Stack:
  def __init__(self):
    self.count = 1
    self.lst = []
  
  def push(self, element):
    if element == self.count:
      self.lst.append(element)
      self.count += 1
  
  def length(self):
    return len(self.lst)

n = int(input())
a = list(map(int, input().split()))
stack = Stack()
for i in a:
  stack.push(i)

b = stack.length()
if b == 0:
  print('-1')
else:
  print(n - b)

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

count=0
num=1
for i in range(n):
  if A[i] == num:
    count += 1
    num += 1
if count == 0:
  print('-1')
  exit()
print(n-count)

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

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

    if n == 1:
        print(-1)
    else:
        print(N - n + 1)
    return


if __name__ == '__main__':
    main()

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

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

print(ans if ans < len(a) else -1)

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

i=1
for a in alst:
    if a==i:
        i+=1
i-=1

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

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

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

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

Solution (Python):
import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
m = 1
for i in a:
    if i == m:
        m += 1
if m == 1:
    print(-1)
    exit(0)
print(1+n-m)

Solution (Python):
n = int(input())
an = list(map(int, input().split()))
num = 1
for i in an:
    if i == num:
        num +=1
if num == 1:
    print(-1)
    exit(0)
ans = 1+n-num
print(ans)

Solution (Python):

n=int(input())
v=list(map(int,input().split()))
if 1 not in v:
    print(-1)
else:
    ans=0
    num=1
    for i,e in enumerate(v):
        if e==num:
            num+=1
            ans+=1
            
            
    print(n-ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
ls = []
for i, x in enumerate(A):
    ls.append([i+1, x])
ls.sort(key=lambda x: x[1])
cur = 1
num = 0
ans = 0
s = set()
for x, y in ls:
    if cur == y and y not in s and num < x:
        cur += 1
        num = x
        s.add(y)
    else:
        ans += 1
if ans == N:
    print(-1)
else:
    print(ans)

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

check = 1
ans = 0


for i in a:
  ans+=1
  if i == check:
    ans -=1
    check+=1
    
if a==[j for j in range(1,N+1)]:
  print(0)
  exit()
  
elif ans==N:
  print(-1)
  exit()

print(ans)

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

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

if 1 in t:
    ans = 0
else:
    print(-1)
    exit()

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

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
if 1 not in A:
  print(-1)
  exit()
cnt = 0
s = 1
for a in A:
  if a == s:
    cnt += 1
    s+=1
print(n-cnt)

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

pos_num = 1
count = 0
for num in bricks:
    if num == pos_num:
        pos_num += 1
    else:
        count += 1

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

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

num = 1
a = 0

for i in list:
  if i == num:
    num = num +1
  else:
    a = a + 1

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

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

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

ans = 0
t = 1
s = []
for i in a:
  if i != t: ans += 1
  else:
    t += 1
    s.append(i)

if s == []:
  print(-1)
else:
  print(ans)

Solution (Python):
def resolve():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = [int(i) for i in input().split()]
    if 1 not in A:
        print(-1)
        return
    sumA = 0
    try:
        i = 1
        while sumA + i <= N:
            sumA += A.index(i, (sumA + i - 1)) - (sumA + i - 1)
            i += 1
        print(sumA)
    except BaseException:
        sumA += N - (sumA + i - 1)
        print(sumA)


resolve()

Solution (Python):








def solve(N, As):
    ans = 0
    i = 1
    for A in As:
        if A == i:
            i += 1
        else:
            ans += 1
    ans = -1 if ans == N else ans
    print(ans)


if __name__ == '__main__':
    
    N = int(input())
    
    As = [int(i) for i in input().split()]
    
    solve(N, As)

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 + 1 -count)

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 + 1 -count)

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

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

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

for i in a:
    if i == num:
        num += 1
    else:
        ans += 1

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

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

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

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

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

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

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

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

count = 0
last_i = 0
temp = 1
t = 0

if 1 not in set(A_n):
    print('-1')
else:
    for a in A_n:
        if a == temp:
            temp += 1
        else:
            count += 1
    print(count)

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

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


tmp = 1
counter = 0
for a in A:
    if a==tmp:
        counter += 1
        tmp += 1

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

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

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

ans = n-count+1

if 1 not in a:
    ans = -1

print(ans)

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

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

idx = 1
ans = 0
length = len(A)
lis = []
for a in A:
  if a == idx:
    lis.append(idx)
    idx += 1
    continue
  else:
    ans += 1
    length -= 1

if lis != [] and lis == list(range(1, length+1)):
  print(ans)
else:
  print(-1)

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

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

    if a.count(1) == 0:
        print(-1)
        return
    index = 1
    cnt = 0
    for i in range(N):
        if index == a[i]:
            index+=1
        else:
            cnt+=1

    print(cnt)






if __name__ == '__main__':
    main()

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

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

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

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

Solution (Python):
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline

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

    target = 1
    for i in A:
        if i == target:
            target += 1
    
    if target == 1:
        print(-1)
        return

    print(N - target + 1)


main()

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

if 1 not in lis:
    cnt = -1
else:
    for idx, val in enumerate(lis):
        if val != idx+1-cnt:
            cnt += 1

print(cnt)

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

target, point = 0, 0
ans = 0
for i in range(n):
    if a[i] == target:
        ans += i - point
        point = i + 1
        target += 1

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

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

ans = 0
cnt = 1
while A:
    a = A.popleft()
    if cnt == a:
        cnt += 1
        continue
    else:
        ans += 1
if ans == N:
    print(-1)
else:
    print(ans)

Solution (Python):






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





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

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

isOKs = [False] * (N+1)
isOKs[0] = True
for A in As:
    if isOKs[A-1]:
        isOKs[A] = True

for A in reversed(range(1, N+1)):
    if isOKs[A]:
        print(N-A)
        break
else:
    print(-1)

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

count=0
k=1
for i in range(len(A)):
    if A[i]==k:
        count+=1
        k+=1

if count==0:
    print(-1)
else:
    print(len(A)-count)

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

f=0
for a in A:
    if a==f+1:
        f+=1

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

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

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

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

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

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

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

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

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

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

ans, cnt = 0, 1
for i in a:
    if i == cnt:
        cnt += 1
    else:
        ans += 1
print(-1 if ans == n else ans)

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

Solution (Python):
def resolve():
    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 ans == N:
        print(-1)
        return
    print(ans)

    return
resolve()

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

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

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

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

main()

Solution (Python):
import sys
import bisect

N = int(input())
a = list(map(int,input().split()))
numdic = {}
for i, num in enumerate(a):
    if num not in numdic:
        numdic[num] = [i+1]
    else:
        numdic[num].append(i+1)

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

nowplace = 0
count = 0
for i in range(1,N+1):
    if i not in numdic:
        break
    else:
        place = bisect.bisect_right(numdic[i], nowplace)
        if place >= len(numdic[i]):
            break
        else:
            nowplace = numdic[i][place]
            count += 1

print(N-count)

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

k = 1
cnt = 0
for a in A:
    if a == k:
        cnt += 1
        k += 1

ans = -1 if k == 1 else N - cnt
print(ans)

Solution (Python):


from sys import stdin
import fractions



N = int(stdin.readline().strip())
a = list(map(int, stdin.readline().strip().split()))

rem = 0
ne = 1
ok = False

for ai in a:
  
  if ai==ne:
    ne += 1
    ok = True
  else:
    rem += 1

if ok:
  print(rem)
else:
  print("-1")

Solution (Python):

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

Solution (Python):

"""
D - Brick Break
https://atcoder.jp/contests/abc148/tasks/abc148_d

"""
import sys


def solve(N, arr):
    x = 1
    last = -1
    ans = 0
    for a in arr:
        if a != x:
            ans += 1
        else:
            x += 1
            last = 1
    return ans if last != -1 else -1


def main(args):
    N = int(input())
    arr = [int(a) for a in input().split()]
    ans = solve(N, arr)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
import sys

N = int(input())
a = list(map(int, input().split()))
ans = 0 
count = 1
if 1 not in a :
    print(-1)
    sys.exit()

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

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

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

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

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

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

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

if all([i == -1 for i in a]):
  print(-1)
else:
  print(ans)

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

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

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


if __name__ == '__main__':
    q_d()

Solution (Python):
num = int(input())
input_line = list(map(int,input().split(' ')))

bb = 0
bi = 0

for i in input_line:
    if(i==(bi+1)):
        bi += 1
    else:
        bb += 1
if(bi>0):
    print(bb)
else:
    print(-1)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
cur=1
for i in range(N):
    if A[i]==cur:
        cur+=1
if cur==1:
    print(-1)
elif cur>1:
    print(N-(cur-1))

Solution (Python):
import numpy as np
N = int(input())
L = np.array(input().split(),dtype = int)

m = min(N,max(L))

r = np.array(np.linspace(1,m,m),int)


dp = [0 for i in range(N+1)]

for i in range(0,N):
    if dp[i]+1 ==L[i]:
        dp[i+1] = dp[i]+1
    else:
        dp[i+1] = dp[i]

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

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

count = 0
target = 1

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

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

print(count)

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

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

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

target = 1

broken = 0

for i in a:
    if i == target:
        target += 1
    else:
        broken += 1

if broken < n:
    print(broken)
else:
    print(-1)

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

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

print(ans if ans != n else -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
if count==0:
  print(-1)
else:  
  print(N-count)

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

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

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

Solution (Python):
import sys
n = int(input())
a = [int(i) for i in input().split()]
if not 1 in a:
    print(-1)
    sys.exit()
target = 1
count = 0
for i in a:
    if i == target:
        target += 1
    else:
        count += 1
print(count)