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

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

Submission status: Accepted

Solution code:
n = int(input())
seq = [int(x) for x in input().split()]
cur_id = 1
ans = 0
for i in range(len(seq)):
  if seq[i] == cur_id:
    cur_id += 1
  else:
    ans += 1
if cur_id == 1:
  ans = -1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
K = 0
p = 0
bricks = [[] for i in range(N)] #iと書かれたレンガがどこにあるか

for i in range(N):
  bricks[a[i] - 1].append(i)

for i in range(N):
  if len(bricks[i]) == 0:
    break
  elif bricks[i][-1] < p:
    break
  else:
    for j in bricks[i]:
      if j >= p:
        p = j
        K += 1
        break
if K == 0:
  print(-1)
else:
  print(N - K)

Submission status: Accepted

Solution code:
import sys
 
def main():
    N = int(input())
    a = list(map(int,input().split()))
    num = 1
 
    for i in a:
        if(i==num):
            num += 1
 
    if(num!=1):
        print(len(a) - num + 1)
    else:
        print('-1')
 
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

i = 0

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

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

Submission status: Accepted

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

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

if cnt + ans == N and cnt != 0:
    print(ans)
else:
    print(-1)

Submission status: Accepted

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


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

Submission status: Accepted

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

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

print([ans, -1][i==1])

Submission status: Accepted

Solution code:
from bisect import bisect_right, bisect_left
from itertools import groupby, permutations, combinations
from fractions import gcd
import numpy as np
from sys import stdin, stderr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def main():
    N = int(input())
    a = [int(x) for x in input().split()]
    cnt = 1
    ret = 0
    for i in range(N):
        if a[i] == cnt:
            cnt += 1
        else:
            ret += 1
    if ret == N:
        ret = -1
    return ret


if __name__ == "__main__":
    print(main())

Submission status: Accepted

Solution code:
class AC:
    @staticmethod
    def inputStr(char):
        return input().split(char)
    
    @staticmethod
    def inputInt(char):
        a = input().split(char)
        for i in range(len(a)):
            a[i] = int(a[i])
        return a

N  = int(input())
a = AC.inputInt(" ")

counter_1 = 1 #探す番号
counter_2 = 0 #ループ用
delete = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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
    else:
        continue
if now == 1:
    print(-1)
else:
    print(N-(now-1))

Submission status: Accepted

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

tmp = 1
for l in L:
    if l == tmp:
        tmp += 1
if tmp == 1:
    print(-1)
else:
    print(N-tmp+1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if not 1 in A:
    res = -1
else:
    b = 1
    B = []
    for a in A:
        if a == b:
            B.append(b)
            b += 1
    res = N - len(B)

print(res)

Submission status: Accepted

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

num = 1

for a in A:
    if(a == num):
        num += 1

ans = N - num + 1

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

cur = 1
ans = 0
for _a in a:
    if _a == cur:
        cur += 1
    else:
        ans += 1

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

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    a_list = list(map(int,input().split()))

    count = 1
    ans = 0
 
    for i in range(N):
        if a_list[i] != count:
            ans += 1
        else:
            count += 1
 
    if ans == N:
        print(-1)
    else:
        print(ans)
    
 
if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

temp=1
res=[]
for x in a:
  if x == temp:
    temp+=1

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

Submission status: Accepted

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

temp=1
res=[]
for x in a:
  if x == temp:
    temp+=1

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

Submission status: Accepted

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

start_num = 1
result_list = []
for num in nums:
    if num == start_num:
        result_list.append(num)
        start_num += 1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

break_num = 0
counter = 1

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

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

Submission status: Accepted

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

#administrasion nxt nas
nxt = 1
ans = 0

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

Submission status: Accepted

Solution code:
n = int(input())
ls = list(map(int, input().split()))
k = 1
lo = 0
ans = 0
for x in range(n):
    if ls[x] == k and k == 1:
        lo = x
        ans = x
        k += 1
      
    elif ls[x] == k:
        ans += x-1 - lo
        lo = x
        k += 1
      
    else:
        pass
if k == 1:
    print(-1)
else:
    print(ans+((n-1)-lo))

Submission status: Accepted

Solution code:
n = int(input())
cnt = 1
break_cnt = 0
input_brick = map(int, input().split(' '))
flag = False
for i in input_brick:
    if i == 1:
        flag = True
    if cnt == i:
        cnt += 1
        continue
    break_cnt += 1
if flag:
    print(break_cnt)
else:
    print('-1')

Submission status: Accepted

Solution code:
n = int(input())
lis = list((map(int, input().split())))
flug = 1
for i in lis:
    if i == flug:
        flug += 1
if flug == 1:
    print('-1')
else:
    print(n - flug + 1)

Submission status: Accepted

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

p=1
for i in a:
  if i==p:
    p+=1
if p!=1:
  print(n-p+1)
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

now = 1
br = 0
for j, i in enumerate(a):
  if i == now:
    now += 1
    id.append(j)
  else:
    br += 1

#a = [a[i] for i in id]
if len(id) == 0:
  print(-1)
else:
  print(br)

Submission status: Accepted

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

Submission status: Accepted

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

c = 0


for i in range(N):
    
    if input_a[i] == c+1:
        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()))
brick = [0]
number = 1

import sys

if min(a) > 1:
  print('-1')
  sys.exit()

for i in range(N):
  if a[i] == number:
    brick.append(number)
    number += 1
print(N - max(brick))

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(i) for i in input().split()]
count = 0
for i in range(n):
    if a_list[i] == count + 1:
        count += 1
if count > 0:
    print(n - count)
else:
    print(-1)

Submission status: Accepted

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

for i in range(len(a)):
  if a[i] != point:
    ans += 1
  else:
    point += 1
if point == 1:
  print("-1")
else:
  print(ans)

Submission status: Accepted

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

    cnt =  1
    flag = False
    for i in a_list:
        if i == cnt:
            cnt+=1
            flag = True
            continue

    ans = n - cnt + 1
    if not flag:
        ans = -1
    print(ans)

main()

Submission status: Accepted

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

i = 1
count = 0
for e  in blocks:
    if e == i:
        count += 1
        i += 1

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

Submission status: Accepted

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

# process/output
if not 1 in alist:
    print(-1)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

renga_count = int(input())
renga_list = [int(i) for i in input().rstrip().split(" ")]

count = 0
for i in renga_list:
    if i == count + 1:
        count += 1

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

Submission status: Accepted

Solution code:
_ = input()
l = list(map(int, input().split()))
br = 0
cn = 1
for i, n in enumerate(l):
	#print([i,n,cn])
	if n != cn:
		br += 1
	else:
		cn += 1
if br == len(l):
	print(-1)
else:
	print(br)

Submission status: Accepted

Solution code:
from functools import reduce
N = int(input())
a = tuple(map(int, input().split()))
b = reduce(lambda i, x: i + 1 if i == x else i, a, 1)
print((N - b + 1) if b > 1 else -1)

Submission status: Accepted

Solution code:
N = int(input())
nums = [int(s) for s in input().split()]
 
min_breaks = -1
 
count_num = 1
break_index = 1

for i in range(N):
  if nums[i] == count_num:
      if min_breaks == -1:
          min_breaks = 0
      if count_num == 1:
      	 min_breaks += ((i+1) - break_index)
      else:
         min_breaks += ((i+1) - break_index - 1)
      
      break_index = (i+1)
      count_num+=1

if break_index < N and min_breaks != -1:
  min_breaks += ((i+1) - break_index)
  
print(min_breaks)

Submission status: Accepted

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

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)


def solve(N: int, a: "List[int]"):
    target = 1
    ret = 0
    for v in a:
        if v != target:
            ret += 1
        else:
            target += 1
    if target == 1:
        ret = -1
    print(ret)
    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:
import sys
input = sys.stdin.readline

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

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

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

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  a = [int(s) - 1 for s in input().split()]
  res = 0
  for i in range(n):
    if a[i] != (i-res):
      res += 1
  if res == n:
    print(-1)
  else:
    print(res)
    
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n=int(input())
t=tuple(map(int,input().split()))
c=0
now=1
for i in range(n):
  if t[i]!=now:
    c+=1
  else:
    now+=1
if now==1:
  print(-1)
else:
  print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n1 = int(input())
n2 = [int(i) for i in input().split()]

t3 = 0

for i4 in range(n1):
    if n2[i4] != i4 - t3 + 1:
        t3 += 1



if t3 == n1:
    print(-1)
else:
    print(t3)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
an = list(map(int, input().split()))
enable = 0

if not 1 in an:
  enable = 1
  
count = 0
block = 1
for i in range(N):
  if an[i] != block:
    count += 1
  else:
    block += 1
    
if enable == 1:    
  print(-1)
else:
  print(count)

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
a = nl()

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

if(cnt==0):
  print(-1)
  exit()

print(shift)

Submission status: Accepted

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

ans = 0
block = 1
no_block = False

for i in range(n):   
    if a[i]!=block:
        ans+=1
    else:
        no_block = True
        block+=1

if no_block is False:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

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

    current += 1

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

Submission status: Accepted

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

now = 0
for x in a:
    if  x == 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()))
# print(N, A)
index = 1
ans = 0
found = False
for i in range(N):
    if A[i] == index:
        found = True
        index += 1
    else:
        ans += 1

print(ans if found else -1)

Submission status: Accepted

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

cnt = 0
s = 1
for a in al:
    if a==s:
        s += 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()))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC148D - Brick Break
import sys

read = sys.stdin.buffer.read


def main():
    N, *A = map(int, read().split())
    remaining = 0
    target = 1
    for i in A:
        if i == target:
            remaining += 1
            target += 1
    print(N - remaining if remaining else -1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC148D - Brick Break
def main():
    N, *A = map(int, open(0).read().split())
    remaining = 0
    target = 1
    for i in A:
        if i == target:
            remaining += 1
            target += 1
    print(N - remaining if remaining else -1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC148D - Brick Break
def main():
    N, *A = map(int, open(0).read().split())
    remaining = 0
    cur = 0  # current index
    for target in range(1, N + 1):
        be_kept = False
        for i in range(cur, N):
            if A[i] == target:
                remaining += 1
                cur = i + 1
                be_kept = True
                break
        if not be_kept:
            break
    print(N - remaining if remaining else -1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
lst = [int(x) for x in input().split()]
i = 1
ret = 0
for x in lst:
    if x == i:
        i += 1
    else:
        ret += 1

if len(lst) == ret:
    print(-1)
else:
    print(ret)

Submission status: Accepted

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

ans = 0
memo = 1
for i in range(n):
    if a[i] == memo:
        memo += 1
    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,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
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 list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = LI()
    t = 1
    for c in a:
        if c == t:
            t += 1
    if t == 1:
        return -1

    return n - t + 1


print(main())

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for ii in range(n):
    #print(ii, a[ii])
    if count == a[ii]:
        count += 1
#print(count)
if count == 1:
    print(-1)
else:
    ans = n - (count - 1)
    print(ans)

Submission status: Accepted

Solution code:
import math
import bisect
N = int(input())
c  = 0 #あったらあかんやつ
now = 1
nums = list(map(int,input().split(" ")))
for temp in nums:
    if temp == now:

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    lst = [int(s) for s in input().split()]
    if lst.count(1) < 1:
        print(-1)
        return
    current = 1
    for i in lst:
        if i == current:
            current += 1

    answer = N - current + 1
    print(answer)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

if len(l) > 0:
  print(N - len(l))
else:
  print(-1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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


num = 1
_index = None
num_index_list = []

for i in range(N):
    if original_list[i] == num:
        num_index_list.append([num, i])
        num += 1
    else:
        pass

time = N - len(num_index_list)

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

Submission status: Accepted

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

k = 1
S = 0

for a in A:
    if k == a:
        S += 1
        k += 1
if S:
    print(N-S)
else:
    print(-1)

Submission status: Accepted

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

now = 1

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

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

Submission status: Accepted

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

j=1
k=0
for i in a:
  if i==j:
    k+=1
    j+=1

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

Submission status: Accepted

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

Submission status: Accepted

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

    if not 1 in an:
        print('-1')
        return

    count = 1
    for a in an:
        if a == count:
            count += 1
    print(N - count + 1)

main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N = int(input())
a_N = list(map(int, input().split(' ')))
break_count = 0
remain_count = 1
for a_n in a_N:
    if a_n == remain_count:
        remain_count += 1
    else:
        break_count += 1
if break_count == N:
    print(-1)
else:
    print(break_count)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split(" ")))
    i = 1
    flag = True
    for a in A:
        if a == i:
            flag = False
            i += 1
    if flag:
        print(-1)
    else:
        print(len(A) - i + 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
'''input
3 5
1 2 1
2 3 0
'''
# connected components
from sys import stdin, setrecursionlimit
from bisect import bisect_left
import sys, threading


# main start
n = int(stdin.readline().strip())
arr = list(map(int, stdin.readline().split()))
k = 0
for i in range(n):
    if arr[i] == k + 1:
        k += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

c = 1
res = 0
for a in A :
    if c == a :
        c += 1
    else :
        res += 1

print(res if not res == n else -1)

Submission status: Accepted

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

num = 1
ans = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = input().split()
#print("input",a)

ans = []
count = 0
x = 1
for i in a:
    if x == int(i):
        x += 1
        ans.append(i)
    else:
        count += 1

#print("ans",' '.join(ans))
if count != n:
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
numN = int(input())
input_line = input().split(' ')
brick = [int(i) for i in input_line]

seq = 0

for i, b in enumerate(brick):
  if b == 1:
    seq += 1
    cur = 2
    for c in brick[i + 1:]:
      if c == cur:
        seq += 1
        cur += 1
    break

if seq == 0:
  print(-1)
else:
  print(numN - seq)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
index = []
num = 1
tmp = 0
for i,a in enumerate(A):
    tmp += 1
    if a == num:
        index.append(i+1)
        num += 1
        tmp = 0
        continue
if num == 1:
    print(-1)
else:
    ans = index[0]-1
    for i in range(1, len(index)):
        ans += index[i] - index[i-1] - 1
    ans += tmp
    print(ans)