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

ans = 0
n = 1

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

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

Submission status: Accepted

Solution code:
#初期入力
import sys
N = int(input())
A=[int(i) for i in input().split()]
#


if 1 not in A:
    print(-1)
    exit
else:
    i=1 #探す数字
    brick_remain =set() #「1」は入っていること上記で確かめた
    for a in A:
        if a ==i:
            brick_remain.add(a)
            i +=1
    brick_destroy = N - len(brick_remain)       
    print(brick_destroy)

Submission status: Accepted

Solution code:
def main():
  N=int(input())
  a = [int(s) - 1 for s in input().split()]
  result = 0
  for i in range(N):
    if a[i] != (i - result):
      result += 1

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

if __name__=='__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
i = 0
val = 1
while i < n:
    while(i < n and a[i] != val):
        i = i + 1
        count = count + 1
    
    if(i < n):
        i = i + 1
        val = val + 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'''AtCoder水色になりたいのよさ
一番左を「1」にするために、「num」の初期値を1にし、
配列aを順番に判定していきます。
1以外のブロックは壊して「b_num」に足していきます。
1が見つかった後は、「num」を1増やして、同じ処理を続けます。
結果的に壊したブロックの数「b_num」が答えになります。
'''
n = int(input())
a = list(map(int, input().split()))
num = 1
b_num = 0

for i in range(n):
    if num != a[i]:
        b_num += 1
    else:
        num += 1

if b_num == n:
    b_num = -1

print(b_num)

Submission status: Accepted

Solution code:
'''ika tako
先頭から貪欲に残せる要素を拾っていって、
残る要素数を  N  から引いた値が答え
'''
n = int(input())
aaa = list(map(int, input().split()))
 
if 1 not in aaa:#'1'がどこにも無ければ不可能なので'-1'を出力して終了
    print(-1)
    exit()
 
i = 0
a = 1
while True:
    try:
        j = aaa.index(a, i)
        #文字列.index(検索する文字列 [,開始の位置 [,終了の位置]])
    except ValueError:#指定する文字列がない場合ValueError
        break
    i = j + 1
    a += 1
 
print(n - a + 1)

Submission status: Accepted

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

i = 0
num = 1
cnt = 0
while i <= N-1:
    if a[i] == num:
        num += 1
        i += 1
    else:
        cnt += 1
        i += 1

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

Submission status: Accepted

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

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

flag=False
cnt=1
for i in range(n):
    if a[i]==cnt:
        cnt+=1
        flag=True

if flag:
    print(n-cnt+1)
else:
    print(-1)

Submission status: Accepted

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

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

for e in a:
  if e == cur:
    cur += 1
  else:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
serch_value = 1
for v in a:
    if (v == serch_value):
        serch_value += 1
found_value = serch_value - 1
if (found_value == 0):
    print(-1)
else:
    print(n-found_value)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
if __name__ == "__main__":
    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)
        sys.exit(0)

    print(len(a) - (target - 1))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

out = 0
num = 1
for A in As:
    if  A == num:
        num += 1
    else:
        out += 1
if out == n:
    print(-1)
else:
    print(out)

Submission status: Accepted

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

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

if ans == n:
    ans = -1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

# D - Brick Break
n = int(input())
a = list(map(int, input().split()))
i = 1
bricks = 0

for ai in a:
	if ai == i:
		bricks += 1
		i += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

    #先頭から砕かない個数を数える
    for i in range(N):
        if num > N - 1:
            print(-1)
            exit()
        elif A[i] == num+1:
            num += 1
    if num == 0:
        print(-1)
    else:
        print(N-num)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

break_cnt = 0
find_num = 1

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

if find_num == 1:
    print(-1)
else:
    print(break_cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
A = rl()

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

Submission status: Accepted

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

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

Submission status: Accepted

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


def solve(N: int, a: "List[int]"):
    num_break = 0
    n = 1
    for a_ in a:
        if a_ == n:
            n+=1
        else:
            num_break+=1
    if n > 1:
        print(num_break)
    else:
        print(-1)

    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:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

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

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    A = readInts()
    now = 1
    i = 0
    cnt = 0
    while i < n:
        if A[i] == now:
            now += 1
        else:
            cnt += 1
        i += 1
    if cnt == n:
        print('-1')
    else:
        print(cnt)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    idx = 1
    cnt = 0
    for i in range(N):
        if A[i] == idx:
            idx += 1
            cnt += 1
    if cnt == 0:
        print(-1)
    else:
        print(N-cnt)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def main():
    N = int(input())
    a = list(map(int, input().split()))
    CNT = 0
    ID = 1
    for i in range(N):
        if a[i] == ID:
            ID += 1
        else:
            CNT += 1
    if CNT != 0 and ID != 1:
        print(CNT)
    elif CNT == 0 and ID != 1:
        print(CNT)
    elif CNT != 0 and ID == 1:
        print(-1)

if __name__ == '__main__':
    main()

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:
n = int(input())
lis = list(map(int, input().split()))
res = 0
cnt = 0
flg = False
for i in lis:
    if i == 1:
        flg = True
        res = max(res, cnt)
        cnt = 1
    elif i == cnt + 1:
        cnt += 1

if not flg:
    print(-1)
else:
    print(n - max(res, cnt))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
num = 1
for i in a:
    if 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()))

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

Submission status: Accepted

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

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

  print(ans)

Submission status: Accepted

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

if 1 in a:
    i = a.index(1)
    now = 1
    while i < n:
        if a[i] == now + 1:
            now += 1
        i += 1
    print(n - now)
else:
    print(-1)

Submission status: Accepted

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

idx = 1
chop = 0
for n in range(N):
    if As[n] == idx:
        idx += 1
    else:
        chop += 1


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

Submission status: Accepted

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

num_looking_for = 1
index = 0
cnt = 0

while index < n:
    if a[index] == num_looking_for:
        num_looking_for += 1
    else:
        cnt += 1
    index += 1

if cnt == n:
    print(-1)
    quit()
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)

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

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

Submission status: Accepted

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

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

print(cnt if cnt <= N-1 else "-1")

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
a = list(map(int, input().split()))
if 1 in a:
    d = deque(a)
    count = 0
    num = 1
    pick = 0
    while d:
        pick = d.popleft()
        count += 1
        if pick == num:
            count -= 1
            num += 1

    print(count)
else:
    print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if b_num == n:
    b_num = -1

print(b_num)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

checkNum = 1
howMany = 0

for j in range(N):
    if line[j] == checkNum:
        checkNum += 1
    else:
        howMany += 1

# print(line)
# print(checkNum)
# print(howMany)

if checkNum == 1:
    print("-1")
else:
    print(howMany)

Submission status: Accepted

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

change = False
ans = 0
re = 0

for i in range(N):
    if A[i]==re+1:
        re += 1
        change = True
    else:
        ans += 1

if change == True:
    print(ans)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
k=0
c=1
if 1 not in a:
	print('-1')
	exit()
else:
	for i in a:
		if i!=c:
			k+=1
		else:
			c+=1
print(k)

Submission status: Accepted

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

Submission status: Accepted

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

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

if ans == 0:
    print(-1)
else:
    print(len(a)-ans)

Submission status: Accepted

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

a_only = list(set(a_inputs))

ans=0
count=0
for i in range(len(a_inputs)):
    if a_inputs[i]==(count+1):
        count+=1
        continue
    else:
        ans+=1

if a_inputs==a_only:
    print(0)
elif 1 not in a_inputs:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    num_list = list(map(int, input().split()))

    count = 0
    search_num = 1

    for num in num_list:
        if search_num == num:
            search_num += 1
            continue
        count += 1
        
    print(count if search_num != 1 else -1)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
a = list(map(int, input().split()))
if a.count(1) == 0:
    print(-1)
else:
    q = deque(a)
    count = 0
    crash_count = 0
    while q:
        if q[0] != 1 + count:
            q.popleft()
            crash_count += 1
        else:
            q.popleft()
            count += 1
    print(crash_count)

Submission status: Accepted

Solution code:
_, *a = map(int, open(0).read().split())
p, c = 1, 0
for i in a:
  c += i!=p
  p += i==p
print(-1 if p==1 else c)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    ans, cnt = N, 1
    for a in A:
        if a == cnt:
            ans -= 1
            cnt += 1
    if ans == N:
        print('-1')
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
del_num = []

for index, i in enumerate(num):
    if i == ans + 1:
        ans += 1
    else:
        del_num.append(index)

if len(num) == len(del_num):
    print(-1)

else:
    print(len(del_num))

Submission status: Accepted

Solution code:
n = int(input())
a = map(int, input().split())
ans_lst = []
for ai in a:
    if len(ans_lst) == 0 and ai == 1:
        ans_lst.append(1)
    elif len(ans_lst) > 0 and ans_lst[-1] == ai - 1:
        ans_lst.append(ai)
if len(ans_lst) == 0:
    print(-1)
else:
    print(n - len(ans_lst))

Submission status: Accepted

Solution code:
n = int(input())
a = map(int, input().split())
ans_lst = []
for ai in a:
    if ai == 1 and len(ans_lst) == 0:
        ans_lst.append(1)
    elif len(ans_lst) > 0 and ans_lst[-1] == ai - 1:
        ans_lst.append(ai)
if len(ans_lst) == 0:
    print(-1)
else:
    print(n - len(ans_lst))

Submission status: Accepted

Solution code:
n = int(input())
a = map(int, input().split())
ans_lst = []
for ai in a:
    if ai == 1:
        ans_lst.append([1])
    else:
        for ans in ans_lst:
            if ans[-1] == ai - 1:
                ans.append(ai)
if len(ans_lst) == 0:
    print(-1)
else:
    min = 200001
    for i in range(len(ans_lst)):
        if min > len(ans_lst[i]):
            min = len(ans_lst[i])
    print(n - min)

Submission status: Accepted

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

cnt = 0
num = 1

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

Submission status: Accepted

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

num = 1
ans = 0

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

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

Submission status: Accepted

Solution code:
data=int(input())
array=list(map(int,input().split()))
counter=0
qwe=1
for i in range(data):
    if array[i]==qwe:
        counter+=1
        qwe+=1
if counter==0:
    print(-1)
else:
    print(data-counter)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_right as bsr
from collections import Counter as cnt
from collections import defaultdict as dd
from collections import deque
from fractions import gcd
from itertools import permutations as perm
from math import ceil, factorial, floor
from operator import itemgetter as ig

sys.setrecursionlimit(1000000)

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def yesno(v, yes="Yes", no="No", upper=False):
    if upper:
        print([yes.upper(), no.upper()][not v])
    else:
        print([yes, no][not v])


def main():
    """エントリーポイント"""
    N = input(int)
    A = input([int, N])
    i, ans = 1, 0
    for a in A:
        if not a == i:
            ans += 1
        else:
            i += 1
    if ans == N:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = n - count if count > 0 else -1
print(ans)

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

Submission status: Accepted

Solution code:
n = int(input())
ns = map(int, input().split())
rslt = 0
num = 1
for i in ns:
    if num == i:
        num += 1
        continue
    rslt += 1
if n == rslt:
    print(-1)
else:
    print(rslt)

Submission status: Accepted

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

if k == 0:
   print(-1)
else:
   print(x)

Submission status: Accepted

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

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

Submission status: Accepted

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

counter = 1
lista = []
for i in range(len(a)):
    if a[i] == counter:
        lista.append(a[i])
        counter += 1
if len(lista) > 0:
    print(n - len(lista))
else:
    print(-1)

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

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

nm = 1
cnt = 0

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

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

Submission status: Accepted

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

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

print(n-ct if ct>0 else -1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC148D - Brick Break

import bisect

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

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

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

Submission status: Accepted

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

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

  else:
    breaks += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
a = list(map(int,input().split()))
ok = False
for i in range(n):
    if a[i]==1: ok = True
if ok==0: print(-1)
else:
    ans = 0
    num = 1
    for i in range(n):
        if a[i]==num:
            ans+=1
            num+=1
    print(n-ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
  n = int(input())

  i = 1
  for a_i in input().split():
    if i == int(a_i):
      i += 1
  
  if i == 1:
    i = n + 2
    
  print(n - i + 1)


if __name__ == "__main__":
  main()

Submission status: Accepted

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

v = 0
for x in a:
    if x == v + 1:
        v += 1
print(n - v) if v != 0 else print(-1)

Submission status: Accepted

Solution code:
n = int(input())
*an, = map(int, input().split())
if 1 not in an:
    print(-1)
else:
    i = 1
    ans = 0
    for a in an:
        if a==i:
            i += 1
        else:
            ans += 1
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = 1
fr = 0
for i in A:
    if count == i:
        count += 1
        fr += 1
if fr > 0:
    print(N-count+1)
else:
    print('-1')

Submission status: Accepted

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

Submission status: Accepted

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

find = 1

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

print(-1 if find == 1 else len(A) - find + 1)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin
import queue
import sys
import math

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

Submission status: Accepted

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

num = 1
count = 0
for n in a:
    if num == n:
        num += 1
    else:
        count += 1

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