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: Wrong Answer

Solution code:
from copy import deepcopy

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    for bit in range(1<<N):
        B = deepcopy(A)
        removes = []
        for i in range(N):
            if bit>>i&1:
                removes.append(i)
        cnt = 0
        for r in removes[::-1]:
            del B[r]
        for i, b in enumerate(B):
            if i+1 == b:
                cnt += 1
        if cnt == len(B):
            print(len(removes))
            exit()
    print(-1)

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    numbers=[]
    n = int(input())
    numbers=list(map(int,input().split()))
    buffer=[0]
    for i in range(1,n+1):
        search_range=numbers[i-1:]
        if 1 not in search_range:
            print(-1)
            exit()
        elif i not in search_range:
            break
        else:
            index=search_range.index(i)
            buffer.append(index-sum(buffer))
    print(sum(buffer))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
#-*-coding:utf-8-*-
import sys
input=sys.stdin.readline

def main():
    numbers=[]
    n = int(input())
    numbers=list(map(int,input().split()))
    buffer=[0]
    for i in range(1,n+1):
        search_range=numbers[i-1:]
        out_range=len(numbers[:i-1])
        if i not in search_range and len(buffer)==1:
            print(-1)
            exit()
        elif i not in search_range or i==n:
            break
        else:
            index=search_range.index(i)
            buffer.append(index-sum(buffer))
    print(sum(buffer))

if __name__=="__main__":
    main()

Submission status: Wrong Answer

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

count=0
z=True
j=0

if  1 not in a:
    print(-1)

for k in range(j,n):
    if a[k]==count+1:
        j=k
        count=count+1

print(n-count)

Submission status: Wrong Answer

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

if n == 1 and al[0] == 1:
    print(0)
else:
    i, j, num, ans = 0, 0, 1, 0
    while i < n:
        if al[i] != num:
            i += 1
        else:
            ans += i-j
            j = i+1
            num += 1
            i += 1

    # print(i, j, ans)

    if num != 1 and j != n:
        ans += i-j

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

Submission status: Wrong Answer

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

if n == 1 and al[0] == 1:
    print(0)
else:
    i, j, num, ans = 0, 0, 1, 0
    while i < n:
        if al[i] != num:
            i += 1
        else:
            ans += i-j
            j = i+1
            num += 1
            i += 1

    if ans != 0 and j != n:
        ans += i-j

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

Submission status: Wrong Answer

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

i, j, num, ans = 0, 0, 1, 0
while i < n:
    if al[i] == num:
        ans += i-j
        j = i+1
        num += 1
        i += 1
    else:
        i += 1

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = sorted(set([i for i in a]))
c = ans = i = 0
for i in range(n):
    try:
        if a[i]==b[c]: c+=1
        else: ans+=1
    except: break
print(ans+n-i-1) if ans else\
           print(-1)

Submission status: Wrong Answer

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

for i in range(N):
    if i+1 in A[i:]:
        A = A[:i] + A[A.index(i+1):]
        try_num += 1
    else:
        break

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

Submission status: Wrong Answer

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

ans = 1
cnt = 0

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
A=list(map(int,input().split()))
i = 1
j = 0
K = N-1
while True:
    try:
        k = A[j:].index(i)
        k += j
        K -= k-j
        if K < 0:
            print(-1)
            exit(0)
        j = k+1
        i += 1
    except:
        if j == N:
            print(N-1-K)
        else:
            print(-1)
        break

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 == 0:
  print(-1)
else:
  print(crash)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	D
# CreatedDate:  2020-08-30 14:25:42 +0900
# LastModified: 2020-08-30 14:33:11 +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 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: Wrong Answer

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

Submission status: Wrong Answer

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(count-1)
    count = 0
    x += 1
if list1 == []:
    print(-1)
else:
    print(sum(list1))

Submission status: Wrong Answer

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[0]!=f):
      del a[0]
      ans += 1
    else:
      f += 1
  print(ans)
else:
  print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(cnt if cnt>=0 else -1)

Submission status: Wrong Answer

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
print(len(A)-start)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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

def myindex(l, x, default=-1):
    if x in l:
        return l.index(x)
    else:
        return default

for i in range(1, n+1):
  b = myindex(a, i, default=-1)
  print(b)
  if b == -1:
    del a[(i-1):]
    break
  else:
    del a[(i-1):b]


if len(a) == 0:
  print('-1')

else:
  print(n - len(a))

Submission status: Wrong Answer

Solution code:
n = int(input())

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

def index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

for i in range(1, n+1):
  b = index(a, i, default=False)
  if b == False:
    del a[(i-1):]
  else:
    del a[(i-1):b]


if len(a) == 0:
  print('-1')

else:
  print(n - len(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
num = 0
j = -1
if max(a) != min(a) or len(a) == 1:
    a.reverse()
    a = list(dict.fromkeys(a))
    a.reverse()
    for i in range(1,n+1):
        if i in a and j < a.index(i):
            j = a.index(i)
            num += 1
    print(n-num)
else:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
## 63
import sys

n = int(input())
a = list(map(int,input().split()))
num = len(a)
point1 = 0
point2 = 0

try:
    point1  = a.index(1)
except:
    print(-1)
    sys.exit()
a = a[point1:]

point1 = 1

for i in range(2,n):
    try:
        point2  = a.index(i)
    except:
        break
    a = a[:point1] + a[point2:] 
    point1 += 1
    
print(num-len(a))

Submission status: Wrong Answer

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
    
    print(i - 1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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
print(n-sum(ans))

Submission status: Wrong Answer

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
print(N-f)

Submission status: Wrong Answer

Solution code:
# ABC_148_D_Brick_Break.py

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

if not 1 in A:
    print(-1)
    exit()
k = A.index(1)
A = A[k+1:]
print(A)

i=2
while i in A:
    k=A.index(i)
    A=A[k+1:]
    i+=1
print(N-i+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))

    A_d = []

    for a in A:
        a -= 1
        A_d.append(a)

    for bit in range(1<<N):
        A_copy = deepcopy(A_d)
        for i in range(N):
            if bit>>i&1:
                del A_copy[i]
        print(A_copy)
        if len(A_copy) == len(A_d):
            continue
        for j, a in enumerate(A_copy):
            if j==a:
                print(j, a)
                print(len(A_d)-len(A_copy))
                quit()
    print(-1)

Submission status: Wrong Answer

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

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

if now_number == 0:
  print(-1)
else:
  print(now_count+1 - now_number)

Submission status: Wrong Answer

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

cnt = 0
check = 1
ls = [i for i in range(1,N+1)]
bool1 = False

for a in A:
  if a == check:
    check += 1
  else:
    cnt += 1
  if [i for i in range(1,check+1)] == ls[:(N-cnt)] and check != 0:
    bool1 = True
    break

if bool1:
  print(cnt)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
cnt = 0
idx = 0
while 1:
  if (cnt+1) in A[idx:]:
    cnt += 1
    idx = A.index(cnt)
  else:
    break

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 > 1 else -1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import exit

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


def is_equal(x, target):
    if target == list(range(1, x+1)):
        return True
    else:
        return False


next_value = 1
break_count = 0

while not is_equal(len(A), A):
    try:
        index = A.index(next_value)
        del A[next_value - 1:index]
        break_count += (index - (next_value - 1))
        next_value += 1
    except:
        print(-1)
        exit()

print(break_count)

Submission status: Wrong Answer

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

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

print(N - cur)

Submission status: Wrong Answer

Solution code:
from collections import deque

N=int(input())
a=deque(map(int,input().split()))
cur=1
ans=0
if not 1 in a:
  print(-1)
  exit()
while cur!=len(a):
  if a[cur-1]!=cur:
    a.popleft()
    ans+=1
  else:
    cur+=1
print(ans)

Submission status: Wrong Answer

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

index = 0
i = 1
while True:
    for j in range(N-1, index-1, -1):
        if a[j] == i:
            index = j + 1
            i += 1
            match += 1
            break
    else:
        break

print(N - match if match else -1)

Submission status: Wrong Answer

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

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

if 1 not in a:
    ans = -1

print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

print(len(A)-count)

Submission status: Wrong Answer

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

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

print(count)

Submission status: Wrong Answer

Solution code:
#<D>
n = int(input())
a = list(map(int,input().split()))
c=a[n-1]
a.sort()
a=list(set(a))
#fuct = [i for i in range(1,n+1)]
b=[0]
i = 0
ans = 0
while True:
    if a[0] != 1:
        print(-1)
        break
    if b[i]+1 == a[i]:
        b.append(a[i])
        if a[i] == c:
            print(n - len([i for i in b if i > 0]))
            break
    elif b[i]+1 < a[i]:
        print(n-len([i for i in b if i > 0]))
        break
    elif b[i] == a[i]:
        b.append(0)
    i += 1

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
c=a[n-1]
a.sort()
a=list(set(a))
#fuct = [i for i in range(1,n+1)]
b=[0]
i = 0
ans = 0
while True:
    if a[0] != 1:
        print(-1)
        break
    if b[i]+1 == a[i]:
        b.append(a[i])
        if a[i] == c:
            print(n - len(b) + 1)
            break
    elif b[i]+1 < a[i]:
        print(n-len(b)+1)
        break
    elif b[i] == a[i]:
        ans += 0
    i += 1

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a =  list(map(int,input().split()))
list_num = len(a)
a.reverse()
kl = 1
kr = len(a)
cnt = 0
while kl < len(a):
  if a[kr-1] != kl:
    try:
      del a[a.index(kl)+1:kr]
    except:
      if cnt == 0:
        print(-1)
        exit()
      else:
        print(cnt)
  cnt += 1
  kl += 1
  kr = len(a) - cnt

print(list_num-len(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a =  list(map(int,input().split()))
list_num = len(a)
a.reverse()
kl = 1
kr = len(a)
cnt = 0
while kl < len(a):
  if a[kr-1] != kl:
    try:
      del a[a.index(kl)+1:kr]
    except:
      print(-1)
      exit()
  cnt += 1
  kl += 1
  kr = len(a) - cnt

print(list_num-len(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a =  list(map(int,input().split()))
list_num = len(a)
a.reverse()
kl = 1
kr = len(a)
cnt = 0
while kl <= len(a):
  if a[kr-1] != kl:
    try:
      del a[a.index(kl)+1:kr]
    except:
      print(-1)
      exit()
  cnt += 1
  kl += 1
  kr = len(a) - cnt

print(list_num-len(a))

Submission status: Wrong Answer

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

list_num = len(a)
kl = 0

for i in b:
  try:
    del a[kl:a.index(i)]
  except:
    break
  kl += 1

for i,v in enumerate(a):
  if i+1 != v:
    print(-1)
    exit()
print(list_num-len(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a =  list(map(int,input().split()))
list_num = len(a)
kl = 0
kr = 1
while kl < len(a):
  if kr in a:
    del a[kl:a.index(kr)]
  else:
      print(-1)
      exit()
  kl += 1
  kr += 1


print(list_num-len(a))

Submission status: Wrong Answer

Solution code:
n = int(input())
a =  list(map(int,input().split()))
list_num = len(a)
kl = 0
kr = 1
while kl < len(a):
  if a[kl] != kr:
    try:
      del a[kl:a.index(kr)]
    except:
      print(-1)
      exit()
  kl += 1
  kr += 1


print(list_num-len(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

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

		if x==y:
			return

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

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

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

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

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

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

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

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

n = readInt()
a = readInts()

ans = 10*10

for i in range(n):
	if i+1>a[i]:
		ans = min(ans,i+1-a[i])
if ans==10*10:
	print(-1)
else:
	print(ans)

Submission status: Wrong Answer

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

next_target = 1

idx = []

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

if not idx:
    print(-1)
else:
    prev = -1
    sum_ = 0
    for x in idx:
        sum_ += x - prev - 1
        prev = x

    print(sum_)

Submission status: Wrong Answer

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
    continue
  cnt += 1

print(cnt if num > 1 else 0)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

nxt = 1
broken = 0
for i in range(n):
  if B[i]==nxt:
    nxt += 1
  else:
    broken += 1
    
print(broken)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
aN=list(input().split())
arr=[]
for a in aN:
    arr.append(int(a))

SatisfactoryOrder=False
targetNumber=1
hummer=0
if arr.count(1)<1:
    print(-1)
    exit()

Submission status: Wrong Answer

Solution code:
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

n = I()
a = Intl()
cnt = 0
a = a[::-1]
now = a[0]
result = [now]
for i in range(1,n):
    if a[i] >= now:
        cnt += 1
    elif now - 1 == a[i]:
        now = a[i]
    else:
        cnt += 1
print(cnt if now == 1 and a[0] == len(a)-cnt else -1)

Submission status: Wrong Answer

Solution code:
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

n = I()
a = Intl()
cnt = 0
a = a[::-1]
now = a[0]
#print(now,a)
for i in range(1,n):
    if a[i] >= now:
        cnt += 1
    elif now-1 == a[i]:
        now = a[i]
    else:
        cnt += 1
print(cnt if now == 1 else -1)

Submission status: Wrong Answer

Solution code:
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

n = I()
a = Intl()
cnt = 0
now = a[-1]
a = a[::-1]
for i in range(1,n):
    if a[i] >= now:
        cnt += 1
    else:
        now = a[i]
print(cnt if now == 1 else -1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

flag = 1
if not 1 in a:
	flag = 0

cnt = 0
j = 1
for i in range(1,N):
	if a[i] == j:
		#print(i,j,cnt)
		j += 1
		cnt += 1
	else:
		pass

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
dp = [0]*N 

if 1 not in a_list:
    print(-1)
else:
    dp[0]= 1
    count = 0
    for i in range(N-1):
        if dp[i] == a_list[i]:
            dp[i + 1] = dp[i] + 1
        else:
            dp[i + 1] = dp[i]
            count += 1
    print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
dp = [0]*N 

if 1 not in a_list:
    print(-1)
else:
    dp[0]= 1
    count = 0
    for i in range(N-1):
        if dp[i] == a_list[i]:
            dp[i + 1] += 1
        else:
            dp[i + 1] = dp[i]
            count += 1
    print(count)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
N = int(input())
a = list(map(int, input().split()))
num = 1
while len(a) >= 1:
    if num in a:
        a = a[a.index(num) +1 :]
        num += 1
    else:
        print(-1)
        exit()
else:
    print(N - num + 1)

Submission status: Wrong Answer

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

if A.count(1)==0:
  print('-1')
  exit()
  
if A==sorted(A):
  print('0')
  exit()

ans=0
j=1

for i in range(N):
  if A[i]==j:
    ans+=1
    j+=1
    print('OK')
  else:
    print('a')
  
print(N-ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, *a = map(str, open(0).read().split())
    s = ''.join(a)

    m = 1
    cnt = 0
    while 1:
        ind = s.find(str(m))
        if ind == -1:
            cnt += len(s)
            break
        s = s[ind + 1:]
        cnt += ind
        m += 1

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    n, *a = map(int, open(0).read().split())
    a = deque(a)
    now = 0
    ind = 1
    m = n
    tmp = 0
    while a:
        now = a.popleft()

        if now == ind:
            ind += 1
            tmp = now
        else:
            m -= 1

    if tmp == m:
        print(n - m)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    from collections import deque
    n, *a = map(int, open(0).read().split())
    a = deque(a)
    now = 0
    ind = 1
    m = n
    while a:
        now = a.popleft()

        if now == ind:
            ind += 1
        else:
            m -= 1

    if now == m:
        print(n - m)
    else:
        print(-1)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

flag = True
hakai = 0
while flag==True:
    flag = False
    for i in range(len(a)-1):
        if a[i] >= a[i+1]:
            hakai += 1
            a[i] = 0
            flag = True
    a = [i for i in a if i!=0]

if sum(a)==(len(a)+1)*len(a)/2:
    print(hakai)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
ad=deque(a)
tmp=1
ans=0
while ad:
    if ad[0] ==tmp:
        tmp+=1
        ad.popleft()
    else:
        ad.popleft()
        ans+=1
if ans==0:
    print(-1)
else:
    print(ans)