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.



Sample input:
3
2 1 2


Sample output:
1

Submission status: Accepted

Solution code:
N = int(input())
s1 = [int(x) for x in input().split()]
Ans = 0

for i in range(N):
    if Ans+1 == s1[i]:
        Ans += 1

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

Submission status: Accepted

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

t = 1
ans=0

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

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

if t==ans:
    print(ans-1)

else:
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = map(int, input().split())
ans = 0

for i in range(1, n):
  for a in A:
    if i == a:
      i += 1
      break
    else:
      ans += 1

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
num = 1
count = 0
for i in a:
    if i == num:
        num += 1
    else:
        count += 1

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
k = 1

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

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

Submission status: Accepted

Solution code:
import sys

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

try:
  bricks.index(1)
except:
  print(-1)
  sys.exit()

result = 0
numbers = []
watch = 1
for i in range(n):
  if bricks[i] == watch:
    watch = watch + 1
  else:
    result = result + 1

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    counter = 0
    for a in A:
        if a == counter + 1:
            counter += 1
    if counter == 0:
        ans = -1
    else:
        ans = N - counter
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    N = int(input())
    a_l = [int(i) for i in input().split()]
    if 1 not in a_l:
        print(-1)
        sys.exit()
    counter = 1
    b = 0
    for i in a_l:
        if i == counter:
            counter += 1
        else:
            b += 1
    print(b)
    sys.exit()


if __name__ == "__main__":
    main()

Submission status: Accepted

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

t = 1
b = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


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

t = 1
ans = 0

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=0
#n=200000
if 1 not in a:
  print(-1);exit()
from collections import*  
b=deque(a)
#print(b)
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(b,cnt)     

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# abc148_d.py
from sys import stdin
N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
#print(a)
cnt = 0
idx = 1
#flg = 1
for i in a:
	if i == idx:
		idx = idx + 1
		#flg = 0
	else:
		cnt = cnt + 1
		#if idx != 1:
			#flg = 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import itertools#これは色々使える組み込み関数みたいなやつ
#import math#数学的計算はこれでいける。普通に0.5乗しても計算可能
#w=input()
i=0
N=int(input())
#N, L=input().split()
#S=input()
#T=input()
#S_list=list(S)
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)



#for i in range(N):
# print(i,j,times, a_list)

 #if a_list[j]==j+1:
#     j=j+1
#     if j==len(a_list):
#         print(times)
#         sys.exit()

 #if a_list[i]==i+1:
 #i=j
 #print(i, a_list)
 #if i+1==len(a_list):
#    print(times)
#    sys.exit()
# if a_list[j]!=j+1:
#     a_list.pop(j)
     #j=i
     #i=j
#     times+=1
#     if len(a_list)==0:
#      print(-1)
#      sys.exit()
     #elif j+1==len(a_list):
     #print(times)
    #     sys.exit()

#     elif
#print(times)
#

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = [int(x) for x in input().split(" ")]

# j 番目からリストの最後までの間で x があるか確かめ、ある場合は最小のインデックスを返し、ない場合は "n" を返す
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])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


count = 0
c = 1

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
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)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
# a = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]

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)

Submission status: Accepted

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

#min_indexlist = [a_list.index(min(a_list))]

min_indexlist = []

m = min(a_list)

#print(m)

#ここがおかしい
"""
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)



#print(min_indexlist)

#砕いたレンガの後のリスト
crash_list = []
for i in range(len(min_indexlist)):
    crash_list.append(a_list[min_indexlist[i]])

#print(crash_list)

#砕いたレンガの数
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
N = int(input())
a_list = list(map(int, input().split())) #[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 1]

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

#idx = 0
value = 1

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

print(len(a_list) - value)

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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()))
# from math import gcd
# from collections import defaultdict as dd, Counter
# from bisect import bisect_left as bl, bisect_right as br

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    #print(a)
    a_org = a.copy()
    match_cnt = 1
    pop_cnt = 0
    for i in range(N):
        #print(a_org[i], match_cnt)
        if a_org[i] != match_cnt:
            #a.pop(i)
            pop_cnt = pop_cnt + 1
        else:
            match_cnt = match_cnt + 1
            #print('i: ', i, a_org[i])
    
    if pop_cnt == N:
        pop_cnt = -1
    print(pop_cnt)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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
 
#print(a)
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))

Submission status: Accepted

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

Submission status: Accepted

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