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

min_indexlist = [a_list.index(min(a_list))]

m = min(a_list)

for i in range(len(a_list)):
    if( a_list[i] == m+1):
        m = a_list[i]
        min_indexlist.append(i)

#砕いたレンガの後のリスト
crash_list = []
for i in range(len(min_indexlist)):
    crash_list.append(a_list[min_indexlist[i]])

#print(crash_list)

#砕いたレンガの数
crash = n - len(min_indexlist)


frag=True

for i in range(len(crash_list)):
    if(crash_list[i] != i+1):
        frag = False


if(frag == True):
    print(crash)
elif(frag == False):
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
n_index = list(range(1, n+1))

breaks = 0
if min(a_list) > 1:
    print(-1)
else:
    for n in n_index:
        if len(a_list) > 0:
            if min(a_list) > n:
                breaks += len(a_list)
                print(breaks)
                exit()
            if n in a_list:
                i = a_list.index(n)
                a_list = a_list[i+1:]
                breaks += i
    print(breaks)

Submission status: Wrong Answer

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

# 無理な場合は-1

# 1からN-1が出てくるまでの個数を足していく？
idx = 0
bf_idx = 0
num = 1
brick = 0
flg = False
while True:
    if num == A[idx]:
        flg = True
        num += 1
        brick += idx - bf_idx
        bf_idx = idx + 1
    idx += 1
    if idx > N - 1:
        break

if flg == False:
    brick = -1
print(brick)

Submission status: Wrong Answer

Solution code:

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


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

number = [n for n in range(1, N)]

breaking_blocks = 0

for num in number:
  index = my_index(a_num, num)
  if index:
    breaking_blocks = breaking_blocks + index
    a_num = a_num[index+1:]
  elif breaking_blocks == 0:
    breaking_blocks = -1
    break
  else:
    break

print(breaking_blocks)

Submission status: Wrong Answer

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

j=1
ans=0
tmp=0
for i in range(0,N):
  if a[i]==j:
    ans += i-tmp
    tmp = i+1
    j += 1
#print(i,j,ans,tmp)
if ans==0 and j>1:
  print(ans)
elif ans==0 and j==1:
  print(-1)
elif N>tmp:
  print(ans+(N-tmp))
else:
  print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if 1 not in A:
    print(-1)
    exit()
for i in range(N-1):
    for j in range(i+1,N):
        if A[i]>=A[j]:
            count+=1

            break

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
num = 1
if 1 in A:
    b = A.index(1)
    c = len(A)
    while(b != "error"):
        del[A[num-1:b]]
        num += 1
        if num in A:
            b = A.index(num)
        else:
            b = "error"
    print(c - len(A))
    
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int,input().split()))
l_first = min(l)
l_min = min(l)
x = l.index(l_min)

for _ in range(N):
    if l_min+1 in l[x:]:
        l_min +=1
        x = l[x:].index(l_min)
    else:
        break

if l_first == l_min:
    print(-1)
else:
    print(len(l)-(l_min-l_first+1))

Submission status: Wrong Answer

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

if 1 not in aa:
    print(-1)
    exit()
cmp_list = [1]
for a in aa:
    if a == cmp_list[-1] + 1:
        cmp_list.append(a)
print(len(aa) - len(cmp_list))

Submission status: Wrong Answer

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

while i < len(a):
    if i+1 in a:
        ii = a.index(i+1)
        a = a[0:i] + a[ii:]
        c = c + ii-i
        i = i+1
    else:
        break
if i == 0:
    c = -1
print(c)

Submission status: Wrong Answer

Solution code:
#n=int(input())
#a,b=map(int, input().split())
#l=list(map(int, input().split()))
N=int(input())
a=list(map(int, input().split()))
#phase_1
i=0
while i<len(a):
  if i+1<a[i]:
    a.pop(i)
  else:
    i+=1
#phase_2
a.sort()
count=len(a)
for j in range(count-1):
  if a[j]==a[j+1]:
    count-=1

#print(a)
if count==0:
  count=N+1
print(N-count)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
c = 0
d = range(1, n + 1)
for nn in range(1, n + 1):
    if a == d:
        break
    if nn in a:
        if a.index(nn) == nn -1:
            continue
        else:
            cc = len(a[nn - 1:a.index(nn)])
            c += cc
            d = d[:cc]
            del a[nn - 1:a.index(nn)]
    else:
        if c == 0:
            print(-1)
            exit()
        else:
            c +=  len(a[nn:])
            break
print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
index = 0
break_ = 0
ans = []
while break_ < N - 1:
  if index == len(a):
    break
  elif index + 1 != a[index]: #一致しなかったら壊す
    a.pop(index)
    break_ += 1
  else: #一致したら次
    ans.append(index + 1)
    index += 1
#print(index)
#print(break_)
#print([i + 1 for i in range(N - break_)])
#print(ans)
#print(a)
if ans != a:
  print(-1)
else:
  print(break_)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
n_index = list(range(1, n+1))

breaks = 0
if min(a_list) > 1:
    print(-1)
else:
    for n in n_index:
        if len(a_list) > 0:
            if min(a_list) > n:
                breaks += len(a_list)
                print(breaks)
                exit()
            if n in a_list:
                i = a_list.index(n)
                a_list = a_list[i+1:]
                breaks += i
                print(a_list)
    print(breaks)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

i = 1
if 1 in a:
    while i in a:
        del a[:a.index(i)]
        i += 1
        print(n-i+1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
number = list(map(int,input().split()))
count = 0
if not 1 in number:
    print(-1)
else:
    for i in range(N):
        now = i+1
        if now in number:
            #number.index(now)が左から数えたときのnowのindex番号
            count += number.index(now)
            number[:number.index(now)+1]=[]

        else:
            count += len(number)
    print(count)

Submission status: Wrong Answer

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

min_indexlist = [a_list.index(min(a_list))]

m = min(a_list)

for i in range(len(a_list)):
    if( a_list[i] == m+1):
        m = a_list[i]
        min_indexlist.append(i)

#砕いたレンガの後のリスト
crash_list = []
for i in range(len(min_indexlist)):
    crash_list.append(a_list[min_indexlist[i]])

#砕いたレンガの数
crash = n - len(min_indexlist)


frag=True

for i in range(len(crash_list)):
    if(crash_list[i] != i+1):
        frag = False

if(frag == True):
    print(crash)
elif(frag == False):
    print(-1)

Submission status: Wrong Answer

Solution code:
N=int(input())
number=input().split(' ')
number_list=[]
for i in number:
    number_list.append(int(i))
candidates=[]
candidate=[number_list[0]]
for i in range(1,N):
    if(number_list[i-1]>number_list[i]):
        candidates.append(candidate)
        candidate=[number_list[i]]
    else:
        candidate.append(number_list[i])
candidates.append(candidate)
ml=1
for i in candidates:
    if(ml<len(i)):
        ml=len(i)

if(N==1):
    print(0)
elif(N==ml):
    print(-1)
else:
    print(N-ml)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = []
cnt = 0
for i in range(1, n + 1):
    j = i
    cnt = 0
    try:
        while i != a[j - 1]:
            cnt += 1
            j += 1
    except IndexError:
        break

if 1 not in set(a):
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
a = list(map(int, input().split()))
count=0
i=0
while True:
  if i+1 >= len(a): break
  if a[i] >= a[i+1] :
    del a[i]
    count+=1
    continue
  i+=1
if count >= n:
  print('-1')
else:
  print(str(count))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
K = a[-1]

for i in range(1, K):
  if i not in a:
    print(-1)
    exit()

print(N - K)

Submission status: Wrong Answer

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

ans = 0
for i in range(1, N):
    A = ''.join(a)
    c_i = A.find(str(i))
    c_j = A.rfind(str(i+1))
    if c_i == -1:
        ans = -1
        break
    elif c_j == -1:
        ans = N - i
        break
    elif c_i < c_j:
        a = a[c_i:]
    elif c_i > c_j:
        ans = N - i
        break

print(ans)

Submission status: Wrong Answer

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

x = 1
for i in a:
    if i == x:
        print(i,x)
        x += 1

print(n-(x-1))

Submission status: Wrong Answer

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

ans = []
count = 0
q = 1

for a in arr:
	if a == q:
		q += 1
		ans.append(a)
	else:
		count += 1

print(count)

Submission status: Wrong Answer

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

c = 0
for i in range(max(A)):
    if i+1 in A:
        c += 1
        A = A[A.index(i+1):]
        if len(A) == 1:
            print(N-c)
            exit()
    else:
        print(-1)
        exit()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input().split(' ')
p = 1
broken = 0
id = 1
for i in range(n):
  a[i] = int(a[i])
  if id == a[i]:
    id += 1
  else:
    broken += 1
print(broken)

Submission status: Wrong Answer

Solution code:
import sys

first = int(input())
numbers = list(map(int, input().split()))

if numbers == [] or 1 not in numbers:
    print(-1)
    sys.exit(0)

index = 0
value = 1
breaks = 0
while True:
    try:
        n = numbers.index(value)
    except ValueError:
        if len(numbers) == first and first != 1:
            print(-1)
        else:
            print(breaks + len(numbers) - (value - 1))
        sys.exit(0)
    breaks += n - (value - 1)
    numbers = numbers[0:(value - 1)] + numbers[n:]
    value += 1

Submission status: Wrong Answer

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

save = 1
ans = -1
flag = True

for idx,i in enumerate(a):
    if idx == i and flag:
        flag = False
    if save == i:
        if i == 1:
            ans += 1
        save += 1
    else:
        ans += 1
if flag:
    print('0')
else:
    print(ans if len(a)-1 != ans else '-1')

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
A = input()

if A == '1':
    print(0)
    exit()
                  

idx_prev = 0
for i in range(1, n + 1):
    if A[idx_prev:] == str(i):
        idx_prev += 1
        continue

    idx = A[idx_prev:].find(str(i))
    if idx == -1:
        break

    idx_prev += idx + 1

if i == 1:
    print(-1)
    exit()

print(n - i + 1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
n_index = list(range(1, n+1))

breaks = 0
if min(a_list) > 1:
    print(-1)
else:
    for n in n_index:
        if len(a_list) > 0:
            if min(a_list) > n:
                print(breaks)
                exit()
            if n in a_list:
                i = a_list.index(n)
                a_list = a_list[i+1:]
                breaks += i
    print(breaks)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = []
cnt = 0
for i in range(1, n):
    j = i
    cnt = 0
    try:
        while i != a[j - 1]:
            cnt += 1
            j += 1
    except IndexError:
        break

if 1 not in set(a):
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

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

position = 0
ans = 0
while True:
  if position == N:
    break
  for i in range(position,N):
    if a[i] == position + 1:
      position += 1
      ans += 1
      break
  else: #終了
    
    if ans == 0: #全く無理
      print("-1")
    else: # 途中でもう揃えられない
      print(N-ans)
    exit()
print(N - ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
l = list(map(int,input().split()))
l_first = min(l)
l_min = min(l)
x = l.index(l_min)

while True:
    if l_min+1 in l[x:]:
        l_min +=1
        x = l[x:].index(l_min)
    else:
        break

if l_first == l_min:
    print(-1)
else:
    print(len(l)-(l_min-l_first+1))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input().split()
count = 1

for i in range(0,n):
  #print(i)
  if int(a[i]) == count:
    count +=1
print(n-count+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = list(map(int,input().split()))
c = 0
l = []
ind = 0
"""
for i in range(n):
  if s[i] == i:
    print(c)
    break
"""
for j in s:
  c += 1
  con=abs(int(j) - (ind+1))
  l.append(con)
  ind += 1
  ma=min(l)
print(ma)

Submission status: Wrong Answer

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

c = 1
cp = 0

for i in range(N):
  ai = an[i]
  if ai != c:
    cp += 1
    
  elif ai == c:
    c += 1
    
print(cp)

Submission status: Wrong Answer

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

for i in range(1, n+1):
  for j in range(b, n):
    if i == a[j]:
      out += j-b
      b = j+1
      break
  else:
    out += n-b
    break

print(out)

Submission status: Wrong Answer

Solution code:
test = ""
test = \
"""
1
1
"""
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
    

n = int(input2())
nums = list(map(int, input2().strip().split()))
nums.reverse()

ans = 0
wanted = 1
while nums:
    tmp = nums.pop()
    if tmp == wanted:
        wanted += 1
    else:
        ans += 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

p = 0
c = 1
i = 0
ans = 0
while i < n:
    if a[i] == c:
        ans += i - p
        p = i + 1
        c += 1

    i += 1

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a=[int(x) for x in input().split()]
i=0
cnt=0
x=1
hozon=0
while i<n:
	if a[i]==x:
		cnt+=i-hozon
		hozon=i+1
		x+=1
	i+=1
	
if x == 1:
	cnt = -1
print(int(cnt))

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
li = list(map(int, input().split()))

list = list(np.arange(1, N+1))
res = []
count = 0
for i in range(N):
    if li[i] != list[0]:
        count += 1
    else:
        res.append(list[0])
        list.pop(0)

if count == 0:
    print(0)
elif list[0] - 1 == li[N-1]:
    print(count)
else:
    print(-1)

Submission status: Wrong Answer

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

  
if 1 not in A:
  ans=-1
else:
  ans=0
  for i in range(1,n+1):
    if i in A:
      k=A.index(i)
      ans+=k
      A=A[k+1:]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

ans = 0
_find = 1
before = 0

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

for i in range(n):
    if a[i]==_find:
        ans += i-before
        _find += 1
        before = i+1

print(ans)

Submission status: Wrong Answer

Solution code:
#!python3.4.3

n = int(input())

a_list = list(map(int, input().split(' ')))
result = []

c = 0
for i in range(1, n+1):
    for j in range(c, n):
        if i == a_list[j]:
            result.append(i)
            c = i
            break
    if len(result) != i:
        break

# print(result)

if len(result) == 0:
    print(-1)
else:
    print(n - len(result))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
i = 1
n = 0
if 1 not in a:
    i = -1
else:
    while True:
        if i not in a:
            n += len(a)
            break
        n += a.index(i)
        a = a[a.index(i)+1:]
        i += 1

print(n)

Submission status: Wrong Answer

Solution code:
import sys
from bisect import bisect_left
from collections import deque
input = sys.stdin.readline
N = int(input())
A = [int(x) for x in input().strip().split()]
t = 0
f = 1
ans = deque([])

for n, a in enumerate(A, start=1):
    if a == f:
        ans.append(n - t - 1)
        f += 1
        t = n
else:
    if t != N:
        ans.append(n - t - 1)

if f == 1:
    print(-1)
else:
    print(sum(ans))

Submission status: Wrong Answer

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

ans = 1
count = 1
y = 0

if A.count(1) == 0:
    ans = -1
else:
    X = []
    for i in range(n):
        try:
            x = A[y:].index(count)
            y = x + 1
            X.append(x)
            count += 1
        except:
            break
    ans = sum(X)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())

a = list(map(int, input().split()))
index = 1
cnt = 0

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



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

Submission status: Wrong Answer

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

import sys
import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
#        for line in sys.stdin:
#            lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = False
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    n = [int(e) for e in lines[0].split(" ")][0]
    a = [int(e) for e in lines[1].split(" ")]

    return (n, a)

def solve(n, a):

    current_num = 1
    count = 0
    result = []
    for i in range(n):
        if a[i] != current_num:
            count = count + 1
        else:
            current_num = current_num + 1
            result.append(a[i])

    if len(result) == 0:
        count = -1 
    elif len(result) == 1 and result[0] == 1:
        count = 0
    elif len(result) == 1 and result[0] != 1:
        count = -1
    else: 
        for i in range(1, len(result)):
            if result[i] != result[i-1] + 1:
                count = -1
                break


    return count

def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r)
    else:
        print("%s" % result)

if __name__ == '__main__':

    main()

Submission status: Wrong Answer

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

#print(A)
tmp = 0
count = 0
for i in reversed(range(N)):
	if i == N-1:
		tmp = A[i]
	else:
		if A[i]==tmp-1:
			tmp = A[i]
		else:
			count += 1
if tmp != 1:
	print("-1")
else:
	print(count)

Submission status: Wrong Answer

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

position = 0
ans = 0
while True:
  if position == N:
    break
  for i in range(position,N):
    if a[i] == position + 1:
      position += 1
      ans += 1
      break
  else: #終了
    
    if ans == 0: #全く無理
      print(-1)
    else: # 途中でもう揃えられない
      print(N-ans)
    exit()
print(N - ans)

Submission status: Wrong Answer

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

p = 0
c = 1
i = 0
ans = 0
while i < n and p < n:
    if a[i] == c:
        ans += i - p
        p = i + 1
        c += 1

    i += 1

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = []
cnt = 0
for i in range(1, n):
    k = i
    j = i
    cnt = 0
    try:
        while k != a[j - 1] and j <= n:
            cnt += 1
            j += 1
    except IndexError:
        break

if 1 not in a:
    print(-1)
else:
    print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
alist = list(map(int, input().split()))
left = 0
for i in range(1, N + 1):
    try:
        old_left = left
        left = alist[left:].index(i)
        p = left + old_left
    except ValueError:
        if i == 1:
            print(-1)
        else:
            print(N - i + (N - p))
        break
else:
    print(0)

Submission status: Wrong Answer

Solution code:
def solve():
    N, *a = map(int, open(0).read().split())
    
    if 1 not in a:
        print(-1)
        exit()
    
    ans = 0
    target = 1
    i_tmp = -1
    for i, x in enumerate(a):
        if x == target:
            ans += i - i_tmp - 1
            i_tmp = i
            target += 1
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

res = -1
for i in range(N):
	K = a[i:]
	if K == list(range(1, N-i+1)):
		res = i
print(res)

Submission status: Wrong Answer

Solution code:
import sys
N=int(input())
a = sys.stdin.readline().split()
i=1
for num in a:
    if int(num) == i:
        i=i+1
if i != 1:
    print(N-i+1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

tmp = a_list[n-1]
max_num = tmp
for i in range(n-1, -1, -1):
    if a_list[i] == tmp - 1:
        tmp -= 1

if tmp > 1:
    print(-1)
else:
    print(n - max_num)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
''' 
------------------------
author : iiou16
------------------------
'''

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

    count = 0
    id = 1

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))	
i = 0
brk = 0
flg = True
while i + brk<= n-1:
    while a[i+brk] != i+1:
        brk += 1
        if i + brk >= n-1:
            break
    i +=1
if brk < n-1:
    print(brk)
else:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

check = 1
ans = 0

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

print(n - ans)

Submission status: Wrong Answer

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

if 1 not in a:
  print("-1")
  exit()
else:
  nokosu = 1
  a=a[a.index(1)+1:]

for i in range(2,n):
  if i not in a:
    print(n - nokosu)
    exit()
  else:
    nokosu += 1
    if len(a)==1:
      print(n - nokosu)
      exit()
    a = a[a.index(i)+1:]

Submission status: Wrong Answer

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

ans = 1
count = 1
y = 0

if A.count(1) == 0:
    ans = -1
else:
    
    for i in range(n):
        try:
            x = A[y:].index(count) + len(A[:y])
            ans += x - y -1
            y = x
            count += 1
        except:
            break

print (ans)

Submission status: Wrong Answer

Solution code:
n,*a=map(int,open(0).read().split())
now,ans=a[-1],0
for i in a[::-1]:
    if i==now:
        now-=1
    else:    
        ans+=1
print(ans if now==0 else -1)

Submission status: Wrong Answer

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

if 1 not in a:
  print("-1")
  exit()
else:
  nokosu = 1
  a=a[a.index(1)+1:]

for i in range(2,n):
  if i not in a:
    print(n - nokosu)
    exit()
  else:
    nokosu += 1
    if len(a)==1:
      print(nokosu)
      exit()
    a = a[a.index(i)+1:]

Submission status: Wrong Answer

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

breaks = 0
if min(a_list) > 1:
    print(-1)
else:
    for n in n_index:
        if n in a_list:
            i = a_list.index(n)
            a_list = a_list[i+1:]
            breaks += i
    print(breaks)

Submission status: Wrong Answer

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

s = 1
count = 0
fail = 0
ans = 0
for a in A:
    if a == s:
        s += 1
        ans += count
        count = 0
    else:
        count += 1
        fail += 1

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

Submission status: Wrong Answer

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

if min(a)!=1:
  print(-1)
else:
  a_set = list(set(a))
  if sum(a_set) == ((len(a_set) * (len(a_set)+1))/2):
    print(int(int(N) - len(a_set)))

Submission status: Wrong Answer

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

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

if ans==0 and j>1:
  print(ans)
elif ans==0 and j==1:
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

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

tmp = a_list[n-1]
max_num = tmp
for i in range(n-1, 0, -1):
    if a_list[i] == tmp - 1:
        tmp -= 1

if tmp > 1:
    print(-1)
else:
    print(n - max_num)

Submission status: Wrong Answer

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

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

# 不可能
if (ans == 0 and crash != 0):
    print(-1)
else:
    print(ans + crash)

Submission status: Wrong Answer

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

c = 1
b = [1]
while c!=0 and (len(a) != 0) :
    b = []
    c = 0
    for i in enumerate(a):
        if not (i[0]+1 < i[1]):
            b.append(i[1])
        else:
            c = 1
    a = b
 
print(a)
if len(a)==0:
    print(-1)
else:
    print(N-len(set(a)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import numpy as np

n = int(input())
A = np.array(list(map(int, input().split())))

if np.array_equal(A, np.array([1])):
    print(0)
    exit()
                  

idx_prev = 0
for i in range(1, n+1):
    idx = np.where(A[idx_prev:]==i)[0]
    if len(idx) == 0:
        break

    idx_prev += idx[0] + 1

if i == 1:
    print(-1)
    exit()

print(n - i + 1)

Submission status: Wrong Answer

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

s = 1
count = 0
fail = 0
ans = 0
for a in A:
    if a == s:
        s += 1
        ans += count
        count = 0
    else:
        count += 1
        fail += 1

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