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
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
def main():
    n = int(readline())
    a = [int(i) for i in readline().split()]
    j = 1
    for i in a:
        if i == j:
            j += 1
    if j-1 == 0:
        print(-1)
        return
    print(n-(j-1))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in a:
    if cnt == i:
        cnt += 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())) 


cnt_yes = 1
cnt_no = 0
for i in range(N):
    if A[i] != cnt_yes:
        cnt_no+=1
    else:
        cnt_yes+=1

flag = False
for a in A:
    if a ==1:
        flag = True
    
if flag:
    print(cnt_no)
else:
    print('-1')

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 = list(map(int,input().split())) 

a = 1
n = 0
for i in range(N): 
    if l[i]==a:
        a = a+1
    else:
        n += 1
if a==1:
    print("-1") 
else:
    print(n)

Submission status: Accepted

Solution code:
import bisect
n=int(input())
a=list(map(int,input().split()))
c=max(a)
ct=[[0] for _ in range(c+1)]
for i in range(n):
	ct[a[i]].append(i+1)
ans = -1
memo = -1
for j in range(c+1):
	if max(ct[j])>memo:
		ans += 1
		memo = ct[j][bisect.bisect_right(ct[j], memo)]
	else:
		break
if ct[1]==[0]:
	print(-1)
else:
	print(n-ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*n
def int1(x):
  return int(x)-1
a[:]=map(int1,input().split())

r=0
#j0=0
#import numpy as np
ii=0
for jj in range(n):
  if a[jj]==ii:
    r+=1
    ii+=1
if r==0:
  print(-1)
  exit()

print(n-r)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
a = list(map(int,input().split()))
q = deque(a)
count = 1
ans = 0
for i in range(n):
    if q[0] == count:
        count += 1
        q.popleft()
    else:
        q.popleft()
if count == 1:
    print(-1)
else:
    print(len(a)-count+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

B=[i for i in range(1,N+1)]
C=[]
ans=0
for i in B:
    while A:
        x=A.popleft()
        if i!=x:
            ans+=1
        else:
            C.append(x)
            break
if C==[]:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int , input().split()))
# count = 0 if 1 in a else -1
# i = 1
# while i in a:
#   ind = a.index(i)
#   count += ind
#   a = a[ind + 1:]
#   i += 1
# print(count)

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

print(n - len(ans) if 1 in a else -1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np

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

search = 1

if a.min()>1:
    print(-1)
else:
    for i in range(N):
        if a[i]==search:
            search += 1
        else:
            continue
    else:
        print(N-search+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
from collections import deque
import bisect

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

i = 1
sum = 0
end = False
while True:
    while A[0] != i:
        A.popleft()
        sum += 1
        if len(A) == 0:
            end = True
            break

    if end == True:
        if i == 1:
            sum = -1
        break

    A.popleft()
    i += 1
    if len(A) == 0:
        break

print(sum)

Submission status: Accepted

Solution code:
# D - Teleporter
N= int(input())
A = [int(j) for j in 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)

Submission status: Accepted

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

current_idx = -1
next_num = 1
broken = 0
success = False

while broken < n:
    # current_idx より右で next_num が見つかるまで探す
    found = False
    for idx in range(current_idx + 1, n):
        if as_[idx] == next_num:
            # 見つかった
            current_idx = idx
            next_num += 1
            found = True
        else:
            broken += 1

    if n - broken == next_num - 1:
        success = True
        break

if n - broken > 0 and success:
    print(broken)
else:
    print(-1)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def gcd(x, y):
    if x % y == 0:
        return y
    return gcd(y, x % y)


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    num = 1
    for ai in a:
        if ai == num:
            num += 1
        else:
            ans += 1
    if num == 1:
        print(-1)
    else:
        print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
N = int(input())
lis = list(map(int,input().split()))
count = 0
search = 1
result = -1
rest = 0
for i in lis:
    if i == search:
        search += 1
        rest +=1
    else:
        count +=1

if rest == search-1 and rest != 0:
    result = count

print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = tuple(map(lambda n: int(n) - 1, input().split()))

rem = 0
bk = 0
for a in A:
    if a == rem:
        rem += 1
    else:
        bk += 1

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    import itertools

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


#     indices = [i for i in range(N)]
#     for i in range(N):
#         for v in itertools.combinations(indices, i):
#             v = list(v)
#             # print(v)
#             tmp_l = get_l(A, v)
#             # print(tmp_l)
#             if judge(tmp_l):
#                 print(i)
#                 exit()
#     print(-1)


# def get_l(l, rm_ind):
#     ans = []
#     for i, a in enumerate(l):
#         if i in rm_ind:
#             continue
#         ans.append(a)
#     return ans


# def judge(l):
#     for i, a in enumerate(l):
#         if i+1 != a:
#             return False
#     return True


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
idx = 1

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# ABC148D

n = int(input())
a = list(map(int, input().split()))
if 1 not in a:
    print(-1)
else:
    count = 0
    find = 1
    for i in a:
        if i == find:
            find += 1
        else:
            count += 1
    print(count)

Submission status: Accepted

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

c=1
ans = 0
while(len(a)>0):
  if c== a.pop():
    c+=1
  else:
    ans+=1

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

Submission status: Accepted

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

num = 1
i = 0
while i <= N-1:
    if la[i] == num:
        num += 1
    i += 1

if num == 1:
    print("-1")
else:
    print(N - num + 1)

Submission status: Accepted

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

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

ans = N - found if found > 0 else -1
print(ans)

Submission status: Accepted

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

break_cnt = 0
next_num = 1
for a in a_list:
    if a == next_num:
        next_num +=1
    else:
        break_cnt+=1

if break_cnt != n:
    print(break_cnt)
else:
    print(-1)

Submission status: Accepted

Solution code:
ii = lambda:int(input())
mi = lambda:list(map(int,input().split()))
ix = lambda x:list(input() for _ in range(x))
mix = lambda x:list(mi() for _ in range(x))
iix = lambda x:list(int(input()) for _ in range(x))
##########
def main(n,a):
    if (not 1 in a):return -1
    res = 0
    idx = 1
    for i in a:
        if i != idx:
            res += 1
        else:
            idx += 1
    return res
    
n = ii()
a = mi()
print(main(n,a))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
f = True
cnt = 1
for a in A:
     if cnt==a:
         cnt+=1
if cnt==1:print(-1)
else:print(n-cnt+1)

Submission status: Accepted

Solution code:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect
import itertools
import heapq


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(input):
    bricks = string_to_int(input)
    count = 1
    broken = 0
    for b in bricks:
        if b == count:
            count += 1
            continue
        broken += 1

    return -1 if broken == len(bricks) else broken


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(input())
    print(ret)

Submission status: Accepted

Solution code:
n = int(input())
t = [False]*(n+1)
t[0] = True
l = [int(i) for i in input().split()]
ans = 0
for i in range(n):
  x = l[i]
  if t[x-1]:
    t[x] = True
    ans = max(ans, x)
print(n-ans if ans > 0 else -1)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
now = 1
flag = True
for i in range(N):
  if a[i] == now:
    flag = False
    now += 1
if flag:
  print(-1)
  exit()
print(N-now+1)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
k = list(map(int, input().split()))
num = 1
ans = n
flg = False
for i in range(n):
  if k[i] == num:
    ans -= 1
    num += 1
  
if ans == n:
  ans = -1
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May  9 16:33:03 2020

@author: shinba
"""


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

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

Submission status: Accepted

Solution code:
N = int(input())
ls = list(map(int, input().split()))
counter = 1
for elem in ls:
    if elem == counter:
        counter += 1
if counter == 1:
    print(-1)
else:
    print(N - counter + 1)

Submission status: Accepted

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

Submission status: Accepted

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

main()

Submission status: Accepted

Solution code:
def StalinSort(L):
    """スターリンソート
    参考
    https://qiita.com/Sirloin/items/c9e5c74be3366df65bce
    ソート対象のリストを先頭要素から調べて、昇順になっていない要素をリストで返す
    input   L:ソート対象のリスト
    output  purge:昇順になっていない（邪魔になる）要素のリスト
    """

    targ = 1
    purge = []
    for l in L:
        if l != targ:
            purge.append(l)
        else:
            targ += 1
    return purge

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

if len(A) == len(StalinSort(A)):
    print(-1)
else:
    print(len(StalinSort(A)))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
t=1
for i in range(n):
  if l[i]==t:
    t+=1
if t>1:
  print(n-t+1)
else:
  print('-1')

Submission status: Accepted

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

for i in range(N):
  if i != N-1 and a[i] == j:
    j += 1
  
  elif i != N-1 and a[i] != j:
    ans += 1

  elif i == N-1 and a[i] == j:
    break
  
  else:
    ans += 1
    break

if N == 1 and a[0] == 1:
  ans = 0
    
elif ans == N:
  ans = -1
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = N
now = 1
if 1 not in A:
    print(-1)
    exit()

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
s = 0
for a in A:
    if a == s + 1:
        s = a
if s == 0:
    print(-1)
else:
    print(N-s)

Submission status: Accepted

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

Submission status: Accepted

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

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

if k == 1:
    print(-1)
else:
    print(len(an) - k + 1)

Submission status: Accepted

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

count = 0
find = 1
for i in range(n):
    if A[i] == find:
        find += 1
    else:
        count += 1
print(count if count != n else -1)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a = list(map(int,input().split()))
        
    j = 1
    for i in range(N):
        if(j == a[i]):
            j += 1
    print(N - j + 1 if j > 1 else -1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a = input().split()
    for i in range(N):
        a[i] = int(a[i])
    j = 1
    for i in range(N):
        if(j == a[i]):
            j += 1
    if j > 1:
        print(N-j+1)
    else:
        print(-1)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(count if i > 1 else -1)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    # -1になるのは１がlに１個もないとき
    if 1 not in l:
        print(-1)
    # 今何を探しているかをcntに入れる
    # 最初は1を探す
    else:
        cnt=1
        for i in range(n):
            if l[i]==cnt:
                # 1増やした数字を次に探したいからcnt増やす
                cnt+=1
        # cntは最後に見つかった最大の数+1になっているからcnt-1が見つかった個数
        # cnt-1以外はすべて砕く
        print(n-(cnt-1))
resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map( int , input().split()))
T = list()
if A.count(1) == 0:
    print(-1)
else:
    for i in range(len(A)):
        if A[i] == len(T)+1:
            T.append(A[i])
    print(N-len(T))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
from fractions import gcd
def resolve():
    N,=pin()
    A=tupin()
    
    if 1 not in A:print(-1);return
    suuji=0
    ans=0
    for a in A:
        if a==suuji+1:
            suuji+=1
        else:ans+=1
    print(ans)
#%%submit!
resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
#from fractions import gcd
#from math import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

n = I()
A = LI()

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

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

Submission status: Accepted

Solution code:
# D - Brick Break

N = int(input())
A = [int(a) for a in input().split()]

ls = []
i = 1
for j in range(N):
    if A[j] == i:
        ls.append(A[j])
        i += 1

print(len(A) - len(ls) if 0 < len(ls) else -1)

Submission status: Accepted

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

cnt =0
index =1
i=0
while i <N:
    if lenga[i]!=index:
        cnt+=1
    else:
        index+=1

    i+=1

if cnt>N-1:
    print('-1')
else:
    print(cnt)

Submission status: Accepted

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

num = [0 for i in range(n + 1)]
tugi = 1
count = 0
flag = False

for i in range(n):
    if a[i] == 1:flag = True
    if a[i] != tugi:
        count += 1
    else:
        tugi += 1

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
import math
import scipy
import itertools

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    tar, ans = 1, 0
    bit = 0
    for i in range(n):
        if a[i] == tar:
            bit = 1
            tar += 1
        else:
            ans += 1
    if bit == 0:
        print(-1)
    else:
        print(ans)
    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

c = 1
ans = 0

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

Submission status: Accepted

Solution code:
def main():
    ind = int(input())
    t = list(map(int, input().split()))
    ans = []
    for i in range(ind):
        if len(ans) + 1 == t[i]:
            ans.append(t[i])
    count = -1 if len(ans) == 0 else ind - len(ans)
    print(count)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l1 = [0]
for i in l:
  a = l1.pop()
  if i==a+1:
    l1.append(a)
    l1.append(i)
  else:
    l1.append(a)
if len(l1)==1:
  print(-1)
else:
  print(n-len(l1)+1)

Submission status: Accepted

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

if 1 not in a:
    print(-1)
else:
    t = 1
    ans = 0
    for ai in a:
        if t == ai:
            t += 1
        else:
            ans += 1
    print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    a_list = list(map(int, input().split(' ')))
    broken_cnt = 0
    remain_max_number = 0
    for a in a_list:
        if a == remain_max_number + 1:
            remain_max_number += 1
        else:
            broken_cnt += 1
    answer = broken_cnt if broken_cnt < N else -1
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    N = int(input())
    *a, = map(int, input().split())

    n = 1
    s = 0

    for _ in a:
        if _ == n:
            n += 1
        else:
            s += 1
    if n == 1:
        print(-1)
    else:
        print(s)


main()

Submission status: Accepted

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

res = 0
i = 1
for a in A:
    if a != i:
        res += 1
        continue
    i += 1

if res == N:
    res = -1

print(res)

Submission status: Accepted

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

next_number = 1
num_broken = 0

for brick in bricks:
  if brick == next_number:
    next_number += 1
  else:
    num_broken += 1
    
if next_number == 1:
  print(-1)
else:
  print(num_broken)

Submission status: Accepted

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

Submission status: Accepted

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

val = 1
ans = 0
for a_i in a:
    if a_i == val:
        val += 1
    else:
        ans += 1

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

Submission status: Accepted

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


def solve(N: int, a: "List[int]"):
    num = 1
    count = 0

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

    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:
# -*- coding: utf-8 -*-
"""
Created on Sun May  3 19:04:26 2020

@author: Kanaru Sato
"""

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

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

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

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int,input().split()))
i = 1
j = 1
c = 0
while(j <= n):
    if(a[j-1] == i):
        i += 1
    else:
        c += 1
    j += 1
if(c == n):print(-1)
else:print(c)

Submission status: Accepted

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

cnt = 1
ans = 0

for a in ali:
    if a == cnt:
        cnt = cnt + 1
    else:
        ans = ans + 1

if ans == N:
    ans = -1

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
ans=0
l=1
for i in a:
  if i!=l:
    ans+=1
  else:
    l+=1
if ans==N:
  print("-1")
else:
  print(ans)

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 += 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()))
ans=0
c=1
for i in a:
  if i==c:
    c+=1
  else:
    ans+=1
if c==1:
  print(-1)
else:
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

index = 1
cnt = 0
for i in range(len(a)):
  if a[i] != index:
    cnt +=1
  else:
    index +=1
    
if min(a)!=1:
  print("-1")
else:
  print(cnt)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
cnt=1
for i in range(n):
  if a[i]==cnt:
    cnt+=1
if n>1:
  if cnt>1:
    print(n-cnt+1)
  else:
    print(-1)
else:
  if a[0]==1:
    print(0)
  else:
    print(-1)

Submission status: Accepted

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

for n in range(N):
  if a[n]==num:
    num +=1

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