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:
import sys

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

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

res = 0
j = 1

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

print(res)

Submission status: Accepted

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

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

Submission status: Accepted

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

main()

Submission status: Accepted

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

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

flag = False

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

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(map(int, input().split()))
cur = 1
flag = 0

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

Submission status: Accepted

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

ans = -1
tmp_nums = []
check_num = 1

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
a = list(map(int, input().split()))

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

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

def check(n):
  for i in range(N):
    if a[-1-i] == n:
      n -= 1
  return n == 0

r = N
left = 1
right = N
for _ in range(N):
  if (right - left) <= 2:
    if check(right):
      r = right
      break
    elif check(right-1):
      r = right - 1
      break
    else:
      r = right - 2
      break
  t = (left + right) // 2
  if check(t):
    left = t
  else:
    right = t

print(N-r)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import deque

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

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

Submission status: Accepted

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

num=0

j=0

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

    if j==N:
        break

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

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

Submission status: Accepted

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

point = 1
count = 0
ans = -1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
#        for line in sys.stdin:
#            lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    n = [int(e) for e in lines[0].split(" ")][0]
    a = [int(e) for e in lines[1].split(" ")]

    return (n, a)

def solve(n, a):

    current_num = 1
    count = 0
    result = []
    for i in range(n):
        if a[i] != current_num:
            count = count + 1
        else:
            current_num = current_num + 1
            result.append(a[i])
    
    if debug:
        log("result=%s" % result)
        log("count=%s" % count)
    
    if result == []:
        count = -1

    for i in range(1, len(result) + 1):
        if result[i-1] != i:
            if debug:
                log("result[%d]=%s" % (i, result[i-1]))
            count = -1
            break


    return count

def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r)
    else:
        print("%s" % result)

if __name__ == '__main__':

    main()

Submission status: Accepted

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

c = 1
for i, v in enumerate(a):
    if v == c:
        c += 1

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc148/tasks/abc148_d


def main():
    num = int(input())
    origin = [int(i) for i in input().split(" ")]
    break_count = 0
    want = 1
    satisfy = False
    for b in origin:
        if b == want:
            want += 1
            satisfy = True
            continue
        else:
            break_count += 1
    print("-1" if not satisfy else break_count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
number=input().split(' ')
number_list=[]
for i in number:
    number_list.append(int(i))
index=1
flag=0
count=0
for i in number_list:
    if(i==1 and flag==0):
        flag=1
        index+=1
    elif (i==index):
        index+=1
    else:
        count+=1

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

Submission status: Accepted

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


#print(a)

ref1 = 1 #  
count = 1
ans = 0
while(count <= N):
    if a[count-1] != ref1: # 一致しないとき
        count += 1 # リスト値の引き上げ
        ans += 1
        #print("check",count,"???",ans)
    else: # 一致するとき
        ref1 += 1 # 基準値の引き上げ
        count += 1
        #print("check2",ref1)
print(ans if ans < N else -1)

Submission status: Accepted

Solution code:
num, bricks = int(input()), list(map(int,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:
a=int(input())
q=[]
q+=[int(x) for x in input().split()]
s=q.count(1)
r=1
e=0
if s==0:
  print("-1")
else:
  for i in range(a):
    if r==q[i]:
      r+=1
  print(a-r+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from __future__ import print_function

import sys
input = sys.stdin.readline

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)
    return

# import math
# import string
# import fractions
# from fractions import Fraction
# from fractions import gcd

# def lcm(n,m):
#     return int(n*m/gcd(n,m))

# import re
# import array
# import copy
# import functools
# import operator

# import collections
# import itertools
# import bisect
# import heapq


# from heapq import heappush
# from heapq import heappop
# from heapq import heappushpop
# from heapq import heapify
# from heapq import heapreplace

# from queue import PriorityQueue as pq

# def reduce(p, q):
#     common = fractions.gcd(p, q)
#     return (p//common , q//common )
# # from itertools import accumulate
# # from collections import deque

# from operator import mul
# from functools import reduce

# def combinations_count(n, r):
#     r = min(r, n - r)
#     numer = reduce(mul, range(n, n - r, -1), 1)
#     denom = reduce(mul, range(1, r + 1), 1)
#     return numer // denom

# import random



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

    cnt=0
    j=1
    for i in range(n):
        if l[i]==j:
            j+=1
            cnt+=1
            pass
        else:
            pass
    if cnt==0:
        print(-1)
    else:
        print(n-cnt)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

count = 0
tmpNumber = 1

for idx in range(N):
    if (a[idx] != tmpNumber):
      	count += 1
    else:
        tmpNumber += 1
if (tmpNumber == 1):
    print(-1)
else:
    print(count)

Submission status: Accepted

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

count = 0
value = 1
for i in range(0, len(array)):
    if (value == array[i]):
        value += 1
    elif (value != array[i]):
        count += 1

if (value == 1):
    print(-1)
    exit()
print(count)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a_array = list(map(int,input().split()))

cnt = 0
now = 1
for i in range(N):
    if a_array[i] > now or a_array[i] < now:
        cnt += 1
    else:
        now += 1
if cnt == N:
    cnt = -1
print(cnt)

Submission status: Accepted

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

i = 0

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

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

Submission status: Accepted

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

num = 0
j = 1

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

print(num)

Submission status: Accepted

Solution code:
N = int(input())
Input = input().split()

Inputint = [int(i) for i in Input]

new = []
COUNTER = 1
for i in Inputint:
    if COUNTER == 1:
        if i == 1:
            new.append(i)
            COUNTER += 1
    else:
        if i == new[-1]+1:
            new.append(i)
        else:
            continue

if not new:
    print(-1)
else:
    print(N-len(new))

Submission status: Accepted

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

counter = 0
breaknum = 0

flag = False

for i in list(aList):
    if(i == counter+1):
        counter += 1
    else:
        breaknum += 1
    
if(counter == 0):
    print(-1)
else:
    print(breaknum)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
b = 1
anc = 0
roop = 0
if 1 in a:
    for i,v in enumerate(a):
        if v == b:
            anc += roop
            temp = i
            b+=1
            #print('roop',roop)
            roop = 0
            #print('b',b)
            #print('temp',temp)
            #print('anc',anc)

        else:
            roop +=1
            if i == n-1:
                
                anc += roop
    print(anc)
else:

    print(-1)

Submission status: Accepted

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

is_over = False
now = 0

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

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

Submission status: Accepted

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

dp = [0]*N

if a[0] == 1:
    dp[0] = 1

for i in range(N-1):
#    print i, a[i+1], dp[i]                                                                            
    if a[i+1] == dp[i] + 1:
        dp[i+1] = dp[i] + 1
    else:
        dp[i+1] = dp[i]


ans = N - dp[-1]
if ans > N -1:
    print "-1"
else:
    print ans

Submission status: Accepted

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

def cal(i):
    c = 1
    ans = 0
    for j in range(n):
        if i[j] == c:
            c += 1
        else:
            ans += 1

    if c == 1:
        return -1
    else:
        return ans

print(cal(i))

Submission status: Accepted

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

# 無理な場合は-1

# 1からN-1が出てくるまでの個数を足していく？
idx = 0
cnt = 0
num = 1
brick = 0
flg = False
while True:
    if num == A[idx]:
        flg = True
        num += 1
        brick += cnt
        cnt = 0
    else:
        cnt += 1
    idx += 1
    if idx > N - 1:
        brick += cnt
        break

if flg == False:
    brick = -1

print(brick)

Submission status: Accepted

Solution code:
# 21:19
from collections import deque
n = int(input())
a = deque(list(map(int,input().split())))
ans = 0
count = 1
i = 0
while True:
    if a[i] != count:
        ans += 1
        a.popleft()
    else:
        count += 1
        i += 1
    if n == ans + i:
        break
if len(a) == 0:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

x = 1
b = 0
for i in a:
    if i != x:
        b += 1
    elif i == x:
        x += 1

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

Submission status: Accepted

Solution code:

n = int(input())
a =[int(i) for i in input().split()]
j = 1
ans = 0
k =0
l = -1
if 1 not in a:
	ans = -1
else:
		for i in range(k,n):
			if k>=n:
				break
			elif a[i] == j:
				ans+=i-k
				k = i+1
				j +=1
				l = i

if l!=-1:
	ans+=n-1-l
print(ans)
"""
k  =1
j =0
for  i in range(k,10):
		if i !=2:
			print(i)
		else:
			k = 3
			break
		j +=1
"""

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

answer = N - n + 1
if answer == N:
    answer = -1
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
num_list = [int(i) for i in input().split()]

count = 0
match = 1

for i in range(N):
    if num_list[i] == match:
        match += 1
    else:
        count += 1

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

Submission status: Accepted

Solution code:
N, *L,  = [list(map(int, s.split())) for s in open(0)]
L = L[0]
minj = 0
for l in L:
    if l == minj + 1:
        minj = l
if minj == 0:
    print(-1)
else:
    print(len(L) - minj)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    n = int(input()[:-1])
    l = list(map(int, input()[:-1].split(" ")))

    c = 1
    s = 0
    tmp = 0
    while l:
        if tmp >= n:
            break
        if l[tmp] != c :
            s += 1
        else:
            c += 1
        tmp += 1

    res = s
    if s == n:
        res = -1

    print(res)

main()

Submission status: Accepted

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

cnt = 0
n = 1

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

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

print(cnt)

Submission status: Accepted

Solution code:
#problemD: D - Brick Break
N = int(input())
a =  list(map(int, input().split()))

def counter(a):
    count = 0
    i = 1
    for row in a:
        if row != i:
            count = count + 1
            i = i
        else:
            count  = count
            i = i + 1
    return count

if 1 in a:
    print(counter(a))
else:
    print('-1')

Submission status: Accepted

Solution code:
import sys

length = int(input())
bricks = input().split(' ')

counter = 0
top = 0
for i, brick in enumerate(bricks):
    if top+1 == int(brick):
        top += 1
    else:
        counter += 1
if top == 0:
    counter = -1
print(counter)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().strip().split(' ')))

ans = 0
by = 1

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

Submission status: Accepted

Solution code:
n = int(input())
li = [int(i) for i in input().split()]
a = 1
ans = 0
for i in range(n):
  if li[i] != a:
    ans += 1
  else:
    a += 1

if ans == n:
  print(-1)
elif ans == 0 and a == n+1:
  print(0)
else:
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
renga = tuple(map(int, input().split()))

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())

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


myans = 0
mynum = 1

i = 0 
while i < N:
    
    if mylist[i] == mynum:
        mynum += 1
        
    else:
        myans += 1
        
    i += 1

if myans != N:
    print(myans)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
A = map(int, input().split())
B = []
cnt = 0

for a in A:
    if a == len(B)+1:
        B.append(a)
    else:
        cnt += 1

if len(B) != 0 and B[-1] == N-cnt:
    print(cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
k = 1
ans = 0
for i in range(n):
  if a[i] == k:
    k += 1
  else:
    ans += 1
if ans == n:
  print("-1")
  exit()
print(ans)

Submission status: Accepted

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

pos_cnt = 1
remove_cnt = 0
for d in data:
    if d == pos_cnt:
        pos_cnt += 1
    else:
        remove_cnt += 1

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

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
cnt=0
i=1
for a in A:
    if a ==i:
        i+=1
 
ans =n-i+1
if ans ==n:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=map(int,input().split())
c=0
i=1
for m in a:
	if m != i: c+=1
	else: i+=1
print(c if c<n else"-1")

Submission status: Accepted

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

i = 0
c = 0
n = 1

while True:
    if i >= N:
        break
    while i < N and a[i] != n:
        c += 1
        i += 1
    if c == N:
        c = -1
        break
    i += 1
    n += 1

print(c)

Submission status: Accepted

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

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

if ans==0 and tmp==N:
  print(ans)
elif ans==0 and j==1:
  print(-1)
elif N>tmp:
  print(ans+(N-tmp))
else:
  print(ans)

Submission status: Accepted

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

if not(1 in A):
  print('-1')
else:
  index = 0
  count = 1
  while index<N:
    if A[index] == count:
      count+=1
    index+=1
  print(N-(count-1))

Submission status: Accepted

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

count = 0
if 1 in x:
    try:
        j = 0
        for i in range(n):
            while x[j]!=(i+1):
                j = j+1
                count += 1    
            j +=1        
        print(count)
    except:
        print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('input_file.txt')
	
#+++++
import fractions
#a,b = map(int, input().split())
n = int(input().strip())
aa=list(map(int,input().split()))
i = 1
count = 0
for a in aa:
	if a != i:
		count += 1
	else:
		i += 1
		
if count == n:
	print('-1')
else:
	print(count)

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
renga_list = list(map(int, input().split()))


i = 1

for renga in renga_list:
    if i == renga:
        i += 1
        
result = n - i + 1
if n == result:
    print(-1)
else:
    print(result)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int,input().split()))
c = 0
l = []
ind = 1
#print(s)
for i in s:
  #print(i)
  if i == ind:
    ind += 1
    #print("t")
  else:
    c += 1
#print(ind)
if ind == 1:
  print(-1)
else:
  print(c)

Submission status: Accepted

Solution code:
def main():
	n = int(input())
	# b = int(input())
	a = list(map(int,input().split()))
	# A = [list(map(int, input().split())) for _ in range(n)]
	# print(a)
	ans = 0
	kijun=1
	check = 0

	while check < n:
		# print("check = {}".format(check))
		# print("kijun = {}".format(kijun))
		if a[check] == kijun:
			check += 1
			kijun += 1
			# print("true")
		else:
			check += 1
			ans += 1
		# 	print("false")
		# print("-----")



	# for i in range(1,n+1):
	# 	# print("i = {}".format(i))
	# 	if a[i-1]==kijun:
	# 		kijun += 1
	# 		continue
	# 	else:
	# 		ans += 1
		
	if ans == n:
		print(-1)
	else:
		print(ans)
	




if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
min_val = 0
for i in range(N):
    temp = min_val + 1
    if temp == a_list[i]:
        min_val = a_list[i]
        
if min_val == 0:
    print(-1)
else:
    print(N - min_val)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
block = [int(x) for x in input().split()]
s = 1
c = 0
for i in range(n):
  if block[i] == s:
    s += 1
  else:
    c += 1
if block == [1]:
  print(c)
elif s == 1:
  print(-1)
else:
  print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
c = 1

for i in range(len(l)):
    if l[i] == c:
        c += 1
if c == 1:
    print(-1)
else:
    print(len(l)-c+1)

Submission status: Accepted

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

r = 0
i = 1
k = 0
while True:
    if a[k] == i:
        i += 1
        k += 1
    else:
        k += 1
        r += 1
    if k >= len(a):
        if r == len(a):
            print(-1)
        else:
            print(r)
        exit()

Submission status: Accepted

Solution code:
n = int(input())
a = input().split()
j=0
c=0
for i in range(len(a)):
    if(int(a[i])!=j+1):
        c+=1
    else:
        j+=1
if(n==c):
    print(-1)
else:
    print(c)

Submission status: Accepted

Solution code:
#D
N=int(input())
mylist=list(map(int, input().split()))
i=1
j=0
count=0

while j<N:
    if mylist[j]==i:
        count+=1
        i+=1
        j+=1
        #print("if"+str(i)+","+str(j))
    else:
        j+=1
        #print("else"+str(i)+","+str(j))

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
k = 0
for i in range(1, n + 1):
    try:
        while i != a[k]:
            cnt += 1
            k += 1
    except IndexError:
        break
    k += 1

if 1 not in set(a):
    print(-1)
else:
    print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
block = list(map(int, input().split()))
num = 1
times = 0
j = 0
while j < N:
  if block[j] != num:
    times += 1
  else:
    num += 1
  j += 1
if num != 1:
  print(times)
else:
  print("-1")

Submission status: Accepted

Solution code:
import numpy as np

def main():
    N = int(input())
    As = list(map(int, input().split()))

    leap_seq = [0]
    for n in range(N):
        if leap_seq[-1]+1 == As[n]:
            leap_seq.append(As[n])

    del leap_seq[0]
    if len(leap_seq) == 0:
        print(-1)
    else:      
      print(int(N-len(leap_seq)))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in 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)

Submission status: Accepted

Solution code:
n  = int(input())


ls = list(map(int, input().split()))
tmp = 1
for i in ls:
    if i == tmp:
        tmp += 1

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

Submission status: Accepted

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

for i in range(1, n+1):
  for j in range(b, n):
    if i == a[j]:
      out += j-b
      b = j+1
      break
  else:
    out += n-b
    break

if out == n:
  out = -1

print(out)

Submission status: Accepted

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

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

    else:
        flag+=1

if 1 not in A:
    print(-1)
else:
    print(flag)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
bbn=1000000007
	
#+++++
		
def main():
	n = int(input())
	al = list(map(int, input().split()))
	
	if 1 not in al:
		return -1
		
	cv = 1
	bn=0
	for v in al:
		if v == cv:
			cv += 1
		else:
			bn += 1
		
	print(bn)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)

if __name__ == "__main__":
	if sys.platform =='ios':
		sys.stdin=open('inputFile.txt')
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
n = int(input())

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

search = 1
ans = 0


for i in range(len(l)):
  if not l[i] == search:
    ans += 1
  else:
    search += 1

if ans == len(l):
  ans = -1

print(ans)

Submission status: Accepted

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

purged_data = [] #purge : [動詞]〜を粛清する
tmp = a[0] - 1
s=1
for i, e in enumerate(a):
    if e==s:
        purged_data.append(e)
        tmp = e
        s+=1
for i in range(len(purged_data)):
    if purged_data[i]!=i+1:
        print(-1)
        sys.exit()
if len(purged_data)==0:
    print(-1)
    sys.exit()
print(len(a)-len(purged_data))

Submission status: Accepted

Solution code:
a = int(input())
array = list(map(int, input().strip().split()))
brk = 0
ok = 0
su = 1
for i in array:
    if su == i:
      ok += 1
      su += 1
    else:
      brk +=1
if brk == 0 and ok != 0:
  print(0)
elif ok == 0:
  print(-1)
else:
  print (brk)