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

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    ans = 0
    n = 1
    for a in A:
        if a == n:
            n += 1
    if n == 1:
        return -1
    return N - n + 1


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

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

tmp = 0

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

Solution (Python):


from collections import deque, defaultdict
import itertools as ite
import sys
import math
import decimal

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
a = map(int, raw_input().split())
cnt = 1
for num in a:
    if num == cnt:
        cnt += 1
if cnt == 1:
    print -1
else:
    print N - cnt + 1

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 1 << 100
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = LI()


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

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

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

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

Solution (Python):
import sys
input=sys.stdin.readline
n=int(input())
A=[int(i) for i in input().split()]
ans=0
for i in range(n):
  if A[i]==ans+1:
    ans+=1
print(-1 if ans==0 else n-ans)

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

Solution (Python):
n,*a=map(int,open(0).read().split())
if 1 not in a:
	print(-1)
	exit()
cur=0
for x in a:
	if cur+1 ==x:
		cur+=1
print(n-cur)

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
c = 0
for a in A:
    if a == c + 1:
        c = a

print(N - c if c else -1)

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
def ii(): return int(readline())
def mi(): return map(int, readline().rstrip().split())
def li(): return list(readline().rstrip())
def lmi(): return list(map(int, readline().rstrip().split()))
def end(*arg): print(*arg); sys.exit()



def main():
    n = ii()
    a = lmi()
    i = 0
    tmp = 1
    while i < n:
        if a[i] == tmp:
            tmp += 1
            i += 1
        else:
            i += 1
    if(tmp == 1):
        print(-1)
    else:
        print(n - (tmp - 1))
    return


if __name__ == '__main__':
    main()

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  ans=0
  ind=1
  for x in l:
    if x==ind:
      ind+=1
    else:
      ans+=1

  if ans==n:
    return -1
  return ans


print(main())

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

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

Solution (Python):

import sys
input = sys.stdin.readline

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

ans = 0
prev = 0
for item in a:
    if item != prev + 1:
        ans += 1
    else:
        prev += 1
if ans == n:
    print(-1)
else:
    print(ans)

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

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

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

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
now = 1
res = 0
for i in a:
    if i == now:
        now += 1
    else:
        res += 1
if now == 1:
    print(-1)
else:
    print(res)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))

    now=0
    for a in A:
        if(a==now+1):
            now+=1

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

Solution (Python):

import sys
INF = float("inf")


def solve(N: int, a: "List[int]"):
    counter = 0
    current = 1
    for v in a:
        if v != current:
            counter += 1
        else:
            current += 1
    if counter == N:
        print(-1)
    else:
        print(counter)

    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):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    j = 1
    for i in range(n):
        if a[i] == j:
            j += 1
    if n-j+1 == n:
        print(-1)
    else:
        print(n-j+1)


main()

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque, Counter
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math
INF = 10 ** 9

def main():
    n = getN()
    nums = getList()
    tmp = 1
    br = n
    for num in (nums):
        
        if num == tmp:
            br -= 1
            tmp += 1
            



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

if __name__ == "__main__":
    main()

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

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

Solution (Python):
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()
if 1 not in ls:
    print(-1)
    exit()
res = 0
prev = 0
for x in ls:
    if x == prev + 1:
        prev = x
    else:
        res += 1
print(res)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    need = 1
    for a in A:
        if a==need:
            need+=1


    if need!=1:
        print(N-need+1)
    else:
        print(-1)
if __name__ == '__main__':
    main()

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


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

now=0

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

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

Solution (Python):

N = int(input())

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

ans = N

now = 1
for i in range(N):

    if a[i] == now:
        ans -= 1
        now += 1

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

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


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

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

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

ind = 1
ans = 0
flag = False
for i in range(n):
    if arr[i] != ind:
        ans += 1
    else:
        ind += 1
        flag = True

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

import sys
from collections import defaultdict, deque
from operator import itemgetter
from heapq import heappop, heappush

def inpl(): return list(map(int, input().split()))
MOD = 10**9 + 7

N = int(input())
A = inpl()
ans = 0
x = 1
for a in A:
    if x == a:
        x += 1
    else:
        ans += 1
if (ans == N):
    print(-1)
else:
    print(ans)

Solution (Python):
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N = int(sys.stdin.buffer.readline())
A = list(map(int, sys.stdin.buffer.readline().split()))

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

p = 0
ans = 0
for i in range(1, N + 1):
    while p < N and A[p] != i:
        p += 1
        ans += 1
    p += 1
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): 
    return int(input())
def inpm(): 
    return map(int,input().split())
def inpl(): 
    return list(map(int, input().split()))
def inpls(): 
    return list(input().split())
def inplm(n): 
    return list(int(input()) for _ in range(n))
def inpll(n): 
    return [list(map(int, input().split())) for _ in range(n)]

def main():
    n=inp()
    a=inpl()
    ans = 0
    pre = 1
    for i in range(n):
        if a[i]==pre:
            pre+=1
            ans+=1
    if ans==0:
        print(-1)
        return
    print(n-ans)


if __name__ == "__main__":
    main()

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10000000)







INF = 500000
MOD = 10**9+7

n = int(input())
a = [int(x) for x in input().split()]
if 1 not in a:
    print(-1)
    exit()
i, k = 0, 1
cnt = 0
while i < n:
    while i < n:
        if a[i] == k:
            cnt += 1
            i += 1
            k += 1
            break
        i += 1
print(n-cnt)

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

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

if brake == n:
    brake = -1
print(brake)

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

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

i = 0
a = 1
while True:
    try:
        j = aaa.index(a, i)
    except ValueError:
        break
    i = j + 1
    a += 1

print(n - a + 1)

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 >= 2:
    print(n - target + 1)
else:
    print(-1)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
minans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
A=list(map(int,input().split()))
k=1
for i ,ai in enumerate(A):
    if ai!=k:
        count+=1
    else:
        k+=1
if count==n:
    print(-1)
else:
    print(count)

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

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    k = 1
    ans = 0
    for i in range(n):
        if a[i] != k:
            ans += 1
        else:
            k += 1
    if k == 1:
        print(-1)
        return
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()

input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True


ans=0

n=I()
a=LI()
x=0
c=1
for i in a:
    if i==c:
        x+=1
        c+=1
    
ans=n-x
if c==1:
    ans=-1
print(ans)

Solution (Python):
import itertools, heapq, bisect
from collections import defaultdict as ddic, deque, Counter

rr = raw_input
rri = lambda: int(raw_input())
rrm = lambda: map(int, raw_input().split())
MOD = 10 ** 9 + 7


N = rri()
A = rrm()

cur = 1
ans = 0
for x in A:
    if x == cur:
        cur += 1
        continue
    else:
        ans += 1
print ans if ans < N else -1