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 pysnooper
#import numpy
#import os,re,sys,operator
#from collections import Counter,deque
#from operator import itemgetter,mul
#from itertools import accumulate#,combinations,groupby,combinations_with_replacement,permutations
from sys import stdin,setrecursionlimit
#from bisect import bisect_left,bisect_right
#from copy import deepcopy
#import heapq
#import math
#import string
#from time import time
#from functools import lru_cache,reduce
#from math import factorial
#import sys
from fractions import gcd
setrecursionlimit(10**6)
input=stdin.readline

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

now=1

for i in range(n):
    if alist[i]==now:
        now+=1

if now!=1:
    print(n-now+1)
else:
    print(-1)

Submission status: Accepted

Solution code:
from math import isinf

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

ans = float('inf')
broke = 0
cur = 0
for i, a in enumerate(A):
    if a == cur + 1:
        cur += 1
        ans = min(ans, broke + N - i - 1)
    else:
        broke += 1
if isinf(ans):
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

now = 1
cnt = 0
for a in As:
    if a == now:
        now += 1
    else:
        cnt += 1
if cnt == N:
    print(-1)
else:
    print(cnt)

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	a = getlist()
	val = 1
	for i in range(N):
		if a[i] == val:
			val += 1
	if val == 1:
		print(-1)
	else:
		print(N - val + 1)
	


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
aa = inpl()

cnt = 1
for a in aa:
    if a == cnt:
        cnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

import fractions
from functools import reduce
def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)
def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm(*numbers):
    return reduce(lcm_base, numbers, 1)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

n = onem()

a = l()

c = 0

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

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())
a = list(map(int, input().split()))
prev = 0
for i in a:
    if i == prev+1:
        prev += 1
if prev == 0:
    print(-1)
else:
    print(n-prev)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    ans = 0
    n = 1
    for a in A:
        if a == n:
            n += 1
    if n == 1:
        return -1
    return N - n + 1


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

tmp = 0

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

Submission status: Accepted

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

from collections import deque, defaultdict
import itertools as ite
import sys
import math
import decimal

sys.setrecursionlimit(1000000)

INF = 10 ** 18
MOD = 10 ** 9 + 7

N = input()
a = map(int, raw_input().split())
cnt = 1
for num in a:
    if num == cnt:
        cnt += 1
if cnt == 1:
    print -1
else:
    print N - cnt + 1

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 1 << 100
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n = I()
A = LI()


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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
A=[int(i) for i in input().split()]
ans=0
for i in range(n):
  if A[i]==ans+1:
    ans+=1
print(-1 if ans==0 else n-ans)

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
if 1 not in a:
	print(-1)
	exit()
cur=0
for x in a:
	if cur+1 ==x:
		cur+=1
print(n-cur)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
c = 0
for a in A:
    if a == c + 1:
        c = a

print(N - c if c else -1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
def ii(): return int(readline())
def mi(): return map(int, readline().rstrip().split())
def li(): return list(readline().rstrip())
def lmi(): return list(map(int, readline().rstrip().split()))
def end(*arg): print(*arg); sys.exit()
# template


def main():
    n = ii()
    a = lmi()
    i = 0
    tmp = 1
    while i < n:
        if a[i] == tmp:
            tmp += 1
            i += 1
        else:
            i += 1
    if(tmp == 1):
        print(-1)
    else:
        print(n - (tmp - 1))
    return


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:
        brick+=1
        continue
    else:
        ans+=1
if ans==n:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  ans=0
  ind=1
  for x in l:
    if x==ind:
      ind+=1
    else:
      ans+=1

  if ans==n:
    return -1
  return ans

# main()
print(main())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

n = int(input())
a = [int(item) for item in input().split()]

ans = 0
prev = 0
for item in a:
    if item != prev + 1:
        ans += 1
    else:
        prev += 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
cnt = 0
for i in a:
    if j != i:
        cnt += 1
    else:
        j += 1
print(cnt if cnt != n else -1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
now = 1
res = 0
for i in a:
    if i == now:
        now += 1
    else:
        res += 1
if now == 1:
    print(-1)
else:
    print(res)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))

    now=0
    for a in A:
        if(a==now+1):
            now+=1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, a: "List[int]"):
    counter = 0
    current = 1
    for v in a:
        if v != current:
            counter += 1
        else:
            current += 1
    if counter == N:
        print(-1)
    else:
        print(counter)

    return


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:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    j = 1
    for i in range(n):
        if a[i] == j:
            j += 1
    if n-j+1 == n:
        print(-1)
    else:
        print(n-j+1)


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
from collections import deque, Counter
def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math
INF = 10 ** 9

def main():
    n = getN()
    nums = getList()
    tmp = 1
    br = n
    for num in (nums):
        # print(num, tmp)
        if num == tmp:
            br -= 1
            tmp += 1
            # print(br, num)



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

if __name__ == "__main__":
    main()

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:
    print(N-k)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()
if 1 not in ls:
    print(-1)
    exit()
res = 0
prev = 0
for x in ls:
    if x == prev + 1:
        prev = x
    else:
        res += 1
print(res)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N=I()
    A=LI()
    need = 1
    for a in A:
        if a==need:
            need+=1


    if need!=1:
        print(N-need+1)
    else:
        print(-1)
if __name__ == '__main__':
    main()

Submission status: Accepted

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


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

now=0

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

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

Submission status: Accepted

Solution code:

N = int(input())

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

ans = N

now = 1
for i in range(N):

    if a[i] == now:
        ans -= 1
        now += 1

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

Submission status: Accepted

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


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

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

Submission status: Accepted

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

ind = 1
ans = 0
flag = False
for i in range(n):
    if arr[i] != ind:
        ans += 1
    else:
        ind += 1
        flag = True

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict, deque
from operator import itemgetter
from heapq import heappop, heappush
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))
MOD = 10**9 + 7

N = int(input())
A = inpl()
ans = 0
x = 1
for a in A:
    if x == a:
        x += 1
    else:
        ans += 1
if (ans == N):
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

N = int(sys.stdin.buffer.readline())
A = list(map(int, sys.stdin.buffer.readline().split()))

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

p = 0
ans = 0
for i in range(1, N + 1):
    while p < N and A[p] != i:
        p += 1
        ans += 1
    p += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
import sys
import bisect
import math
import itertools
import string
import queue
import copy
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
 
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return [list(map(int, input().split())) for _ in range(n)]

def main():
    n=inp()
    a=inpl()
    ans = 0
    pre = 1
    for i in range(n):
        if a[i]==pre:
            pre+=1
            ans+=1
    if ans==0:
        print(-1)
        return
    print(n-ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
# input = sys.stdin.readline
# from bisect import *
# from collections import *
# from heapq import *
# import functools
# import itertools
# import math
INF = 500000
MOD = 10**9+7

n = int(input())
a = [int(x) for x in input().split()]
if 1 not in a:
    print(-1)
    exit()
i, k = 0, 1
cnt = 0
while i < n:
    while i < n:
        if a[i] == k:
            cnt += 1
            i += 1
            k += 1
            break
        i += 1
print(n-cnt)

Submission status: Accepted

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

now = 1
brake = 0
for i in a:
    if i == now:
        now += 1
    else:
        brake += 1

if brake == n:
    brake = -1
print(brake)

Submission status: Accepted

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

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

i = 0
a = 1
while True:
    try:
        j = aaa.index(a, i)
    except 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()))

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:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
minans = inf ;ans = 0 ;count = 0 ;pro = 1

n=int(input())
A=list(map(int,input().split()))
k=1
for i ,ai in enumerate(A):
    if ai!=k:
        count+=1
    else:
        k+=1
if count==n:
    print(-1)
else:
    print(count)

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:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    k = 1
    ans = 0
    for i in range(n):
        if a[i] != k:
            ans += 1
        else:
            k += 1
    if k == 1:
        print(-1)
        return
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

show_flg=False
show_flg=True
#for i in range(n):

ans=0

n=I()
a=LI()
x=0
c=1
for i in a:#range(n):
    if i==c:
        x+=1
        c+=1
    
ans=n-x
if c==1:
    ans=-1
print(ans)

Submission status: Accepted

Solution code:
import itertools, heapq, bisect
from collections import defaultdict as ddic, deque, Counter

rr = raw_input
rri = lambda: int(raw_input())
rrm = lambda: map(int, raw_input().split())
MOD = 10 ** 9 + 7

###
N = rri()
A = rrm()

cur = 1
ans = 0
for x in A:
    if x == cur:
        cur += 1
        continue
    else:
        ans += 1
print ans if ans < N else -1

Submission status: Runtime Error

Solution code:
from copy import deepcopy

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    for bit in range(1<<N):
        B = deepcopy(A)
        removes = []
        for i in range(N):
            if bit>>i&1:
                removes.append(i)
        cnt = 0
        for r in removes[::-1]:
            del B[r]
        for i, b in enumerate(B):
            if i+1 == b:
                cnt += 1
        if cnt == len(B):
            print(len(removes))
            exit()
    print(-

Submission status: Runtime Error

Solution code:
import sys

n=int(input())
a=list(int(x) for x in input().split())

count=0
z=True
j=0

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

for k in range(j,n):
    if a[k]==count+1:
        j=k
        count=count+1

print(n-count)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

seta = set(a)
ans = 0
if 1 not in seta:
  print(-1)
  sys.exit()
for i in range(1,n+1):
  if i in seta:
    idx = a.index(i)
  else:
    ans += len(a) 
    break 
  ans += idx
  a = a[idx+1:]
  
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
## 63
import sys

n = int(input())
a = list(map(int,input().split()))
num = len(a)
point1 = 0
point2 = 0

try:
    point1  = a.index(1)
except:
    print(-1)
    sys.exit()
a = a[point1:]

point1 = 1

for i in range(2,n):
    try:
        point2  = a.index(i)
    except:
        break
    print(a[:point1],a[point2:])
    a = a[:point1] + a[point2:] 
    print(a)
    point1 += 1
    
print(num-len(a))