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:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


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

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

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


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=1
for i in a:
    if cnt==i:
        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()))

k = 0

for v in a:
    if v == k+1:
        k += 1

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

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 0
    cur = 1
    for i in range(N):
        a = A[i]
        if a != cur:
            ans += 1
        else:
            cur += 1
    print(ans if ans != N else -1)

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

cnt=0
m=1

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

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

# def factorization(n):
#     arr = []
#     temp = n
#     for i in range(2, int(-(-n**0.5//1))+1):
#         if temp%i==0:
#             cnt=0
#             while temp%i==0:
#                 cnt+=1
#                 temp //= i
#             arr.append([i, cnt])
#     if temp!=1:
#         arr.append([temp, 1])
#     if arr==[]:
#         arr.append([n, 1])
#     return arr

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

now = 1

c = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

    num = 1
    for a in a_list:
        if a == num:
            num += 1
    
    if num == 1:
        print(-1)
    else:
        print(n - num + 1)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
Query = deque(map(int,input().split()))
ju = 1
Answer = 0
for i in range(N):
    q = Query.popleft()
    if q == ju:
        ju +=1
    else:
        Answer += 1
if ju == 1:
    Answer = -1
print(Answer)

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt=0
hit=0
while cnt<N:
  if ans[hit]+1==A[cnt]:
    hit+=1
    ans.append(hit)
  cnt+=1
print(N-len(ans[1:]) if len(ans)>1 else -1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D
# CreatedDate:  2020-08-30 14:25:42 +0900
# LastModified: 2020-08-30 14:34:10 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd


def main():
    N = int(input())
    A = list(map(int, input().split()))
    if not 1 in A:
        print(-1)
        return
    index = 0
    for a in A:
        if index+1 == a:
            index += 1
    print(N-index)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
x = 1
list1 = []

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

Submission status: Accepted

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

next_number = 1
ans = 0
for i in range(n):
    if a_ls[i] == next_number:
        next_number += 1
    else:
        ans += 1
if ans == n:
    ans = -1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	A = [int(a) for a in input().split(" ")]
	target = 1
	broke = 0
	for i in range(len(A)):
		if A[i] == target:
			target += 1
		else:
			broke += 1
	if broke < N:
		print(broke)
	else:
		print(-1)

main()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

i = i-1

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools
import itertools
import math
import sys

INF = float('inf')


def solve(N: int, a: "List[int]"):
    c = 1
    for i in a:
        c += i == c
    return len(a) - (c-1) if c != 1 else -1


def main():

    sys.setrecursionlimit(10 ** 6)

    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]"
    print(f'{solve(N, a)}')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    number = 1
    count = 0

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

    if 1 not in a_lst:
        count = -1

    print(count)


if __name__ == '__main__':
    main()

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()))
from collections import deque
a = deque(a)
ans = 0
now = 1
while a:
  x = a.popleft()
  if x == now:
    now += 1
  else:
    ans += 1
if ans == N:
  ans = -1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())

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

i=0
ans=0
saigo=0
k=1
kosuu=n

while i<=n-1:
    if a[i]!=k:
        ans+=1
        kosuu-=1
    else:
        saigo=a[i]
        k+=1
    i+=1

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

if kosuu==saigo:
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
from collections import deque
q = deque(A)
i = 1
ans = []
while i <= n and q:
    a = q.popleft()
    if a==i:
        ans.append(a)
        i += 1
if len(ans)==0:
    print(-1)
else:
    print(n-len(ans))

Submission status: Accepted

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

card=1
cnt=0

for i in a:
  if i==card:
    card+=1
  else:
    cnt+=1

if cnt==n:
  print('-1')

else:
  print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

i = 1
ans = 0
for aa in a:

    if aa != i:
        ans += 1
    else:
        i += 1

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

Submission status: Accepted

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

card = 1
cnt = 0

for i in a:
  if i == card:
    card += 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()))
j = 1
for i in range(N):
  if a[i] == j:
    j += 1
    ans = N-j+1
if j == 1:
  ans = -1
print(ans)

Submission status: Accepted

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

breaking=0
cnt=1
for i in a:
    if i==cnt:
        cnt+=1
    else:
        breaking+=1
        
if breaking==len(a):
    print(-1)
else:
    print(breaking)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math

n = int(input())
aL = list(map(int, input().split(" ")))

crnt = 1
for a in aL:
    if a == crnt:
        crnt += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))


def solve():
    n = ini()
    a = inl()
    cur = 1
    for x in a:
        if x == cur:
            cur += 1
    if cur == 1:
        return -1
    return n + 1 - cur


print(solve())

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

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

Submission status: Accepted

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

index = 1
cnt = 0
for i in range(N):
    if index == A[i]:
        index += 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()))
if not 1 in set(a):
    print("-1")
    exit()
b=1
for i in range(n):
    if a[i]==b:
        b+=1
print(n-b+1)

Submission status: Accepted

Solution code:
n = int(input())
j = 1

for i in map(int, input().split()):
    if i == j:
        j += 1

print((-1, n - j + 1)[j != 1])

Submission status: Accepted

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

for i in a:
  if i == pos:
    pos += 1
  else:
    num_break += 1
print(num_break)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
A = inpl()

ans = 0
if A.count(1) == 0:
    ans = -1

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

print(ans)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
#D - Brick Break
N = int(input())
a = list(map(int,input().split()))

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# row = [int(x) for x in input().rstrip().split(" ")]
# n = int(input().rstrip())
# s = input().rstrip()

def resolve():
    import sys
    input = sys.stdin.readline

    n = int(input().rstrip())

    count = 0
    ans = 0
    for a in input().rstrip().split(" "):
        a = int(a)
        if a == count + 1:
            count+= 1
        else:
            ans += 1

    if count == 0:
        print("-1")
    else:
        print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

for a in Al:
  if a == cnt:
    cnt += 1
print(-1 if cnt == 1 else n - (cnt-1))

Submission status: Accepted

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

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

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

Submission status: Accepted

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

tmp = 1
s = set()
for x in a:
  if x == tmp:
    s.add(x)
    tmp += 1

print(n - len(s) if s else -1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n = int(input())

a = list()
a = input().split()

curr = 1
count = 0

for i in a:
    if int(i) == curr: 
        curr+=1
    elif int(i)!=curr:
        count+=1
if curr == 1:
    print("-1")
else:
    print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = [1e6] + list(map(int, input().split()))
cnt = 1
exist = False
for i in a:
    if i == cnt:
        exist = True
        cnt += 1
cnt -= 1

if exist:
    print(n - cnt)
else:
    print(-1)

Submission status: Accepted

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

ans = 0
next = 1

for a in a:
    if a == next:
        next += 1
    else:
        ans += 1

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

Submission status: Accepted

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

cnt = 1
for x in arr:
    if cnt == x:
        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()))
cnt = 1
for a in A:
    if cnt==a:
        cnt += 1
print(N-cnt+1 if cnt!=1 else -1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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


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

count = 0
pos = 1
for i, a in enumerate(A):
    if a == pos:
        pos += 1
    else:
        count += 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
A = inintl()

cnt = 1
ans = 0

for a in A:
    if a != cnt:
        ans += 1
        continue
    else:
        cnt += 1

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

Submission status: Accepted

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

k=0
bc=0
c=0
i=0
while k < n :
    if P[k]!=i+1 :
        bc+=1
    else:
        i+=1
        c+=1
    k+=1

if c==0 :
    bc = -1

print(bc)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
res=False
res2=1
for i in range(n):
    if a[i]==res2:
        res=True
        res2+=1

ans=n-res2+1 if res else -1
print(ans)

Submission status: Accepted

Solution code:
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = tuple(map(int, input().split()))
x = [[] for _ in range(N + 1)]
for i, a in enumerate(A):
    x[a].append(i)

maxcount = 0
todo = [(1, i) for i in x[1]]
while todo:
    a, i = todo.pop()
    maxcount = max(maxcount, a)
    if a == N:
        continue
    na = a + 1
    for ni in x[na]:
        if ni < i:
            continue
        todo.append((na, ni))
if maxcount > 0:
    print(N - maxcount)
else:
    print(-1)

Submission status: Accepted

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

while x <= N - 1:
    if ans + 1 == a[x]:
        ans += 1
    x += 1
    
if ans == 0:
    print(-1)
else:
    print(N - ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

t = 1

s = 0

if t not in a:
    s = -1
else:
    for x in a:
        if x == t:
            t += 1
        else:
            s += 1

print(s)