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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

# 1から順番に並べられたらOK
a = 1
c = 0
for i in range(N):
    if A[i] == a:
        # 確定した扱い
        a += 1
    else:
        # 取り除いた扱い
        c += 1

print(c if a > 1 else -1)

Submission status: Accepted

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

s=1
for i in range(len(X)):
    if X[i]==s:
        s+=1

if s==1:
    print(-1)
else:
    print(len(X)-(s-1))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
count = 1
for i in range(0,N):
  if A[i] == count:
    count += 1
if count != 1:
	print(N-count+1)
else:
  print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

if count_break == n :
    print('-1')
else:
    print(count_break)

Submission status: Accepted

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

now = 1
count = 0

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

Submission status: Accepted

Solution code:
n = int(input())
#a,b = map(int,input().split())
L = list(map(int,input().split()))
#L.sort()

c = min(L)
flag = False
if  c == 1:
    flag = True

li = []
for i in range(n):
    if L[i] == c and flag == True:
        li.append(c)
        c = L[i]+1

if flag:
    print(len(L)-len(li))
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
N, *a = map(int, read().split())
a = [0] + a

seq = []
idx = 1
for i in range(1, N + 1):
    if a[i] == idx:
        seq.append(idx)
        idx += 1

if seq:
    print(N - len(seq))
else:
    print(-1)

Submission status: Accepted

Solution code:
n=int(input())
List = [int(i) for i in input().split()]
ans = 0
Counter = 1
flag = 0
for i in range(n):
  tmp = List[i]
  if tmp==Counter:
    Counter+=1
    flag=1
  else:
   	ans+=1
if flag==0:
  print(-1)
else:
  print(ans)

Submission status: Accepted

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

if 1 not in a:
  print(-1)
else:
  # 数値はあるパターン
  break_num = 0  # 壊した数
  before = 0  # 前回の数字があった場所
  search = 1  # 探している数
  
  for i in range(N):
    if search == a[i]:
      # print("now in", i,  "before", before, "then minus", (i - before))
      break_num += (i - before) - 1
      before = i
      search += 1

  break_num += (N - before)
  print(break_num)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
X=[]
a=1
for i in range(N): 
  if A[i]==a:      
    X.append(a)
    a+=1
ans=N-len(X)

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

Submission status: Accepted

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

flg = False
ret = 0
i = 1
for tmp in a:
    if(i != tmp):
        ret += 1
    else:
        i += 1
        flg = True

if not flg:
    print(-1)
else:
    print(ret)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

now=1
to_break=0
ans=False

for item in a:
  if item==now:
    now+=1
    ans=True
    #print("OK",item)
  else:
    to_break+=1
    #print("壊す",item)
    
if ans:
  print(to_break)
else:
  print(-1)

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

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

Submission status: Accepted

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

ans = 0
n = 0

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

Submission status: Accepted

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

ans = -1
subst = 0
nxtid = 1
for i in range(n):
    if a[i] == nxtid:
        subst += 1
        nxtid += 1
if subst > 0:
    ans = n - subst
print(ans)

Submission status: Accepted

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

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

count = 0
now = 1

for i in range(N):
    if a[i] == now:
        now += 1
    else:
        count += 1
        
if now == 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 x == 1 :
    print(-1)
else :
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

p = 1
ret = 0
ok = False

for x in a:
    if x != p:
        ret += 1
    else:
        p += 1
        ok = True
print(ret if ok else -1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split(" ")))
counter = 1
counter_break = 0

for i in range(n):
    if a[i] != counter:
        counter_break += 1
    else:
        counter += 1
if n == counter_break:
    print(-1)
else:
    print(counter_break)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
check = [0]*n
count = 1
sum = 0
back = 0
for i in range(n):
  if l[i] == count:
    check[i] = 1
    count += 1
  
    
if check.count(1) == 0:
  print(-1)
elif check.count(1) == n:
  print(0)
else:
  print(check.count(0))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
N=int(input())
a=list(map(int,input().split()))
ans=0
i=0
i2=0
while i<N:
    if i2==a[i]-1:
        i=i+1
        i2=i2+1
    else:
        i=i+1
        ans=ans+1
if ans==N:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))

b = []
count = 1
del_x = 0
for i in range(0, n):
    if a[i] == count:
        count += 1
    else:
        del_x += 1

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

Submission status: Accepted

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

pos = 1
ans = 0
leng = len(alist)
while(leng >= pos+ans):
    if (pos != alist[pos-1+ans]):
        ans += 1
    else:
        pos += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(input().split())

count = 1
for i in range(len(A_list)):
  a = int(A_list[i])
  if a == count:
    count += 1
if count == 1:
  output = -1
else:
  output = N - count + 1
print(output)

Submission status: Accepted

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

# print(n, a)

target = 1
keep = 0
for i in range(n):
  if a[i] == target:
    target += 1
    keep += 1
if keep == 0:
  print(-1)
elif keep == n:
  print(0)
else:
  print(n - keep)

Submission status: Accepted

Solution code:
from sys import stdin
f_i = stdin

N = int(f_i.readline())

a = list(map(int, f_i.readline().split()))

ans = N
idx = 0
num = 1

while True:
    try:
        idx = a.index(num, idx)
        ans -= 1
        num += 1
    except ValueError:
        break

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

Submission status: Accepted

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

count = 1
nocount = 0
for i in a:
  if i == count:
    count += 1
  else:
    nocount += 1 
    
    
#print(count,nocount)    
ans = -1 if nocount == n else  nocount
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
i = 0
ans = 0
while i < N:
    if (A[i] == ans+1):
        ans += 1
    i += 1
if ans == 0:
    ans = N + 1
print(N - ans)

Submission status: Accepted

Solution code:

def main():
    N = int(input())
    a = list(map(int, input().split()))
    
    num_remain_blocks = 0
    for ai in a:
        if num_remain_blocks + 1 == ai:
            num_remain_blocks += 1
    if num_remain_blocks == 0:
        print(-1)
    else:
        print(N - num_remain_blocks)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
import datetime
import itertools
import sys
import copy

# 入力
#N = int(input())
#S = str(input())
#N, A, B = map(int, input().split())
N = int(input())
L = list(map(int, input().split()))

#if N == 1 and L[0] == 1 :
#  print (0)
#  exit()

index = 1
cnt = 0
for i in range(N) :
  if L[i] == index :
    index += 1
    cnt += 1

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

Submission status: Accepted

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

a_index = -1
ans = 0
for i in range(1, N + 1):
    for j in range(a_index + 1, N):
        if a_list[j] == i:
            a_index = j
            break
        else:
            ans += 1
    if j == N - 1:
        break
if ans == N:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

counter = 0
idx = 1
# ans = []
for i in range(N):
    if idx != a[i]:
        counter+=1
    else:
        idx += 1
if idx != 1:

    print(counter)

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

Submission status: Accepted

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

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

    else:
        ans += cnt
        cnt = 0
        num += 1

ans += cnt
if 1 not in a:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

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 ret == n and cnt == 1:
  print(-1)
else:
  print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
l = deque(list(map(int, input().split())))
now = 1
ans = 0

for _ in range(n):
  if l.popleft() == now:
    now += 1
  else:
    ans += 1
  
print(-1 if ans == n else ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

    else:
        count += 1

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

Submission status: Accepted

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

num=1
cnt=0
for i in l:
  if i==num:
    num+=1
  else:
    cnt+=1    
 
if cnt!=n and n-cnt==num-1:
  print(cnt)
else:
  print(-1)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
s=1
i=0
while i<N:
  if s==A[i]:
    s+=1
  i+=1
mi=N-(s-1)
if 1 not in A:
  print(-1)
else:
  print(mi)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=input().split()
a=np.array(a,dtype='int32')
k=0
x=0
for i in range(n):
	if a[i]==x+1:
		x+=1
	else:
		k+=1
if k>=n:
	print('-1')
else:
	print(k)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
add,point,cnt,flag=0,1,0,0
while add!=n:
    if A[add]==point:
        point +=1
        flag=1
    else:
        cnt +=1
    add +=1
print(cnt if flag==1 else -1)

Submission status: Accepted

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

i = 0
cnt = 0

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

    print(cnt)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

current = 1
for _a in a:
    if current == _a:
        current += 1

current -= 1
if current == 0:
    print(-1)
else:
    print(n-current if n-current > 0 else 0)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

present_num = 0


for brick in bricks:
    if brick == present_num + 1:
        present_num += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if suc>0:
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int,input().split()))
num = 1
for i in range(N-1):
  if num == arr[i]:
    num += 1
    
if N>1:
  if num==1 and arr[-1]!=1:
    print(-1)
  elif num==1 and arr[-1]==1:
    print(1)
  elif num!=1 and arr[-1]==num:
    print(N-num)
  elif num!=1 and arr[-1]!=num:
    print(N-num+1)

else:
  if arr[0]==1:
    print(0)
  else:
    print(-1)

Submission status: Accepted

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

counter = 1
smash = 0
bl = True

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

if smash == n:
    bl = False
# print(counter)

print(smash if bl else "-1")

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

n = 1
K = 0

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

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

Submission status: Accepted

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

index = 1
count = 0


for i in range(N):
  if int(A[i]) == index:
    index += 1
  else:
    count += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

index = 1

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

if index != 0:
    print(n - index)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
def check(arr):
	num=1
	Q=deque(arr)
	while len(Q):
		while len(Q) and Q[0]!=num:
			Q.popleft()
		if len(Q):
			num+=1
			Q.popleft()
	return len(arr)-(num-1)

n=int(input())
arr=[int(x) for x in input().split()]
ans=check(arr)
if ans==n:
	print(-1)
else:
	print(ans)

Submission status: Accepted

Solution code:
s = input().split()
N = int(s[0])
s = input().split()
count = 1
break_block=0
for i in s:
    if int(i) ==count:
        count+=1
    else:
        break_block+=1
if count==1:
    print(-1)
else:
    print(break_block)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

s = 0

for i in range(n):
  #print(s)
  s_ = (i+1) - l[i]
  if s_ == (i-s):
    s += 1

if s != 0:
  print(n-s)
else:
  print(-1)

Submission status: Accepted

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

i = 1
f = False
for a in aN:
    if i == a:
        i += 1
        f = True

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

Submission status: Accepted

Solution code:
n = int(input())
a = [0] * n
a = list(map(int, input().split(' ')))

ans = 0
cnt = 1
for ai in a:
    if ai == cnt:
        cnt += 1
    else:
        ans += 1

if (cnt == 1):
    ans = -1
print(str(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(n) for n in input().split(' ')]
n = 1
b = []
for m in a:
    if n == m:
        b.append(m)
        n += 1

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
result = 0
num = 1
if A.count(1) == 1 and len(A) == 1:
    print(0)
elif A.count(1) > 0:
    for i in range(N):
        if A[i] != num:
            result += 1
        else:
            num += 1
    if result == 0:
        print(-1 if len(set(A)) != N else 0)
    else:
        print(result)
else:
    print(-1)

Submission status: Accepted

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

break_number = 0
pre_index = 0
cnt = 1
hantei = False
for i in range(N):
    if cnt == N and (cnt != 1):
        break
    if a[i] == cnt:
        break_number += (i-pre_index)
        pre_index = i + 1
        cnt += 1
    if i == (N-1):
        if cnt != 1:
            break_number += max(0, i-pre_index + 1)
if cnt == 1:
    print(-1)
else:
    print(break_number)

Submission status: Accepted

Solution code:
import math
import sys

m=10**9 + 7 
sys.setrecursionlimit(1000010)
N=int(input())
A = list(map(int,input().split()))
# print(N,A)

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

index = 1
broke = 0

for i in A:
  if i == index:
    index += 1
  else:
    broke += 1

if index == 1:
  print(-1)
else:
  print(broke)

Submission status: Accepted

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

    ans = 0
    integer = 1
    for j in range(n):
        if a_list[j] == integer:
            integer += 1
        else:
            ans += 1
    if integer == 1:
        print(-1)
    else:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# D - Brick Break
N = int(input())
A = list(map(int, input().split()))
DP = []
tmp = 0

for i in range(N):
    if A[i] == tmp +1:
        DP.append(A[i])
        tmp += 1
    else:
        pass

if len(DP) >= 1:
    print(N-len(DP))
else:
    print(-1)

Submission status: Accepted

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

now_i = 1
num = 0
for a_i in A_list:
    if a_i == now_i:
        now_i += 1
    else:
        num += 1
if now_i == 1:
    print(-1)
else:
    print(num)

Submission status: Accepted

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

count = 1
ans = 0
for x in nums:
  if x == count:
    count+=1
  else:
    ans+=1

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

Submission status: Accepted

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

cnt = 1
ans = 0
b = []

for i, j in enumerate(a):
    if cnt==j:
        cnt += 1
        b.append(j)
        continue
    else:
        ans += 1
        b.append(0)

if sum(b) == 0:print(-1)
else:print(ans)

Submission status: Accepted

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

def check(m):
    tar = 1
    for i in range(n):
        if a[i] == tar:
            tar += 1
    if tar >= m+1:
        return 1
    else:
        return 0

if check(1) == 0:
    print(-1)
else:
    l = 1
    h = n
    mid = 1
    ans = None

    while l < h:
        mid = (l+h)//2
        guess = check(mid)
        #print(guess)
        if guess:
            l = mid + 1
        else:
            h = mid -1

    saku = sorted([l,h,mid], reverse=True)

    if check(saku[0]):
        print(n-saku[0])
    elif check(saku[1]):
        print(n - saku[1])
    else:
        print(n-saku[2])

Submission status: Accepted

Solution code:
import sys
from pprint import pprint

def solve(n, a):
    pre = 0
    count = 0
    for a_i in a:
        if a_i == pre + 1:
            pre = a_i
            count += 1

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

if __name__ == '__main__':
    n = int(sys.stdin.readline().strip())
    a = map(int, sys.stdin.readline().strip().split(" "))
    
    solve(n, a)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

#L = reversed(L)

#max_a = 10**8
a = 1
ans = 0
for i in range(n):
    if L[i] == a:
        a += 1
    else:
        ans +=1

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

Submission status: Accepted

Solution code:
import math
import fractions

N = int(input())
a = list(map(int, input().split(' ')))
current = 1
answer = 0

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
i = 0
j = 0
f_flg = 0
while i < len(a) and j < len(a):
    while a[j] != i + 1:
        cnt += 1
        j += 1
#         print(j)
        if j > len(a) - 1:
            if f_flg == 0:
                print(-1)
                exit()
            break
    f_flg = 1
    i += 1
    j += 1

print(cnt)

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    a = [int(x) for x in stdin.readline().rstrip().split()]
    count = 0
    current = 1
    for x in a:
        if x == current:
            current += 1
        else:
            count += 1
    if count == N:
        print(-1)
    else:
        print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
i=1
ans=0
for j in range(n):
  if a[j]==i:
    i+=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()))

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

Submission status: Accepted

Solution code:
import heapq

def main():
    n = int(input())
    arr = list(map(int,input().split()))
    nums = {}
    for i in range(1,n+1):
        nums[i] = []

    dp = [0]*n
    heap = []
    for i in range(n):
        nums[arr[i]].append(i)
        if arr[i] == 1:
            dp[i] = 1
            heapq.heappush(heap,(arr[i],i))

    if not heap:
        print(-1)
        return
    visited = set()
    while heap:
        curr = heapq.heappop(heap)
        if curr not in visited:
            visited.add(curr)
            index = curr[1]
            num = curr[0]
            #print(curr,num)
            if num+1 in nums.keys():
                for kid in nums[num+1]:
                    if kid > index:
                        #print(num,kid,dp[kid])
                        dp[kid] = max(dp[kid],dp[index]+1)
                        heapq.heappush(heap,(num+1,kid))

    #print(dp)
    print(n-max(dp))
    
    

main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(i) for i in input().split())

number = 1
break_countr = 0

for k in range(n):
  if(a[k] == number):
    number += 1
  else:
    break_countr += 1

if(number == 1):
  if(1 not in a):
    print(-1)
  else:
    print(break_countr)
else:
  print(break_countr)

Submission status: Accepted

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