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=1
for i in a:
    if i==count:
        count+=1
if count==1:
    print(-1)
else:
    print(N-count+1)

Submission status: Accepted

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

ans=0
i=0
j=1
while i<N:
    if A[i]==j:
        j+=1
    else:
        ans+=1
    i+=1

print([-1,ans][1 in A])

Submission status: Accepted

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

i = 1
for a in A:
    if a == i:
        i += 1
if i == 1:
    print('-1')
else:
    count = i-1
    print(N-count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
alst = list(map(int, input().split()))
if 1 not in alst:
  print(-1)
  exit()

ans = 0
for num in alst:
    if num == ans + 1:
        ans += 1
else:
    print(n - ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
count=0
num=1
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:
n=int(input())
ar=list(map(int,input().split()))
m=0
for i in ar:
    if i-1==m:
        m=i
if m: print(n-m)
else: print(-1)

Submission status: Accepted

Solution code:
N = int(input())
a_s = list(map(int, input().split()))
cnt = 1
answer = 0
for i, a in enumerate(a_s):
    if cnt == a:
        cnt += 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()))
count = 1
ans = 0
for i in range(n):
    if a[i] == count:
        count += 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=0
for a in A:
    if a==I+1:
        I+=1

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

Submission status: Accepted

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

counter = 1
ans = 0

for j in l:
    if j == counter:
        counter += 1
    else:
        ans += 1

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

Submission status: Accepted

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

length = len(A)
find_next = 1

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

if find_next == 1:
    print(-1)
else:
    print(length - find_next + 1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
class Stack:
  def __init__(self):
    self.count = 1
    self.lst = []
  
  def push(self, element):
    if element == self.count:
      self.lst.append(element)
      self.count += 1
  
  def length(self):
    return len(self.lst)

n = int(input())
a = list(map(int, input().split()))
stack = Stack()
for i in a:
  stack.push(i)

b = stack.length()
if b == 0:
  print('-1')
else:
  print(n - b)

Submission status: Accepted

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

count=0
num=1
for i in range(n):
  if A[i] == num:
    count += 1
    num += 1
if count == 0:
  print('-1')
  exit()
print(n-count)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

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

    if n == 1:
        print(-1)
    else:
        print(N - n + 1)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans if ans < len(a) else -1)

Submission status: Accepted

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

i=1
for a in alst:
    if a==i:
        i+=1
i-=1

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

Submission status: Accepted

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

for i in range(len(A)):
    if A[i] == now:
        now += 1
    else:
        count += 1

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
m = 1
for i in a:
    if i == m:
        m += 1
if m == 1:
    print(-1)
    exit(0)
print(1+n-m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# cook your dish here
n=int(input())
v=list(map(int,input().split()))
if 1 not in v:
    print(-1)
else:
    ans=0
    num=1
    for i,e in enumerate(v):
        if e==num:
            num+=1
            ans+=1
            
            
    print(n-ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
ls = []
for i, x in enumerate(A):
    ls.append([i+1, x])
ls.sort(key=lambda x: x[1])
cur = 1
num = 0
ans = 0
s = set()
for x, y in ls:
    if cur == y and y not in s and num < x:
        cur += 1
        num = x
        s.add(y)
    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()))

check = 1
ans = 0


for i in a:
  ans+=1
  if i == check:
    ans -=1
    check+=1
    
if a==[j for j in range(1,N+1)]:
  print(0)
  exit()
  
elif ans==N:
  print(-1)
  exit()

print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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

if 1 in t:
    ans = 0
else:
    print(-1)
    exit()

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
if 1 not in A:
  print(-1)
  exit()
cnt = 0
s = 1
for a in A:
  if a == s:
    cnt += 1
    s+=1
print(n-cnt)

Submission status: Accepted

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

pos_num = 1
count = 0
for num in bricks:
    if num == pos_num:
        pos_num += 1
    else:
        count += 1

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

Submission status: Accepted

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

num = 1
a = 0

for i in list:
  if i == num:
    num = num +1
  else:
    a = a + 1

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ans = 0
c = 1
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())
a = list(map(int, input().split()))

ans = 0
t = 1
s = []
for i in a:
  if i != t: ans += 1
  else:
    t += 1
    s.append(i)

if s == []:
  print(-1)
else:
  print(ans)

Submission status: Accepted

Solution code:
def resolve():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = [int(i) for i in input().split()]
    if 1 not in A:
        print(-1)
        return
    sumA = 0
    try:
        i = 1
        while sumA + i <= N:
            sumA += A.index(i, (sumA + i - 1)) - (sumA + i - 1)
            i += 1
        print(sumA)
    except BaseException:
        sumA += N - (sumA + i - 1)
        print(sumA)


resolve()

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, As):
    ans = 0
    i = 1
    for A in As:
        if A == i:
            i += 1
        else:
            ans += 1
    ans = -1 if ans == N else ans
    print(ans)


if __name__ == '__main__':
    # S = input()
    N = int(input())
    # N, M = map(int, input().split())
    As = [int(i) for i in input().split()]
    # Bs = [int(i) for i in input().split()]
    solve(N, As)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
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:
import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int,input().split()))
num = 1
ans = 0
for i in range(n):
    if a[i] != num:
        ans += 1
    else:
        num += 1

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

Submission status: Accepted

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

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

Submission status: Accepted

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

count = 0
last_i = 0
temp = 1
t = 0

if 1 not in set(A_n):
    print('-1')
else:
    for a in A_n:
        if a == temp:
            temp += 1
        else:
            count += 1
    print(count)

Submission status: Accepted

Solution code:
N = int(input())

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


tmp = 1
counter = 0
for a in A:
    if a==tmp:
        counter += 1
        tmp += 1

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

Submission status: Accepted

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

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

ans = n-count+1

if 1 not in a:
    ans = -1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

idx = 1
ans = 0
length = len(A)
lis = []
for a in A:
  if a == idx:
    lis.append(idx)
    idx += 1
    continue
  else:
    ans += 1
    length -= 1

if lis != [] and lis == list(range(1, length+1)):
  print(ans)
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

    if a.count(1) == 0:
        print(-1)
        return
    index = 1
    cnt = 0
    for i in range(N):
        if index == a[i]:
            index+=1
        else:
            cnt+=1

    print(cnt)






if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline

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

    print(N - target + 1)


main()

Submission status: Accepted

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

if 1 not in lis:
    cnt = -1
else:
    for idx, val in enumerate(lis):
        if val != idx+1-cnt:
            cnt += 1

print(cnt)

Submission status: Accepted

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

target, point = 0, 0
ans = 0
for i in range(n):
    if a[i] == target:
        ans += i - point
        point = i + 1
        target += 1

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

Submission status: Accepted

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

ans = 0
cnt = 1
while A:
    a = A.popleft()
    if cnt == a:
        cnt += 1
        continue
    else:
        ans += 1
if ans == N:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[42]:


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


# In[43]:


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


# In[ ]:

Submission status: Accepted

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

isOKs = [False] * (N+1)
isOKs[0] = True
for A in As:
    if isOKs[A-1]:
        isOKs[A] = True

for A in reversed(range(1, N+1)):
    if isOKs[A]:
        print(N-A)
        break
else:
    print(-1)

Submission status: Accepted

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

count=0
k=1
for i in range(len(A)):
    if A[i]==k:
        count+=1
        k+=1

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

Submission status: Accepted

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

f=0
for a in A:
    if a==f+1:
        f+=1

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

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:
n = int(input())
a = list(map(int, input().split()))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    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)
        return
    print(ans)

    return
resolve()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    # 左から貪欲に1,2,3..ととっていけば良い
    next = 1
    for a in A:
        if a==next:
            next += 1
    if next==1:
        print(-1)
    else:
        print(N-(next-1))

main()

Submission status: Accepted

Solution code:
import sys
import bisect

N = int(input())
a = list(map(int,input().split()))
numdic = {}
for i, num in enumerate(a):
    if num not in numdic:
        numdic[num] = [i+1]
    else:
        numdic[num].append(i+1)

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

nowplace = 0
count = 0
for i in range(1,N+1):
    if i not in numdic:
        break
    else:
        place = bisect.bisect_right(numdic[i], nowplace)
        if place >= len(numdic[i]):
            break
        else:
            nowplace = numdic[i][place]
            count += 1

print(N-count)

Submission status: Accepted

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

k = 1
cnt = 0
for a in A:
    if a == k:
        cnt += 1
        k += 1

ans = -1 if k == 1 else N - cnt
print(ans)

Submission status: Accepted

Solution code:
# 148c

from sys import stdin
import fractions

#stdin = open("test/abc148_d_sample-3.in")

N = int(stdin.readline().strip())
a = list(map(int, stdin.readline().strip().split()))

rem = 0
ne = 1
ok = False

for ai in a:
  #print(ne, ai, rem)
  if ai==ne:
    ne += 1
    ok = True
  else:
    rem += 1

if ok:
  print(rem)
else:
  print("-1")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
D - Brick Break
https://atcoder.jp/contests/abc148/tasks/abc148_d

"""
import sys


def solve(N, arr):
    x = 1
    last = -1
    ans = 0
    for a in arr:
        if a != x:
            ans += 1
        else:
            x += 1
            last = 1
    return ans if last != -1 else -1


def main(args):
    N = int(input())
    arr = [int(a) for a in input().split()]
    ans = solve(N, arr)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import sys

N = int(input())
a = list(map(int, input().split()))
ans = 0 
count = 1
if 1 not in a :
    print(-1)
    sys.exit()

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if all([i == -1 for i in a]):
  print(-1)
else:
  print(ans)

Submission status: Accepted

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

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

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


if __name__ == '__main__':
    q_d()

Submission status: Accepted

Solution code:
num = int(input())
input_line = list(map(int,input().split(' ')))

bb = 0
bi = 0

for i in input_line:
    if(i==(bi+1)):
        bi += 1
    else:
        bb += 1
if(bi>0):
    print(bb)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
L = np.array(input().split(),dtype = int)

m = min(N,max(L))

r = np.array(np.linspace(1,m,m),int)


dp = [0 for i in range(N+1)]

for i in range(0,N):
    if dp[i]+1 ==L[i]:
        dp[i+1] = dp[i]+1
    else:
        dp[i+1] = dp[i]

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

Submission status: Accepted

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

count = 0
target = 1

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

target = 1

broken = 0

for i in a:
    if i == target:
        target += 1
    else:
        broken += 1

if broken < n:
    print(broken)
else:
    print(-1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = [int(i) for i in input().split()]
if not 1 in a:
    print(-1)
    sys.exit()
target = 1
count = 0
for i in a:
    if i == target:
        target += 1
    else:
        count += 1
print(count)

Submission status: Accepted

Solution code:
import math
from collections import deque
from collections import defaultdict
import itertools as it

#自作関数群
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def combmod(n,r,mod):
	return (fact(n)/fact(n-r)*pow(fact(r),mod-2,mod))%mod
def printQueue(q):
	r=copyQueue(q)
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): # much time
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} # 1~n
def bitArr(n):#ビット全探索
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1
def maxValue(a,b,v):
	v2 = v
	for i in range(v2,-1,-1):
		for j in range(v2//a+1): #j:aの個数
			k = i-a*j
			if k%b==0:
				return i
	return -1
def copyQueue(q):
	nq = queue.Queue()
	n = q.qsize()
	for i in range(n):
		x = q.get()
		q.put(x)
		nq.put(x)
	return nq
def get_sieve_of_eratosthenes(n):
	#data = [2]
	data = [0,0,0]
	for i in range(3,n+1,2):
		data.append(i)
		data.append(0)
	for i in range(len(data)):
		interval = data[i]
		if interval!=0:
			for j in range(i+interval,n-1,interval):
				data[j] = 0
	#ans = [x for x in data if x!=0]
	ans = data[:]
	return ans

n = readInt()
a = readInts()

ans = 10*10
ans = 0
for i in a:
	if ans+1==i:
		ans+=1

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    mark = 1
    remain = 0
    for i in range(n):
        if a[i] == mark:
            remain += 1
            mark += 1
    if remain == 0:
        print(-1)
    else:
        print(n-remain)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

next_target = 1

idx = []

for i, a in enumerate(A):
    if a == next_target:
        idx.append(i)
        next_target += 1

if not idx:
    print(-1)
else:
    prev = -1
    sum_ = 0

    if idx[-1] != N - 1:
        sum_ += N - (idx[-1] + 1)

    for x in idx:
        sum_ += x - prev - 1
        prev = x

    print(sum_)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools
import queue
from collections import deque
from fractions import Fraction



def main():
    num = int(input())
    data = list(map(int, input().split()))

    now_ind = 1
    break_num = 0

    for i in range(num):
        if data[i] == now_ind:
            now_ind += 1
        else:
            break_num += 1

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



if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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