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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

number = 1
count = 0
for num in a:
    if num==number:
        number += 1
    else:
        count += 1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def brick_break(n, a_list):
    c = 1
    for a in a_list:
        if a==c:
            c += 1
    if c == 1:
        return -1
    else:
        return n-c+1

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

print(brick_break(n, a_list))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

print(N - count + 1)

Submission status: Accepted

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

Submission status: Accepted

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

if x==1:
  ans=-1
else:
  ans=n-x+1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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



if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

if c==N:
    print("-1")
else:
    print(c)

Submission status: Accepted

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

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

if c==N:
    print("-1")
else:
    print(c)

Submission status: Accepted

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

can = False
cnt = 1
for a in A:
    if a == cnt:
        can = True
        cnt += 1

if can == False:
    print(-1)
else:
    print(N-cnt+1)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
target = 1

for x in range(n):
    if arrA[x] == target:
        target += 1
        continue
    else:
        ans +=1

if ans == n:
    ans = -1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

nums = list(map(int, input().split()))
l = [int(x) for x in range(1, n + 1)]

remain = [0]
count = 0

for i in nums:
    if i == remain[-1] + 1:
        remain.append(i)
    else:
        count += 1

if nums == l:
    print(0)
elif (count > 0) and (count != n):
    print(count)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a_list = [int(x) for x in input().split()]
del_list = []
cr = 1
num = 0
for i in range(N):
  if a_list[i] > i+1:
    a_list[i] = -1
for i in range(N):
  if a_list[i] == -1:
    num += 1
    continue
  if a_list[i] != cr:
    a_list[i] = -1
    continue
  cr+=1

l = [x for x in a_list if x == -1]
if len(l) == N:
  print("-1")
else:
  print(len(l))

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

b = 0

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

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:
        num += 1
        count += 1


if num == 1:
    count = -1
else:
    count = len(a) - count

print(count)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

from collections import deque

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

ind = 1
cnt = 0
while A:
  x = A.popleft()
  if x != ind:
    cnt += 1
  else:
    ind += 1
print(cnt if cnt != N else -1)

Submission status: Accepted

Solution code:
#148d
n=int(input())
a=[int(x) for x in input().split()]
if not (1 in a):
    print(-1)
    exit()
start=0
sum=0
for i in range(1,n+1):
    count=0
    for j in range(start,n):
        if i == a[j]:
            count+=1
            start=j
            sum+=count
            break
    if count==0:
        break

print(n-sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    n=int(input())
    a=list(map(int,input().split()))
    i=0
    cnt=1
    while i<=len(a)-1:
        if a[i]==cnt:
            cnt+=1
        i+=1
    if cnt==1:
        print(-1)
    else:
        print(len(a)-cnt+1)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

A = int(input())
B = [int(x) for x in input().split()]
count = 1
tanking = 0
ans = 0
flag = False
for i in range(A):
    if B[i] == count:
        count += 1
        ans += tanking
        tanking = 0
        flag = True
    else: 
        tanking += 1
        if i == A-1:
            ans+= tanking

if not flag:print("-1")
else : print(ans)

Submission status: Accepted

Solution code:
import sys
N = int(input())
A = list(map(int, input().split()))
ans_lis = [1]

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

ans = 1
for i in range(N):
    if A[i] != 1:
        ans = ans + 1
    else:
        A = A[ans:len(A)]
        break
         
number = 1

for i in A:
    if number == (i-1) :
        ans_lis.append(i)
        number = i

print(N-(len(ans_lis)))

Submission status: Accepted

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

index = 1
count = 0

for i in L:
  if i == index:
    index += 1
  else:
    count += 1
if count == N:
  count = -1
print(count)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def main():
    n = int(input())
    a = [int(_) for _ in input().split()]
    seek = 1

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

    if seek == 1:
        ans = -1
    else:
        ans = n + 1 - seek

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

cnt=0
c=1

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
k = 1
cnt = 0
for j in a:
    if j == k: 
        cnt += 1
        k += 1
if cnt == 0:
    print(-1)
else:
    print(N-cnt)

Submission status: Accepted

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

index = 1
count = 0
for a in A:
  if a == index:
    index += 1
  else:
    count += 1
if count == n:
  count = -1
    
print(count)

Submission status: Accepted

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

itr = 0
num = 0

while(itr < n):
    if(a[itr] == num+1):
        num +=1
    itr += 1

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

Submission status: Accepted

Solution code:
N = input()
a = [int(x) for x in input().split()]
ans, cur = 0, 1
for ai in a:
    if ai == cur:
        cur += 1
    else:
        ans += 1
print(ans if len(a) != ans else -1)

Submission status: Accepted

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

base=1
count=0

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

    #print(i)
    print((N-count)%N)
else:
    print(-1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N = II()
    A = LMIIS()

    num_broken = 0
    pre_required = 0
    for i, a in enumerate(A):
        required = i-(a-1)
        if required != num_broken:
            num_broken += 1
    print(num_broken if num_broken < N else -1)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())

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

Submission status: Accepted

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

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 num == 1:
    print("-1")
else:
    print(cnt)

Submission status: Accepted

Solution code:

if __name__ == '__main__':
    N=int(input())
    A=list(map(int,input().split()))
    temp=[]
    i=1
    for id,a in enumerate(A):
        if a==i:
            temp.append(a)
            i+=1
    ans=N-len(temp)
    if temp.__len__()==0:
        print(-1)
    else:
        print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
num = 1
for a in a_array:
    if a == num:
        num += 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()))
index=1
count=0
for i in a:
  if i==index:
    index+=1
  else:
    count+=1
if count==len(a):
  print(-1)
else:
  print(count)

Submission status: Accepted

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

next_ = 1
cnt = 0
while len(A) > 0:
  i = A.popleft()
  if i == next_:
    next_ += 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()))

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

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = 0
flag_empty = False
flag_end = False
if not 1 in A:
  cnt = -1
else:
  for i in range(1, n+1):
    if not i in A:
      cnt += len(A)
      break
    while 1:
      a = A.popleft()
      if len(A) == 0: 
        flag_empty = True
        break
      if a != i:
        cnt += 1
      else:
        break
    if flag_empty:
      break
   
print(cnt)

Submission status: Accepted

Solution code:
# coding: UTF-8

N = int(input())
A = list(map(int, input().split()))
a = 1
block = []
for i in range(N):
    if A[i] == a:
        block.append(A[i])
        a += 1
    else:
        continue
if len(block) > 0:
    print(N - len(block))
else:
    print(-1)

Submission status: Accepted

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

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

Submission status: Accepted

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

chk=1
for i in a:
  if i==chk:
    chk+=1
if chk==1:
  print(-1)
else:
  print(len(a)-chk+1)

Submission status: Accepted

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

curr = 1
ans = 0
for num in nums:
    if num != curr:
        ans += 1
    else:
        curr += 1
if ans == n:
    print('-1')
else:
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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

res = 0

count = 1

for i in t:
    if i == count:
        count += 1
        res += 1
if res == 0:
    print(-1)
else:
    print(n-res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

now = 1
ans = 0
for a in A:
    if a == 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()))
#print(n,a)

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

print(n-cnt+1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def gcd(a, b):
    if a < b:
        t = a
        a = b
        b = t
    if b == 0:
        return a
    return gcd(b, a % b)

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())
dat = list(map(int, input().split()))
import collections
dat = collections.deque(dat)
alen = len(dat)
t = 1
res = 0
while len(dat) != 0:
    c = dat.popleft()
    if c == t:
        t+=1
    else:
        res += 1
if  res == alen:
    print("-1")
else:
    print(res)

Submission status: Accepted

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

seq_len = 0

for el in arr:
    if el == seq_len+1:
        seq_len += 1


print(N - seq_len if (seq_len) else "-1")

Submission status: Accepted

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

if 1 not in A:
    print(-1)
    exit()
s=1
for i in range(n):
    if A[i]==s:
        s+=1
print(n-(s-1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
l = map(int,input().split())
l_t = [i for i in enumerate(l)]

a_j = 1 #レンガに書かれている整数
count = 0 #残るレンガの数
for i in range(len(l_t)):
  if l_t[i][1] == a_j: #整数と一致するvalueがあったら
    a_j += 1 #次の整数
    count += 1 #残していいレンガが一個増える
  else:
    continue
if N-count != N:
  print(N-count)
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
num = 1
for a in A:
    if a == num:
        num += 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
for i in a:
    if i==k:
        k+=1
if k==1:
    print(-1)
else:
    print(n-k+1)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

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

Submission status: Accepted

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

c = 1
for a in A:
    c += a == c
print(n-c+1 if c > 1 else -1)

Submission status: Accepted

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

kept = 0
for ai in a:
    if ai == kept + 1:
        kept += 1

print(n - kept if kept > 0 else -1)

Submission status: Accepted

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

count = 0
num = 1

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = map(int, input().split())
current = 1
ans = 0

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

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

Submission status: Accepted

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

if na[0] == 1:
    for i in a:
        if i == res:
            res += 1
        else:
            ans += 1
    print(ans)
else:
    print(-1)

Submission status: Accepted

Solution code:
# シンプルに左から探索するだけ
N = int(input())
A = [int(i) for i in input().split()]

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

cnt = 0
k = 0
for j in A:
  if j == L[k]:
    k += 1
  else:
    cnt += 1 

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
import numpy as np

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))

n = ni()
a = na()
f = 1
cnt = 0
for ai in a:
    if ai == f:
        f += 1
        cnt += 1
if cnt == 0:
    print(-1)
else:
    print(n - cnt)

Submission status: Accepted

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

bef=1
ans=0

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = 1
ans = 0

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

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

print(ans)

Submission status: Accepted

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


def solve(N: int, a: "List[int]"):
    if not 1 in a:
        print(-1)
        return
    c = 1
    for i in range(N):
        if a[i] == c:
            c += 1
    print(N-c+1)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

print(b if b<n else -1)

Submission status: Accepted

Solution code:
N = int(input())
M = [int(i) for i in input().split(" ")]
m = sorted(M)
count = 0
min = 1
if m[0]!=1:
  print(-1)
else:
  for j in range(N):
    if M[j]==min:
      min+=1
    else:
      count+=1
      M[j]=0
  print(count)

Submission status: Accepted

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

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

if ng == 1:
    ans = -1

print(ans)

Submission status: Accepted

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

cnt = 0
num = 1
for i in range(N):
  if a[i] == num:
    num += 1
  elif a[i] != num:
    cnt += 1

if N - cnt == num-1 and 1 in a:
  print(cnt)
else:
  print(-1)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

now = 1
bre = 0

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

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

Submission status: Accepted

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