Problem:


Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.



Sample input:
3
2 1 2


Sample output:
1

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

import sys
# import math
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
from itertools import count, cycle, repeat     # count(start[,step]), cycle(iter), repeat(elm[,n])
from itertools import groupby                  # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
from itertools import starmap                  # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product                # product(iter, repeat=n)
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference


def main():
    mod = 1000000007                  # 10^9+7
    inf = float('inf')
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():  return int(input())
    def mi():  return map(int, input().split())
    def mi_0(): return map(lambda x: int(x)-1, input().split())
    def lmi(): return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():  return list(input())
    
    
    n = ii()
    a = lmi()
    c = 1
    ans = 0
    for i in range(n):
        if a[i] == c:
            c += 1
        else:
            ans += 1
    print(ans) if ans != n else print(-1)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math, sys
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache
from heapq import heapify, heappop, heappush
from itertools import accumulate, combinations, permutations
input = sys.stdin.readline
mod = 10**9 + 7
ns = lambda: input().strip()
ni = lambda: int(input().strip())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
a = nl()

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

a = deque(a)
i = 1
ans = 0
while a != deque():
    if a[0] == i:
        a.popleft()
        i += 1
        ans += 1
    else:
        a.popleft()

print(n - ans)

Submission status: Accepted

Solution code:
def myAnswer(N:int,A:list) -> int:
   i = 1
   counter = 0
   for a in A:
      if(a == i):
         i+=1
      else:
         counter +=1
   return counter if(1 in A) else -1


def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))

if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    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:
        cnt += 1
        continue
    num += 1
print(cnt if cnt != n else -1)

Submission status: Accepted

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

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

if a.count(1) == 0:
    print(-1)
    exit()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
num = 1
count = 0
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:
x = int(input())
li = list(map(int, input().split()))
k = 1
s = 0
for i in range(len(li)):
    if k == li[i] :
        k += 1
        s += 1
    if i == len(li)-1 and k == 1:
        s = len(li) + 1
print(len(li)-s)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque
import sys
import heapq
import bisect
import itertools
import queue
import copy
import 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())

def inpl_strl(): return list(sys.stdin.readline())

n = inp()
s = inpl()
b = 1
if s.count(1) == 0:
    print(-1)
else:
    for i in range(n):
        if s[i] == b:
            b += 1
    print(n-(b-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import fractions
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))




N=input1()
a=input_array()
num=1
broken=[]
flag=False
for i in range(N):
	if a[i]!=num:
		broken.append(a[i])
	else:
		num+=1
		flag=True

if flag:
	print(len(broken))
else:
	print(-1)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
a = LIST()
cnt = 0
c = 1

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    A=[int(_) for _ in input().split()]
    cunt=0
    for a in A:
        if a==cunt+1:cunt+=1
    if 1 in A:
        if n==1 and A[0]==1:print(0)
        else:print(n-cunt)
    else:
        print(-1)
        

if __name__=='__main__':
    main()

Submission status: Accepted

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

is_found = False
current = 1
num_of_broken_bricks = 0
for ai in a:
    if ai == current:
        is_found = True
        current += 1
    else:
        num_of_broken_bricks += 1

if not is_found:
    print(-1)
    exit()

print(num_of_broken_bricks)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(n - x + 1 if x > 1 else -1)

Submission status: Accepted

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

check = 1
ans = 0
for x in a_li:
    if x == check:
        check+=1
    else:
        ans+=1
        
if check==1:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for a in L:
    if a != checker:
        ans += 1
    else:
        checker += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

i=0
j=1
exe=False
for n in range(N):
    if a[n]!=j:
        i+=1
            
    else:
        exe=True
        j+=1

if exe==False:
    print(-1)
else:
    print(i)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N=int(input())
    A=list(map(int,input().split()))
    flag=False
    cnt=1
    for i in range(N):
        if A[i]==cnt:
            cnt+=1
            flag=True
        else:
            continue
    if not flag:
        print(-1)
    else:
        print(N-cnt+1)





resolve()

Submission status: Accepted

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

Submission status: Accepted

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

cur = 1
cnt = 0
for a in A:
    if (a == cur):
        cur += 1
    else:
        cnt += 1

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

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())

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

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

ans = Counter(a)[0]

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

Submission status: Accepted

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

Submission status: Accepted

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

nex = 1
ans = []

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

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

Submission status: Accepted

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


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

print(D)

Submission status: Accepted

Solution code:
import sys
si = sys.stdin.readline
so = sys.stdout.write


def main():
    n = int(si())
    l = [int(e) for e in si().split()]

    bn = 1
    count = 0
    for i in range(n):
        if l[i] == bn:
            bn += 1
        else:
            count += 1

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# 2020/04/12
# AtCoder Beginner Contest 148 - D

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

# Init
wkidx = 0
wk_i = -1

# Search
for i in range(1, n+1):
    foundflg = False
    for j in range(wkidx, n):
        if a[j] == i:
            wkidx = j + 1
            foundflg = True
            break
    
    if foundflg == False:
        wk_i = i
        break

if wk_i == -1:
    # Case sorted
    ans = 0
elif wk_i == 1:
    # Case 1 not found
    ans = -1
else:
    # Case other
    ans = n - (wk_i - 1)

# Output
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

"""

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
ans = 0
if 1 in A:
    num = A.index(1)
    ans += num
    A = A[num:]
else:
    print(-1)
    exit()
f = 0
#print(A,ans)
for i in range(2,n+1):
    try:
        d = A.index(i,f)
    except ValueError:
        ans += len(A[f+1:])
        print(ans)
        exit()
    ans += d - f - 1
    f = d
print(ans)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

n = ii()
A = iil()
ans = 0
if 1 in A:
    num = A.index(1)
    ans += num
    A = A[num:]
else:
    print(-1)
    exit()
f = 0
#print(A,ans)
for i in range(2,n+1):
    try:
        d = A.index(i,f)
    except AttributeError:
        ans += len(A[f+1:])
        print(ans)
        exit()
    except ValueError:
        ans += len(A[f+1:])
        print(ans)
        exit()
    ans += d - f - 1
    f = d
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# D - Brick Break

N = int(input())
A = list(int(x) for x in input().split())

ans = 0
idx = 1

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

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

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

Submission status: Accepted

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

dp = [float('inf') for _ in range(N)]

for i in range(N):
    a = A[i]
    idx = bisect.bisect_left(dp, a)
    dp[idx] = a

s = [i for i, d in enumerate(dp) if i + 1 == d]
if len(s) == 0:
    print(-1)
else:
    print(N - 1 - max(s))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

now = 1
for i in a:
    if(i == now):
        now += 1

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

Submission status: Accepted

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

count = 1
for a in As:
    if count == a:
        count += 1

res = N - count + 1
if res == N:#全部砕く必要がある　➡︎　無理なので-1
    res = -1
print(res)

Submission status: Accepted

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

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

Submission status: Accepted

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


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

#print(B,number)

if len(B)==0:
    print(-1)
else:
    print(N-len(B))

Submission status: Accepted

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

search = -1
for a in alist:
  if a == search + 2:
    search = search + 1

ans = (len(alist) - search - 1) if search >= 0 else -1
print(ans)

Submission status: Accepted

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

check=1
for i in lis:
    if i==check:
        check+=1
    
if check!=1: 
    print(len(lis)-(check)+1)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

for n in range(N):
    if a[n] != broken + 1:
        continue
    broken += 1

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

Submission status: Accepted

Solution code:
n=int(input())

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

d=1
flag=False

cnt=0

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

if not flag:
  print("-1")
else:
  print(n-cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
L = []
n = 0
a = 0
for i in range(1,N+1):
    if n >= N:
        break
    elif a == N-1:
        break
    else:
        for j in range(n,N):
            if A[j] == i:
                L.append(A[j])
                n = j+1
                break
            elif j == N-1:
                a = N-1
                break
if L != []:
    print(len(A)-len(L))
elif len(A) == 1:
    print(0)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
i = 0
num = 1
cnt = 0
while i <=n-1:
    if L[i] == num:
        num +=1 
    else:
        cnt +=1
    i+= 1
if num == 1:
    print(-1)
else:
    print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
R = list(map(int, input().split()))
c, i = 0, 1
for r in R:
  if r==i: i+=1
  else: c+=1
print(c if c<n else '-1')

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
r = deque(map(int, input().split()))
i=0
while True:
    if max(len(r),i)==i:
        break
    if r[i]!=i+1:
        r.popleft()
    else:
        i+=1
print('-1' if len(r)==0 else n-len(r))

Submission status: Accepted

Solution code:
n,a=open(0);j=0
for b in map(int,a.split()):
    if b==j+1:j+=1
print([-1,int(n)-j][j>0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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

Submission status: Accepted

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

nxt = 1
ans = 0

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

if ans == N: ans = -1

print(ans)

Submission status: Accepted

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

try:
  position1 = li.index(1)        
except:
  print(-1)
  exit()  
  
kowasu = position1
target = 2
position = position1 + 1
while True:
  flag = False
  for i in range(position, len(li)):
    if li[i] == target:
      flag = True
      kowasu += i - position
#      print(kowasu)
#      print(i)
      tekito = i
      break
  if flag:
    target += 1
    if tekito == len(li) - 1:
      break
    position = tekito + 1
  else:
    kowasu += len(li) - position
    break
print(kowasu)

Submission status: Accepted

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

Submission status: Accepted

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


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

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

Submission status: Accepted

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

Submission status: Accepted

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

i = 1
# j = 0

if 1 in set(a):
    # while j < N and i in set(a[j:]):
    #     j = a[j:].index(i) + j
    #     # print(i, j)
    #     i += 1
    for a_i in a:
        if a_i == i:
            i += 1
    print(N - i + 1)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())

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

if 1 not in A:
  print(-1)
else:
  index = []
  current = 1
  cnt = 0
  for a in A:
    if a == current:
      index.append(cnt)
      current += 1
    cnt += 1
  print(n-len(index))

Submission status: Accepted

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

ind1 = 0

for i in a:
    if i == 1:
        break
    ind1 += 1


if 1 in a:
    b.append(1)
    for i in range(ind1, len(a)):
        if a[i] == b[-1] + 1:
            b.append(a[i])

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

Submission status: Accepted

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


def solve(N: int, a: "List[int]"):
    currentIndex = 1
    discarded = 0
    for i in range(N):
        if a[i] != currentIndex:
            discarded += 1
        else:
            currentIndex += 1
    if discarded == N:
        print(-1)
        exit()
    print(discarded)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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()))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int, input().split())
min_v = 1
cnt = 0
l = len(A)
for a in A:
    if min_v == a:
       cnt += 1
       min_v += 1
if cnt == 0:
    print(-1)
else:
    print(l - cnt)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(lambda x:int(x), input().split(' ')))
# サーチ用変数定義
C = 1
H = 0
# サーチ処理
for i in A:
  if  i==C:
    H += 1
    C += 1
# hitした数を総和から消す
print( N-H if H > 0 else -1)

Submission status: Accepted

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

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

if cnt == 0:
    print(-1)

else:
    print(n-cnt)