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 unittest


class TestD(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([2, 1, 2]), 1)

    def test_2(self):
        self.assertEqual(think([2, 2, 2]), -1)

    def test_3(self):
        self.assertEqual(think([3, 1, 4, 1, 5, 9, 2, 6, 5, 3]), 7)


def solve():
    a = read()
    result = think(a)
    write(result)


def read():
    n = read_int(1)[0]
    return read_int(n)


def read_int(n):
    return read_type(int, n)


def read_float(n):
    return read_type(float, n)


def read_type(t, n):
    return list(map(lambda x: t(x), read_line().split(' ')))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(a):
    if 1 not in a:
        return -1
    next_index = 1
    count = 0

    for i in a:
        if next_index == i:
            next_index += 1
        else:
            count += 1
    return count


def write(result):
    print(result)


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# 整数の入力
N = int(input())

# スペース区切りの整数の入力
datas = list(map(int, input().split()))

idx = 1
for i in range(len(datas)):	
    if datas[i] == idx:
        idx += 1


# ans = len(datas) - (idx-1)
if idx - 1 == 0:
    print(-1)
else:
    print(len(datas) - (idx-1))

Submission status: Accepted

Solution code:
N = int(input())
raw_str = input().split()
temp = 1
breaked = 0

for i in raw_str:
    if int(i) > temp or int(i) < temp:
       breaked += 1
    else:
        temp += 1
        continue

if(breaked == N):
    if(raw_str[0] == '1'):
        print(0)
    else:
        print(-1)
else:
    print(breaked)

Submission status: Accepted

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

Submission status: Accepted

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

t=[]
d=0
for i in s:
  if i==d+1:
    d=i

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

Submission status: Accepted

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


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

    print(N-c)

Submission status: Accepted

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

cur=1
cnt=0
for x in xs:
    if x == cur:
        cnt += 1
        cur += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
len = int(input())

N = input().split(' ')

count = 1

output = 0

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

Submission status: Accepted

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

counter=0
start = 0
end = N

for i in range(N):
    try:
        index = NL.index(i+1, start, end)
        start = index+1
    except:
        break
    else:
        counter += 1


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

Submission status: Accepted

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

base = 1
l = []
for x in As:
    if x == base:
        l.append(x)
        base += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
current = 1

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

    if current != 1:
        print(ans)
    else:
        print('-1')

Submission status: Accepted

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

crashed = []
i = 1
output = -1

# 1つだけの場合
if len(nums) == 1:
  if nums[0] == 1:
    output = 0
  print(output)
# 2つ以上の場合
else:
  crash_count = 0
  for n in nums:
    if n == i:
      crashed.append(n)
      i += 1
    else:
      crash_count += 1
  success = True
  pre = 0
  if len(crashed) == 0:
    print(-1)
  else:
    for n in crashed:
      if n == pre + 1:
        pre = n
      else:
        success = False
        break
    if success:
      print(crash_count)
    else:
      print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

len = int(input())
input_str_arr = input().split(" ")
input = [int(n) for n in input_str_arr]

# 基準値
base = 1
count = 0

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

for i in range(len):
    if(base == input[i]):
        base+=1
        continue
    count+=1

print(count)

Submission status: Accepted

Solution code:
def main(n: int, a: list):
    ans = 0

    i = 1

    if 1 not in set(a):
        print(-1)
        return

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

    print(ans)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int, input().split()))

    main(n, a)

Submission status: Accepted

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

num = 1
counter = 0

for n in range(N):
    if S[n] == num:
        num += 1
    else:
        counter += 1

if(N == counter):
    counter = -1

print(counter)

Submission status: Accepted

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

Submission status: Accepted

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


kari = int(1)
an = 0


for i in range(N):
    if n[i] == kari:
        kari = kari +1
        
       


an = 1 + N-kari


if min(n) > 1.5 :
    ANS = -1
else:
    ANS = an

print(ANS)

Submission status: Accepted

Solution code:
n = int(input())

check_num = 1
delete_num = 0
for i in input().split():
  i = int(i)
  if i == check_num:
    check_num += 1
  else:
    delete_num += 1

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

Submission status: Accepted

Solution code:
N = int(input())
seq = list(map(int, input().split()))
best = float('inf')
if 1 in seq:
    best = N - 1
else:
    print(-1)
    quit()

cur = 1
todel = 0
for i in range(N):
    if seq[i] == cur:
        cur += 1
    else:
        todel += 1
best = min(best, todel)

print(best)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
li = list(map(int, input().split()))

list = list(np.arange(1, N+1))
check = 1
for i in range(N):
    if li[i] == check:
        check += 1

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

Submission status: Accepted

Solution code:
#!python3.4.3

n = int(input())
a_list = list(map(int, input().split(' ')))
result = []

c = 0
for i in range(1, n+1):
    for j in range(c, n):
        if i == a_list[j]:
            result.append(i)
            c = j
            break
    if len(result) != i:
        break

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
i=0
count,j = 0,1
while i < n:
  if j == l[i]:
    count+=1
    j+=1
  i+=1
if count!=0 or n==count:
  print(n-count)
else:
  print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
x = []
j = 1
for i in range(n):
  if a[i] == j:
    x.append(a[i])
    j += 1

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

Submission status: Accepted

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

answer = 0
check = 1
for i in a:
    if i != check:
        answer += 1
    else:
        check += 1
if check == 1:
    print(-1)
else:
    print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

tmp = 0
count = 0

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

Submission status: Accepted

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

p = 0
c = 1
i = 0
ans = 0
while i < n:
    if a[i] == c:
        p = i + 1
        c += 1
    else:
        ans += 1

    i += 1



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

Submission status: Accepted

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

print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import os

def problem148D(N, alist):
  index = 0
  count = 0
  number = 1
  while index < N:
    if alist[index] == number:
      number += 1
    else:
      count += 1
    index += 1
  if count == N:
    count = -1
  print(count)

if __name__ == "__main__":
  N = int(input())
  alist= list(map(int, input().split()))
  problem148D(N, alist)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

k = 1
cnt = 0

for i in range(n):
    if ls_a[i] != k:
        cnt += 1
    elif ls_a[i] == k:
        k += 1

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

Submission status: Accepted

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

c = 1
cp = 0

for i in range(N):
  ai = an[i]
  if ai != c:
    cp += 1
    
  elif ai == c:
    c += 1
    
if N == cp:
    cp = -1

print(cp)

Submission status: Accepted

Solution code:
n = int(input())
a=[int(x) for x in input().split()]
i=0
cnt=0
x=1
hozon=0
while i<n:
	if a[i]==x:
		x+=1
	else:
		cnt+=1
	i+=1
	
if x == 1:
	cnt = -1
print(int(cnt))

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=k+1
        
if k==0:
    answer=-1
else:
    answer=n-k

print("{}".format(answer))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''


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

    count = 0
    id = 1

    for i in range(N):
        if A[i] != id:
            count += 1
        else:
            id += 1
        # print(A[i], id, count)
    if id == 1:
        print(-1)
    else:
        print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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


counter = 0
target = 1
for i in range(len(nums)):
    if nums[i] != target:
        counter += 1
    else:
        target += 1
        continue
if target == 1:
    print(-1)
else:
    print(counter)

Submission status: Accepted

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

cnt = 0
for num in A:
    if num == cnt + 1:
        cnt+=1
if cnt == 0:
    print(-1)
else:
    print(N - cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np

n = int(input())
a = np.array(input().split(), dtype='int')

j = 1

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

ans = n - j + 1

if ans == n:
    ans = -1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
input = sys.stdin.readline
N = int(input())
A = [int(x) for x in input().strip().split()]
t = 0
f = 1
ans = deque([])

for n, a in enumerate(A, start=1):
    if a == f:
        ans.append(n - t - 1)
        f += 1
        t = n
else:
    if t != N:
        ans.append(n - t)

if f == 1:
    print(-1)
else:
    print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = []
count = 0
q = 1

for a in arr:
	if a == q:
		q += 1
		ans.append(a)
	else:
		count += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# import numpy as np
import sys
input = sys.stdin.readline

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

k = 1

seta = set(a)
if k not in seta:
    print(-1)
    sys.exit()

res = 0

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

print(res)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2000000)

n=int(input())
a=[]*n
a=list(map(int,input().split()))
num=1
bre=0
no=True
def search(pos):
    spos=pos
    global num,bre,no
    end=True
    while end:
        if spos >= n:
            break
        if a[spos]==num:
            no=False
            num+=1
            search(spos+1)
            end=False
        else:
            spos+=1
            bre+=1

search(0)
if no:
    bre=-1
print(bre)

Submission status: Accepted

Solution code:
import sys
n=int(input())
a=list(map(int,input().split()))
idx=0
if a==list(range(1,n+1)):
  print(0)
  sys.exit()
if sum(a)==n:
  print(n-1)
  sys.exit()
if 1 not in a:
  print('-1')
  sys.exit()
for i in range(1,n+1):
  for j in range(idx,n):
    if a[j]==i:
      idx=j
      break
    if j==n-1:
      print(n-i+1)
      sys.exit()

Submission status: Accepted

Solution code:
n = int(input())
p = input().split()

a = 0
c = 1

for i in range(n):
    m  = int(p[a])
    if m ==c:
        c+=1
    a += 1

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

Submission status: Accepted

Solution code:
import sys

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

ans = 0
_find = 1
before = 0

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

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

print(ans+n-before)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
A = list(map(int, input().split()))
if 1 not in A:
    print(-1)
    exit()

target = 1
idx = 0
ans = 0
while idx < len(A):
    if A[idx] == target:
        target += 1
        ans += 1
    idx += 1
print(N - ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from collections import deque
N=int(input())
A=map(int,input().split())
# Aから要素を削除していくと[1,2,3,...]になるかどうかを判定する．砕く個数が最小のときは最後に残る要素が最大になる
D=deque(A)
count = 1
for i in range(N):
    d = D.popleft()
    if d == count:
        count+=1
if count == 1:
    print(-1)
else:
    print(N-(count-1))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
cnt = 0
key = 1
ans = False
for i,value in enumerate(A):
    if value == key:
        key += 1
        ans = True

if ans:
    print(N - key + 1)
else:
    print(-1)

Submission status: Accepted

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

ans = 0
count = 1
y = 0

if A.count(1) == 0:
    ans = -1
else:
    for i in range(n):
        if A[i] != count:
            ans += 1
            pass
        else: 
            count += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

stt=0

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
pre_len = len(a) # 最初の長さ
ans = list()

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


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

Submission status: Accepted

Solution code:
import sys
from math import factorial
from fractions import Fraction
import heapq, bisect, fractions
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
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()))
MOD = 10**9 + 7

n = INT()
a = LIST()

i = 0
res = 0
count = 1
while i<n:
    if a[i] == count:
        count += 1
    i += 1
if count == 1:
    print(-1)
else:
    print(n-count+1)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
x = int(input())
result = 0
ary = list(map(int,input().split()))
miss = False
res = 1
for i in range(x):
  if (res == ary[i]):
    res += 1
print(-1 if res-1 == 0 else x - res + 1)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
bricks=list(map(int,input().split()))
st=1
b=0
i=0
f=-1
while(i<n):
    if bricks[i]==st:
        st+=1
        f=1
    else:
        b+=1
    i+=1
if f==-1:
    print(f)
else:
    print(b)

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys
import math
from datetime import datetime
import re
import math
def sol(N, S):

    radar = 1
    contigious = []
    for n in S:
        if n == radar:
            contigious.append(n)
            radar += 1
    if len(contigious) > 0:
        return len(S) - len(contigious)
    else:
        return '-1'







do_submit = True
#do_submit = False

def input_parse(input_str):
    lines = [x.strip() for x in input_str.split("\n") if x.strip()]
    parsed_lines = [list(map(str, line.split())) for line in lines]
    print(parsed_lines)
    # n = int(parsed_lines[0][0])
    # k = int(parsed_lines[0][1])
    S = parsed_lines[0][0]
    return S


if not do_submit:
    S = input_parse("""
    1905
    """)
    print(sol(S))

    S = input_parse("""
    0112
    """)
    print(sol(S))

    S = input_parse("""
    1700
    """)
    print(sol(S))

    S = input_parse("""
    0000
    """)
    print(sol(S))

    S = input_parse("""
    1001
    """)
    print(sol(S))



else:
    n = int(input())
    S = list(map(int, input().split()))
    #S = input()
    print(sol(n, S))

    # S = input().strip()
    # print(sol(S))

Submission status: Accepted

Solution code:
n = int(input())
first = n
k = [int(x) for x in input().split()]
j = 1
for i in (k):
    if(i==j): j+=1
if(j==1): print("-1")
else: print(str(n-j+1))

Submission status: Accepted

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

Submission status: Accepted

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

flag=1
for i in range(0,N):
    if int(a[0][i])==flag:
        flag+=1
       
if flag==1:
    print(-1)
else:
    print(N-flag+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=int(input())
b=input().split()
c="1"
d=0
for i in range(a):
    if b[i]==c:
        c=str(int(c)+1)
    else:
        d+=1
if d==a:
    print(-1)
else:
    print(d)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import fractions
y = int(input())
x = list(map(int, input().split()))
list=[0]

for i in range(len(x)):
  if x[i]== list[-1]+1:
    list.append(x[i])

if len(list)==1:
  print(-1)
else:
  print(len(x)-(len(list)-1))

Submission status: Accepted

Solution code:
test = ""
#test = \
"""
1
1
"""
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
    

n = int(input2())
nums = list(map(int, input2().strip().split()))
nums.reverse()

ans = 0
wanted = 1
while nums:
    tmp = nums.pop()
    if tmp == wanted:
        wanted += 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()))
b = []
c = 0

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

if c != 0:
  print(N-len(b))
elif c == 0:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

def search():
	# import pdb; pdb.set_trace()
	search_start = 0
	for target in range(1, N+1):
		for j in range(search_start, N):
			if int(a[j]) == target:
				search_start = j + 1
				break
		else:
			# ここが実行できていると言うことは、右端まで探索が終わったということ
			# 従って、targetを返す必要がある
			if N != 1 and target == 1:
				return -1
			else:
				return N - (target - 1)
	else:
		# ここが実行できているということは、N個の数字全部の探索が終わったということ
		# すなわち、ブロックを壊す必要がない
		return 0
	return False

print(search())

Submission status: Accepted

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

Submission status: Accepted

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

for a in A:
    if a != i:
        count += 1
    else:
        i += 1

if count == len(A):
    print(-1)
else:
    print(count)

Submission status: Accepted

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

A = [a for i, a in enumerate(A) if a <= i+1]

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

cnt=1
flag=0
result=0

for item in a:
    if item ==cnt:
        flag=1
        cnt+=1
    else:
        result+=1

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

print(result)

Submission status: Accepted

Solution code:
# そのまま入力文字列の入力
n = int(input())

# 空白区切りの数値をリスト化
an = list(map(int, input().split()))

ans = 0

target = 1
has_ans = False
for i in range(0, n):
    if an[i] == target:
        has_ans = True
        target += 1
    else:
        ans += 1

print(ans if has_ans else "-1")

Submission status: Accepted

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

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

# 不可能
if (ans == N):
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

check = 1
ans = 0

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

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