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

num = 1

res = 0

for a in A:
    if a == num:
        num += 1
    else:
        res += 1


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

Submission status: Accepted

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


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


c = 1
ans = 0
for a in A:
    if a == c:
        c += 1
        continue
    ans += 1
if ans == N:
    print(-1)
else:
    print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
a = list(map(int,input().split()))
d = deque()
for i in range(1,200001):
  d.append(i)
count=0
for i in a:
  if i==d[0]:
    count+=1
    d.popleft()
    
if count==0:
  print(-1)
else:
  print(N-count)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n=int(input())
inp=list(map(int,input().split()))
from collections import Counter
z=0
k=1
for i in range(len(inp)):
    if inp[i]==k:
        z+=1
        k+=1
if z==0:
    print (-1)
else:
    print (len(inp)-z)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
i=1
f=False
for ai in a:
    if ai==i:
        i+=1
        f=True

if f:
    print(N-i+1)
else:
    print(-1)

Submission status: Accepted

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

aset = set(a)
if 1 not in aset:
    print(-1)
else:
    nex = 1
    counter = 0
    for i in range(N):
        if a[i] == nex:
            nex += 1
        else:
            counter += 1
    print(counter)

Submission status: Accepted

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

Submission status: Accepted

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

now = 1
cnt = 0
for a in al:
    if a == now:
        cnt += 1
        now += 1
if cnt == 0:
    print(-1)
else:
    print(n-cnt)

Submission status: Accepted

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

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

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

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)
    exit()
print(N - num + 1)

Submission status: Accepted

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

check=1
ans=0

ans_flag=False

for a in A:
    if a==check:
        ans_flag=True
        check += 1
        
    else:
        ans += 1
        
if ans_flag == False:
    ans=-1
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

j = 1
ans = 0

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

if j == 1:
    print('-1')
else:
    print(ans)

Submission status: Accepted

Solution code:
import math
import sys
import bisect
readline = sys.stdin.readline


def main():
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    k = 0

    if A.count(1) == 0:
        print(-1)
        return

    for i in range(n):
        if A[i] == k + 1:
            k += 1

    print(n - k)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    

if '__main__' == __name__:
    resolve()

Submission status: Accepted

Solution code:
x=int(input())
s=list(map(int,input().split()))
p=0
c=1
for n in s:
    if n==c:
        c+=1
        p+=1

if p==0:
    print(-1)
    exit(0)
print(x-p)

Submission status: Accepted

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

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

if not flag:
    print(-1)
    sys.exit()

print(n - len(b))

Submission status: Accepted

Solution code:
N = int(input())
A = input().split(' ')
A = [int(A[i]) for i in range(N)]
NN = 1
count = 0
check = []
for i in range(N):
  if not A[i] == NN:
    count += 1
  else:
    check.append(A[i])
    NN += 1
if not check:
  print(-1)
else:
  print(count)

Submission status: Accepted

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

Submission status: Accepted

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

num = 0
for item in a:
    if item == (num + 1):
        num += 1

if num != 0:
    print(N - num)
else:
    print(-1)

Submission status: Accepted

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

def main(n,a):
    tgt=1
    count=0
    for i in range(n):
        if a[i]==tgt:
            tgt+=1
        else:
            count+=1
    if count==n:
        return -1
    else: 
        return count

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)

Submission status: Accepted

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

i = 1
ans = 0
red = []
for a in A:
    if a == i:
        red += [i]
        i += 1
    else:
        ans += 1
if not ans == len(A):
    print(ans)
else:
    print(-1)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

step = 1

for i in range(len(ai)):
    if ai[i] != step:
        ai[i] = 0
    else:
        step += 1

if sum(ai) == 0:
    print(-1)
else:
    print(ai.count(0))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
_a = LI()
#AB = [LI() for _ in range(N)]
#A,B = zip(*AB)
a = np.array(_a)
#C = np.zeros(N + 1)

if not np.amin(a)==1:
    print(-1)
    exit()

maxNumber = 1

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

print(N - (maxNumber-1))

# current = 0
# for i in range(1,200001):
#     position=[]
#     position = np.where(a[current:] == i)
#     if position ==[] or position[0] == []:
#         break
#     else:
#         current = position[0][0]
# print(current)
    


# if ans:
#     print('Yes')
# else:
#     print('No')

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
k=1
ans=0
for i in range(N):
    if k==a[i]:
        k+=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()))[::-1]
number=2
if 1 not in a: print(-1);exit()
count=n-len(a[:a.index(1)+1])
a=a[:a.index(1)][::-1]
for i in a:
  if i==number:
    number+=1
  else:
    count+=1
print(count)

Submission status: Accepted

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

manzoku = 1
hakai = 0

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

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

Submission status: Accepted

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

for p in range(N):
    if A[p] == n+1:
        n += 1

if n != 0:
    print(N-n)
else:
    print(-1)

Submission status: Accepted

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

ni = 1
remains = 0

for r in renga:
    if ni == r:
        ni += 1
        remains += 1
if ni == 1:
    print(-1)
else:
    print(len(renga) - remains)

Submission status: Accepted

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

cnt = 1
broken = 0
for a in A:
  if a == cnt:
    cnt += 1
  else:
    broken += 1

if cnt == 1:
  print("-1")
else:
  print(broken)

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
ch=1

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

Submission status: Accepted

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

Submission status: Accepted

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

bcnt = 0
signal = 1

for i in a:
    if i == signal:
        signal += 1
    else:
        bcnt += 1

if bcnt == N:
    print('-1')
else:
    print(bcnt)

Submission status: Accepted

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

Submission status: Accepted

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

if is_not_minus:
    print(n - order_counter + 1)
else:
    print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

for a in A:
  if a == k:
    k += 1
  else:
    ans += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
Ali = list(map(int,input().split()))
s = 1
res = 0

for i in range(N):
  if Ali[i] == s:
    s += 1
  else:
    res += 1
    
if s == 1:
  print(-1)
else:
  print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

from sys import exit

def main():
  N = int(input())
  a = list(map(int,input().split()))
  index = -1
  num = 1
  
  for i in range(index+1, N):
    if a[i] != num:
      continue
    elif a[i] == num:
      index = i
      num += 1
      
  if index != -1:
    print(N-(num-1))
    exit()
      
  #indexに更新がなければ-1のまま
  print(index)
  
  
  
  
main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
from collections import OrderedDict
import itertools
from sys import stdin
input = stdin.readline


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

#  pre = A[0]
#  right = -1
#  left = 0
#  max_ = 0
#  for i in range(1, N):
#    if pre+1 == A[i]:
#      right = i
#    else:
#      max_ = max(max_, right-left)
#      left = i
#  max_ = max(max_, N-left)

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

  print(N-max_ if max_ else -1)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


n = int(readline())

p = list(map(int, readline().split()))

num = 1
l = []
for i in range(n):
    if p[i] == num:
        l.append(i)
        num += 1

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

Submission status: Accepted

Solution code:
N = int(input())
aaa = list(map(int, input().split()))
i = 1
ans = 0
for a in aaa:
    if a == i:
        i += 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()))
L=1
ans=0
for i in range(N):
  if A[i]!=L:
    ans+=1
  else:
    L+=1

if N==1 and A[0]==1:
  print(0)
elif 1 not in A:
  print(-1)
else:
  print(ans)

Submission status: Accepted

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

destroy=0
count = 1
for i in range(N):
  if A[i] != count:destroy += 1
  else:count+=1

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

Submission status: Accepted

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

print(cnt) if a.count(1) else print(-1)

Submission status: Accepted

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

ans = 1
a = 0

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(N - cnt if target != 1 else -1)

Submission status: Accepted

Solution code:
import os, sys, re, math
from functools import reduce

N = int(input())
A = [int(n) for n in input().split()]

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

if v > 1:
    print(count)
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
aa = [int(i) for i in input().split()]
idx = 1
for a in aa:
	if a == idx:
		idx += 1
if idx == 1:
	ret = -1
else:
	ret = n - idx + 1
print(ret)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(s) for s in input().split(' ')]
head = 1
count = 0
for a in A:
    if a == head:
        head += 1
        continue
    else:
        count += 1
if head == 1:
    print(-1)
else:
    print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
aas = list(map(int, input().split()))
if n == 1 and aas == [1]:
    print(0)
    exit()
t = 0
for i in aas:
    if i == t + 1:
        t += 1
print(n-t if t > 0 else -1)

Submission status: Accepted

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

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

if n >= N:
    print(-1)
else:
    print(n)

Submission status: Accepted

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

ans = 0
num = 1
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:
import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
A=[int(x) for x in input().rstrip('\n').split()]
now = 0
for x in range(N):
  if A[x] == now+1:
    now += 1
if now == 0:
  print(-1)
else:
  print(N-now)

Submission status: Accepted

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

Submission status: Accepted

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

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

target = 1
cnt = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    a_s = set(a)
    cnt = 0
    order = 1
    if not 1 in a_s:
        print(-1)
        exit()
    else:
        for i in range(len(a)):
            if a[i] != order:
                cnt += 1
            if a[i] == order:
                order += 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

t = 1
for x in a:
    if x == t:
        t += 1
t -= 1

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]

from collections import deque
N = ri()
a = deque(ri_())
i = 0
cnt = 0
if not 1 in a:
    print(-1)
    exit()
while i < N:
    if a[i] == i + 1:
        i += 1
    else:
        a.popleft()
        N -= 1
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))
ans = 0
j = 1
for i in range(N):
    if A[i] == j:
        j+=1
    else:
    	ans += 1
if j == 1:
    print(-1)
else:
    print(ans)

Submission status: Accepted

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

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())
la = [int(w) for w in input().split()]

ans = 0
i = 1
for a in la:
    if a != i:
        ans += 1
    else:
        i += 1
if ans == n and i == 1:
    ans = -1

print(ans)

Submission status: Accepted

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

cnt = 1
ans = 0
for a in alist:
    if a != cnt:
        ans += 1
    else:
        cnt += 1

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

Submission status: Accepted

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

n, s, ind, res = int(input()), list(map(int, input().split())), 1, 0
for i in s:
    if i == ind: ind += 1
    else: res += 1
print(-1 if res == n else res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
c = 1
for i in a:
    if i == c:
        c += 1
print(-1*(c==1) or n - c + 1)

Submission status: Accepted

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

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

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

Submission status: Accepted

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

Submission status: Accepted

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

x = 1
ans = 0

if not 1 in a:
    ans = -1
else:
    for i, item in enumerate(a):
        if item == x:
            x += 1
        else:
            ans += 1

print(ans)

Submission status: Accepted

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

cnt = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
c = 0
ans = -1
for num in a:
    if num==c+1:
        c = num
    else:
        pass
#     print(ans)
if c>0:
    ans = max(ans, c)
if ans>=0:
    print(n-ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
def gcd(*numbers): reduce(math.gcd, numbers)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
count = 0
ans = 0

N = k()
a = l()

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

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

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

Submission status: Accepted

Solution code:
from sys import exit

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

break_count = 0

for key, a in enumerate(A, 1):
    if not a == key - break_count:
        break_count += 1

if break_count == N:
    print(-1)

else:
    print(break_count)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
a = LI()

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

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