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
from io import StringIO
import unittest
import os


# 実装を行う関数
def resolve():
    # 数値取得サンプル
    #   1行N項目 x, y = map(int, input().split())
    #   N行N項目 x = [list(map(int, input().split())) for i in range(n)]
    n = int(input())
    bricks = list(map(int, input().split()))

    # 1が無ければ-1
    if 1 not in bricks:
        print(-1)
        return

    num = 1
    ban = 0
    for brick in bricks:
        if brick == num:
            num += 1
            continue
        ban += 1

        # dev
        1 == 1

    print(ban)





    # 文字取得サンプル
    #   1行1項目 x = input()
    pass


# テストクラス
class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """3
2 1 2"""
        output = """1"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """3
2 2 2"""
        output = """-1"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """10
3 1 4 1 5 9 2 6 5 3"""
        output = """7"""
        self.assertIO(test_input, output)

    def test_input_4(self):
        test_input = """1
1"""
        output = """0"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def tes_t_1original_1(self):
        test_input = """データ"""
        output = """データ"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

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
    continue
  cnt += 1

print(cnt if num > 1 else -1)

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math

sys.setrecursionlimit(10 ** 7)

from collections import defaultdict

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(input())
A = map(int, input().split())

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

nxt = 1
broken = 0
for i in range(n):
  if B[i]==nxt:
    nxt += 1
  else:
    broken += 1
    
if broken==n:
  print(-1)
else:
  print(broken)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
cur = 1
for i in A:
  if i == cur:
    cur += 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()))

res = 0
idx = 1
for v in a:
    if v == idx:
        idx += 1
    else:
        res += 1

if idx == 1:
    print(-1)
else:
    print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [0] + list(map(int, input().split()))

if 1 not in a:
    ans = -1
    print(ans)
else:
    broken = []
    survived = []
    latest = 1
    for i in range(1,N+1):
        if a[i] != latest:
            broken.append(a[i])
        else:
            latest += 1
            survived.append(a[i])
    print(len(broken))

Submission status: Accepted

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

Submission status: Accepted

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

count = []
n_count = 0
now_num = 1

for i in range(N):
  if a[i] == now_num:
    n_count += 1
    now_num += 1
else:
  if n_count != 0:
    count.append(i - (n_count - 1) + N - (i + 1))
    
if count:
  print(min(count))
else:
  print(-1)

Submission status: Accepted

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

print(N-x+1 if x != 1 else -1)

Submission status: Accepted

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

cnt = 0
now = 1
for i, v in enumerate(a):
    if v == now:
        now += 1
        continue
    else:
        cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 0
    tmp = 1
    for i in range(n):
        if a[i] == tmp:
            tmp +=1
        else:
            ans += 1
    if ans == n and n != 1:
        print(-1)
    else:
        print(ans)
    
      
if __name__ == '__main__':
    main()

Submission status: Accepted

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

now=1
for x in a:
  if x==now:
    now+=1
if now==1:
  now=N+2
print(N-now+1)

Submission status: Accepted

Solution code:
N=int(input())
S=list(map(int,input().split()))
first=len(S)
count=0

mokuteki=1
block=0
while count<N:
    if S[count]!=mokuteki:
        
        block+=1
    else:
        mokuteki+=1
        
    count+=1

if mokuteki>1:
    print(block)
else:
    print(-1)

Submission status: Accepted

Solution code:
N=int(input())
S=list(map(int,input().split()))
first=len(S)
count=0
pos=0
mokuteki=1
block=0
while count<N:
    if S[pos]!=mokuteki:
        pos+=1
        block+=1
    else:
        mokuteki+=1
        pos+=1
    count+=1

if mokuteki>1:
    print(block)
else:
    print(-1)

Submission status: Accepted

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

for i in range(len(A)):
    if A[i]==number_count:
        number_count+=1
    else:
        ans_count+=1
        
if number_count!=1:print(ans_count)
else:print("-1")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

n = I()
a = Intl()
cnt = 0
if not 1 in a:
    print(-1)
    exit()
now = 1
for i in range(n):
    if now == a[i]:
        now += 1
    else:
        cnt += 1
print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

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:
        ans += 1
    else:
        cnt += 1

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

Submission status: Accepted

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

break_count = 0
index = 1
for a_i in a:
    if a_i == index:
        index += 1
    else:
        break_count += 1
if index == 1:
    print(-1)
else:
    print(break_count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A=[int(i) for i in input().split()]
ans=0
k=1
for i in range(n):
    if A[i]==(k):
        k+=1
        continue
    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()))
ans=0
x=1
for i in range(N):
    if x!=A[i]:
        ans+=1
    else:
        x+=1
print(-1 if ans==N else ans)

Submission status: Accepted

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

Submission status: Accepted

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

for a in A:
  if a == num:
    num += 1
  else:
    ans += 1
    
if ans == n:
  print("-1")
else:
  print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

def LII_split():
    return list(map(int, input().split()))

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N = input_int()
a_list = LII_split()

ans = 0
# pre_idx = 0  # 前の数字のindex
current_idx = 0
continuous_cnt = 0

for i in range(1, N + 2):

    while current_idx < N:
        if a_list[current_idx] == i:
            continuous_cnt += 1
            break
        else:
            current_idx += 1
    if i == 1 and continuous_cnt == 0:
        break

print(N - continuous_cnt if continuous_cnt > 0 else -1)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().rstrip().split()]
cnt=0
now=0
for i in range(n):
  while(now<n):
    if a[now]==i+1:
      cnt+=1
      break
    else:
      now+=1

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

Submission status: Accepted

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

    ans = 0
    tmp = 1
    for aa in a:
        if aa == tmp:
            tmp += 1
        else:
            ans += 1

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

n = 1
for x in A:
    if x == n:
        n += 1
 
answer = N - n + 1
if answer == N:
    answer = -1
print(answer)

Submission status: Accepted

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

now = 1
for x in a:
  if x == now:
    now += 1
    
print(-1 if now == 1 else n-now+1)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = [0]
    for i in a:
        if i == b[-1]+1:
            b.append(i)
    c = len(a)-len(b[1:])
    print(-1 if c == len(a) else c)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

if 1 not in A:
    print(-1);exit()

tmp=0
for i in range(n):
    if A[i]==tmp+1:
        tmp+=1
print(n-tmp)

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())
a_list = list(map(int, input().split()))
dp = [0]*N

if 1 not in a_list:
    print(-1)
else:
    dp[0]= 1
    count = 0
    for i in range(N-1):
        if dp[i] == a_list[i]:
            dp[i + 1] = dp[i] + 1
        else:
            dp[i + 1] = dp[i]
            count += 1

    if dp[N-1] != a_list[N-1]:
        count += 1
        
    print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

next = 1
ans = 0
found = False
for i in range(N):
    if A[i] == next:
        next += 1
        found = True
    else:
        ans += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()


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

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

    if bl == 1:
        print('-1')
    else:
        print(N - bl + 1)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
c=n+2
for i in range(n):
    if A[i]==1:
        c=2
    elif A[i]==c:
        c+=1
print(n-c+1)

Submission status: Accepted

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

Submission status: Accepted

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

import sys
#import bisect
#from collections import deque
#import math

def input():
    return sys.stdin.readline().rstrip('\n')

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

count = 1

for a in A:
    #print(a, count)
    if a == count:
        count += 1

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

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
A=n1()

tmp=0
for a in A:
    if a==tmp+1:
        tmp+=1
print(-1 if tmp==0 else n-tmp)

Submission status: Accepted

Solution code:
n=int(input())
lis=list(map(int,input().split()))
ans=0
base=1
for i in lis:
    if i==base:
        base+=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=1
for ai in a:
  if ai==cnt:
    cnt+=1
print(n-(cnt-1) if cnt>1 else -1)

Submission status: Accepted

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

if A.count(1)==0:
  print('-1')
  exit()
  
ans=0
j=1

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

Submission status: Accepted

Solution code:
def main():
    n, *a = map(int, open(0).read().split())
    
    g = 0
    t = 1
    for x in a:
        if x == t:
            t += 1
        else:
            g += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    from collections import deque
    n, *a = map(int, open(0).read().split())
    a = deque(a)

    tar = 1
    gab = 0
    while a:
        i = a.popleft()
        if i != tar:
            gab += 1
        else:
            tar += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(raw_input())
elems = map(int,raw_input().split(' '))
wanted = 1
for e in elems:
	if e == wanted:
		wanted+=1
need= n - (wanted -1)
print need if need < n else -1

Submission status: Accepted

Solution code:
N = int(input())

a = list(map(int,input().split()))
l = []

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

s = 0
cnt = 0
for i in a:
    if i == s + 1:
        l.append(i)
        s = i
    else:    
        cnt += 1

print(cnt)

Submission status: Accepted

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

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

if(div >= N):
    print(-1)
else:
    print(div)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# N = 10
# ARR = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
#
#
# N = 3
# ARR = [2, 2, 2]
#
# N = 3
# ARR = [2, 1, 2]
#
# N = 1
# ARR = [1]

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

def calculate(n,arr):
    currentSearchIndexs = []
    currentSearch = 1
    for i in range(n):
        if arr[i] == currentSearch:
            if len(currentSearchIndexs) < currentSearch:
                currentSearchIndexs.append(i)
            else:
                currentSearchIndexs[currentSearch-1] = i

            currentSearch = currentSearch  + 1



    if len(currentSearchIndexs) == 0:
        print(-1)
        return

    print(n - len(currentSearchIndexs))

calculate(N,ARR)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = deque(map(int, input().split()))
num = 1
ans = 0
while A:
  a = A.popleft()
  if a == num:
    num += 1
  else:
    ans += 1
print(-1 if num == 1 else ans)

Submission status: Accepted

Solution code:
import sys
import  math
from heapq import *
import fractions
from collections import defaultdict
stdin = sys.stdin
     
ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N=int(input())
A=nl()
now=1
for i in range(N):
    if(A[i]==now):
        now+=1

if(now==1):
    print(-1)
    sys.exit()

print(N-now+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
ad=deque(a)
tmp=1
ans=0
while ad:
    if ad[0] ==tmp:
        tmp+=1
        ad.popleft()
    else:
        ad.popleft()
        ans+=1
if tmp==1 and ans>0:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
# For taking integer inputs.
import math


def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


N = inp()

a = inlist()

look = 1
removed = 0
for i in range(N):
    if a[i] != look and removed < N-1:
        removed += 1
    else:
        if a[i] == look:
            look += 1
if look == 1:
    print(-1)
else:
    print(removed)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

total=0
num=1
for j in range(N):
    if A[j]!=num:
        total+=1
    else:
        num+=1
if total==N:
    print(-1)
else:
    print(total)

Submission status: Accepted

Solution code:
input()
a=input().split()
b,c=0,1
for i in a:
  if int(i)==c:
    c+=1
  else:
    b+=1
print(b if c>1 else -1)

Submission status: Accepted

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

now = 1
hit = 0
for a in A:
    if a != now:
        hit += 1
    else:
        now += 1
if hit == n:
    print(-1)
else:
    print(hit)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:

N = int(input())
A = list(map(int, input().split()))
cnt = 0
check = 1

for i in A:
  if i == check:
    cnt += 1
    check += 1

if cnt == 0:
  print(-1)
else:
  print(len(A) - cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

find_num=1
break_num=0
flag = False
for i in range(n):
    if a[i] == find_num:
        find_num+=1
        flag = True
    else:
        break_num+=1

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

Submission status: Accepted

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

ans = 0
current = 1
flag = False

for a in a_list:
    if a != current:
        ans += 1
    else:
        current += 1
        flag = True

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

Submission status: Accepted

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

ans = 0
current = 1
flag = False
for a in a_list:
    if a != current:
        ans += 1
    else:
        current += 1
        flag = True
if flag:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    a = list(map(int, input().split()))
    if not 1 in a:
        print(-1)
        exit()
    f = a.index(1)
    c = 2
    ans = f-1
    for i in range(f,N):
        if a[i] == c:
            c += 1
        else:
            ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
n_len = len(a_list)
left = 0
count = 0
for i in range(1,n+1):
    for j in range(left,n):
        if a_list[j] == i:
            count += 1
            break
    left = j + 1
if count == 0:
    print(-1)
else:
    print(n_len-count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if renga>1:
  print(ans)
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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