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:
import sys

n = int(input())
numbers = list(map(int, sys.stdin.readline().split()))

breaks = 0
current = 1
for i in range(n):
    if numbers[i] == current:
        current += 1
    else:
        breaks += 1
print(breaks)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
temp = 1
try:
    iti = a[::-1].index(temp)
    temp += 1
except:
    print(-1)
    exit()
if iti > 0:
    try:
        iti = a[:0:-1].index(temp)
    except:
    	print(-1)
    	exit()
else:
    print(N-1)
    exit()
while iti > 0:
    if temp == 2:
      iti = 0
    temp += 1
    try:
        iti = a[:iti:-1].index(temp)
    except ValueError:
        print(-1)
        exit()
print(N-temp)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
break_list=[]
for i in range(len(a)):
    if a[i]<=i+1:
        x=i+1-a[i]
        break_list.append(x)
if len(break_list) != 0:
	print(min(break_list))
else:
  print(-1)

Submission status: Wrong Answer

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

if len(ab) == 1 and ab[0] == 1:
    print(0)
    exit()
else:
    for i in range(0, n):
        for ii, v in enumerate(ab):
            if i + 1 not in ab:
                f_i += len(ab)
                ab = []
                break
            elif v == i + 1:
                f_i += ii
                ab = ab[ii + 1::]
                break

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

Submission status: Wrong Answer

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

#print(L)
number = 1
ans = 0
for x in L:
    if(x == number):
        number += 1
        ans += 1

if(ans == 0):
    print(-1)
elif(ans ==1):
    print(0)
else:
    print(N - number +1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def check_numbers(numbers):
  another_numbers = []
  for i in range(len(numbers)):
    another_numbers.append(i+1)
  return numbers == another_numbers


def remove_element(numbers):
  another_numbers = []
  for i in range(len(numbers)):
    another_numbers.append(i+1)
  
  for i in range(len(numbers)):
    if numbers[i] > another_numbers[i]:
      numbers.pop(i)
      return True
    
  for i in range(len(numbers) - 1):
    if numbers[i] == numbers[i+1]:
      numbers.pop(i)
      return True
  
  return False


N = int(input())
numbers = [int(n) for n in input().split()]
canRemove = True
num_of_crash = 0

while canRemove:
  canRemove = False
  if len(numbers) == 0:
    print(-1)
    exit()
  if check_numbers(numbers):
    print(num_of_crash)
    exit()
  else:
    canRemove = remove_element(numbers)
    num_of_crash += 1
    N -= 1
print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().strip().split(" ")))
x=[0 for _ in range(n)]
k=1
for i in range(n):
    x[a[i]-1]+=1
ans=0

for i in range(n):
    if x[i]==0:
        k=0
        kk=i
        break
    elif x[i]>0:
        ans+=(x[i]-1)
if k==0 and ans>0:
    for i in range(kk,n):
        if x[i]==0:
            ans+=1
        ans+=x[i]
if k==0 and ans==0:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = input().split()
l_len = len(l)
flag = 0
for i in range(n):
    if str(i+1) in l:
        index = l.index(str(i+1))
        # print(index)
    else:
        break
    flag += 1
    del l[i:index]

if flag != len(l) or n == len(l):
    print("-1")
else:
    print(l_len - len(l))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def check_numbers(numbers):
  another_numbers = []
  for i in range(len(numbers)):
    another_numbers.append(i+1)
  return numbers == another_numbers


def remove_element(numbers):
  another_numbers = []
  for i in range(len(numbers)):
    another_numbers.append(i+1)
  
  for i in range(len(numbers)):
    if numbers[i] > another_numbers[i]:
      numbers.pop(i)
      return True
    
  for i in range(len(numbers) - 1):
    if numbers[i] == numbers[i+1]:
      numbers.pop(i)
      return True
  
  return False


N = int(input())
numbers = [int(n) for n in input().split()]
canRemove = True
num_of_crash = 0

while canRemove:
  canRemove = False
  if len(numbers) == 0:
    print(-1)
    exit()
  if check_numbers(numbers):
    print(num_of_crash)
    exit()
  else:
    print(numbers)
    canRemove = remove_element(numbers)
    num_of_crash += 1
    N -= 1
print(-1)

Submission status: Wrong Answer

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

def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

if len(set(A))==1:
    if A[0] == 1:
        print(1)
        exit()
    print(-1)
    exit()

for i in range(1, N+1):
    try:
        if i!=1:
            x=my_index_multi(A, i)
            xx = list(filter(lambda x : x>xi, x))
            xi = min(xx)
            ans.append(xi)
        else:
            x = my_index_multi(A, i)
            xi = min(x)
            ans.append(xi)
    except ValueError:
        print(len(A) - len(ans))
        exit()

Submission status: Wrong Answer

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

i = 1
while i in lst:
    num = lst.index(i)
    del lst[i-1:num]
    i += 1

if i==1 and lst[0]!=1:
    print(-1)
elif N==int(len(lst)):
    print(0)
else:
    print(N-int(len(lst)))

Submission status: Wrong Answer

Solution code:
N = int(input())
a =  list(map(int,input().split()))
l = len(set(a))
b = list()
count= -1
def my_index(l, x, default=-1):
    if x in l:
        return l.index(x)
    else:
        return default
for i in range(1,N):
  now = my_index(a,i)
  if now != -1:
    count = i
    a = a[now + 1:]
    print(len(a))
  else:
    break
if count == -1:
  print(-1)
else:
  print(N-count)

Submission status: Wrong Answer

Solution code:
N =int(input())
I = [int(i) for i in input().split()]
Is=I #コピー

Is.sort()
#print(Is)

Imax=0
flg=0
for i in range(N-1):
    if (Is[i] < Is[i+1]):
        Imax +=1
        flg=1
    else:
        Imax=0

#Imaxになるまで続ける
cnt=0
temp=1
for i in range(N-1):
    if temp < I[i]:
        cnt+=1
    #elif temp == I[i]:
        #temp=I[i+1]
#print(cnt)

#判定
if N==1:
    print(0)
elif (flg==0)&(Imax==0):
    print(-1)
else:
    print(cnt)

'''
10
3 1 4 1 5 9 2 6 5 3

3
2 1 2

'''

Submission status: Wrong Answer

Solution code:
import copy

a = int(input())
b = input().split()
f = copy.copy(b)
l = []

b = list(map(int, b))

for i in range(1,a):

    if i in b:
        c = b.index(i)
        l.append(b[c])
        del b[0:c]
        

    else:
        break
c = (a - len(l))
if len(f) == len(b):
    print(0)
elif len(l) ==0:
    print('-1')
else:
    print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if not 1 in a:
    print(-1)

count = 1
smash = 0
for i in a:
    if i == count:
        count += 1

    else:
        smash += 1

print(smash)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
m = list(map(int, input().split()))
ans = 0
str =list(range(n+1))
str = str[2:n+1]


if not 1 in m:
    print(-1)

elif m == str:
    print(0)
    
else:
    for i in range(n):
    
        if i in m:
            ans += 1
            l = int(m.index(i))
            m = m[l:]

    print(n-ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [int(n) for n in input().split()]
c = 0
idx = {i:[] for i in range(1,N+1)}
for i,v in enumerate(a):
    idx[v].append(i)
print(idx)
flg = -1
l = 0
for j in range(1, N):
    if not idx[j]:
        print(flg)
        break
    else:
        for k in idx[j]:
            ok = False
            if l < k:
                l = k
                c += 1
                flg = N - c
                ok = True
                break
        if not ok:
            print(flg)
            break
print(flg)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N=int(input())
a=list(input())
c=0
k=0
if print("1" in a) == True:
    c=-1

for i in range(N):
    for j in range(N):
        if i == a[j]:
            if j>k:
                c+=1
                k=j
                break

print(c)

Submission status: Wrong Answer

Solution code:

N = int(input())
nums = input().split()

cnt=0
for i in range(1,N):
    for j in range(N):
        if int(nums[j])==i:
            cnt+=1
print(N-cnt)

Submission status: Wrong Answer

Solution code:

if __name__ == "__main__":
    N = int(input())
    a = input().split()
    hoge = 1
    piyo = 0
    for i in range(len(a)):
        if a == hoge:
            hoge += 1
        else:
            piyo += 1
    if piyo > N-1:
        print("-1")
    else:
        print(piyo)

Submission status: Wrong Answer

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

loopFlag = False
ans = 0
onetwo = 2
limitNum = 0
maxNum=-1
loopcount = 0

for i in range(N):
    if i + 1 in a:
        if i == limitNum:
            limitNum += 1

if limitNum == 0:
    print(-1)
elif limitNum == 1:
    print(N - 1)
else:
    count = 0
    ans = 1
    for i in range(limitNum):
        if maxNum == -1:
            before = [a.index(i+1)]
            #before = [l for l, x in enumerate(a) if x == i + 1]
        else:
            before = [maxNum]

        loopcount = a.count(i + 2)
        for j in range(len(a)):
            if a[j] == i + 2:
                if j > before[0]:
                    count += 1
                    if count == loopcount:
                        ans = i + 2
                        loopFlag = True
                        maxNum = a[j]
                        break
        if loopFlag == False:
            break
            
        







    print(N - ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = tuple(map(int, input().split()))
 
Y = X[::-1]
 
li = set(range(N))
 
for k in li:
    try:
        i = Y.index(k+1)
        Y = Y[:i]
    except:
        i = -1
        leng = k
        break
    leng = k + 1

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

Submission status: Wrong Answer

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

Ls = sorted(list(set(L)))
if len(Ls) == 1:
    pass
else:
    for i in range(len(Ls)):
        if Ls[i] != i + 1: 
            break
    Ls = Ls[0:(i)]

if (len(Ls) == 0):
    print("-1")
elif ((len(Ls) == 1) & (Ls[0] != 1)):
    print("-1")
else:
    out = []
    for i in Ls:
        try:
            idx = L.index(i)
            out.append(L[idx])
            L = L[idx:len(L)]
        except:
            break
    print(n - len(out))

Submission status: Wrong Answer

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

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


for i in range(N-1):
    if A[i] in l:
        continue
    if l[c]+1 != A[i]:
        continue
    for j in range(i+1,N):
        if A[i]>A[j]:
            if A[j] not in l:
                continue
        else:
            if A[i] in l:
                continue
            else:
                l.append(A[i])
                c+=1
if l[-1]+1 == A[-1]:
    l.append(A[-1])

print(N+1-len(l),l)

Submission status: Wrong Answer

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

def my_index_multi(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

for i in range(1, N+1):
    try:
        if i!=1:
            x=my_index_multi(A, i)
            xx = list(filter(lambda x : x>xi, x))
            xi = min(xx)
            ans.append(xi)
        else:
            x = my_index_multi(A, i)
            xi = min(x)
            ans.append(xi)
    except ValueError:
        print(len(A) - len(ans))
        exit()

Submission status: Wrong Answer

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

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

target = 1
block = 0
sum = 0
for i in range(n):
    block += 1
    if l[i] == target:
        sum += block - 1
        block = 0
        target += 1
    
if target == 1:
    print(-1)
else:
    print(sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
kazu=int(input())
mono=input().split()
n=0
cnt=1
prev=0
br=-1
for n in range(kazu):
  if mono[n]==str(cnt):
    if br==-1:
      br=0
    br=br+n-prev
    prev=n+1
    cnt=cnt+1
if br!=-1:
  if prev<kazu:
    br=br+kazu-prev+1
print(str(br))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
temp = 1
try:
    iti = a[::-1].index(temp)
    temp += 1
except:
    print(-1)
    exit()
try:
    iti = a[:0:-1].index(temp)
except:
    print(-1)
    exit()
while iti > 1:
    if temp == 2:
      iti = 0
    temp += 1
    try:
        iti = a[:iti:-1].index(temp)
    except ValueError:
        print(-1)
        exit()
print(N-temp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

N = int(input())
nums = input().split()

cnt=0
for i in range(N):
    for j in range(1,N):
        if int(nums[i])==j:
            cnt+=1
print(cnt)

Submission status: Wrong Answer

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

answer=-1
count=0

for i in range(max(a)):
    while i<=len(a)-1 and len(a)>=1 and a[i]!=i+1:
        if a[i]==i+1:
            break
        else:
            del a[i]
            count+=1
if len(a)==0:
    print(answer)
else:
    print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = list(map(int,input().split()))
index = a_list.index
#sum = 0

if N ==1 and 1 in a_list:
    print(0)

for m in range(1,N):
    #print(m)
    if m in a_list:
        k = index(m)
        #print(k)
        #sum += k -m + 1
        #print(sum)
        if k != 0:
            del a_list[m-1:k]
            #print(a_list)
        if m == len(a_list):
            print(N - m)
            break

    elif m == 1:
        print(-1)
        break

    else:
        #sum += len(a_list) - 1
        print(N - m)
        break

Submission status: Wrong Answer

Solution code:
kazu=int(input())
mono=input().split()
n=0
cnt=1
prev=0
br=-1
for n in range(kazu):
  if mono[n]==str(cnt):
    if br==-1:
      br=0
    br=br+n-prev
    prev=n+1
    cnt=cnt+1
if br!=-1:
  if prev<=kazu:
    br=br+kazu-prev+1
print(str(br))

Submission status: Wrong Answer

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

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

cnt = 1

for i in range(N-1):
  if a[i] == cnt:
    if cnt == K - 1:
      print(N-K)
      exit()
    else:
      cnt += 1

print(-1)

Submission status: Wrong Answer

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

if 1 not in a:
    count=-1
else:
    for i in range(1,N):
        if len(a) == 1:
            break
        while a[0]!=i:
            count+=1
            a.pop(0)
            if len(a) == 1:
                count+=1
                break
        if len(a) == 1:
            break
        a.pop(0)
print(count)

Submission status: Wrong Answer

Solution code:


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

a.insert(0,0)

i = 0
ans = 0

while True:
  if i == a[0]:
    i += 1
    
    if i == len(a) -1:
      break
  else:
    a = a[1:]
    ans += 1
    if len(a) == 0:
      ans = -1
      break

print(ans)

Submission status: Wrong Answer

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

for i in range(N):
    if a[-1] == len(a):
        print(i)
        exit()
    else:
        del a[0]

print(-1)

Submission status: Wrong Answer

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

if 1 not in a:
    count=-1
else:
    for i in range(1,N):
        if len(a) == 1:
            break
        while a[0]!=i:
            count+=1
            a.pop(0)
            if len(a) == 1:
                break
        if len(a) == 1:
            break
        a.pop(0)
print(count)

Submission status: Wrong Answer

Solution code:
import math


def inputIntList():
    return [int(s) for s in input().split()]


def inputInt():
    return int(input())


def main():
    N = inputInt()
    a = inputIntList()
    ans = 0
    look = 0
    current = 0
    while current < len(a) - 1:
        for i, j in enumerate(a[current:]):
            if j == look + 1:
                current += i
                ans += current - i
                break
        look += 1
        # print(look, current, ans)
    return ans


if __name__ == "__main__":
    print(main())

Submission status: Wrong Answer

Solution code:
kazu=int(input())
mono=input().split()
n=0
cnt=1
prev=0
br=-1
for n in range(kazu):
  if mono[n]==str(cnt):
    if br==-1:
      br=0
    br=br+n-prev
    prev=n+1
    cnt=cnt+1
if br!=-1:
  br=br+kazu-prev+1
print(str(br))

Submission status: Wrong Answer

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

cnt=0
while a!=num:
  if len(a)<=0:
    print(-1)
    exit()
  for i in range(len(a)):
    if a[i] is not num[i]:
      cnt+=1
      a.pop(i)
      num.pop(-1)
      break
#print(a)
print(cnt)

Submission status: Wrong Answer

Solution code:
import copy

a = int(input())
b = input().split()
f = copy.copy(b)
l = []

b = list(map(int, b))

for i in range(1,a):

    if i in b:
        c = b.index(i)
        l.append(b[c])
        del b[0:c]
        

    else:
        break
c = (a - len(l))
if len(f) == len(l):
    print(0)
elif len(l) ==0:
    print('-1')
else:
    print(c)

Submission status: Wrong Answer

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

if K == 1:
  if N == 1:
    print(1)
  else:
    print(N-1)
  exit()

cnt = 1

for i in range(N-1):
  if a[i] == cnt:
    if cnt == K - 1:
      print(N-K)
      exit()
    else:
      cnt += 1

print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
X = tuple(input().split())

Y = X[::-1]
Y = ''.join(Y)

li = set(range(N))
leng = 0
for k in li:
    i = Y.find(str(k+1))
    Y = Y[:i]
    if i == -1:
        break
    else:
        leng = k + 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = input().split()
k = 1
try:
 for _ in range(n):
  #print(a[k-1],"!=",k,"なので")
  if int(a[k-1]) != int(k):
   #print("0",a)
   #a.pop(k-1)
   #print("1",a)
   k = k -1
  k = k + 1
except:
 pass
res = n-len(a)
if res == n:
 print("-1")
else:
 print(n-len(a))

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:
# abc148_d.py
from sys import stdin
N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
#print(a)
cnt = 0
idx = 1
flg = 1
for i in a:
	if i == idx:
		idx = idx + 1
		flg = 0
	else:
		cnt = cnt + 1
		if idx != 1:
			flg = 1

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

Submission status: Wrong Answer

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

i = 1
while i in lst:
    num = lst.index(i)
    del lst[i-1:num]
    i += 1

#後はifでなんとかなるか？
if i==1 and lst[0]!=1:
    print(-1)
elif N==int(len(lst)):
    print(0)
else:
    print(N-int(len(lst)))

Submission status: Wrong Answer

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

cnt=0
while a!=num:
  if len(a)<=0:
    print(-1)
    exit()
  for i in range(len(a)):
    if a[i] is not num[i]:
      cnt+=1
      a.pop(i)
      num.pop(-1)
      break
print(a)
print(cnt)

Submission status: Wrong Answer

Solution code:
# abc148_d.py
from sys import stdin
N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
print(a)
cnt = 0
idx = 1
flg = 1
for i in a:
	if i == idx:
		idx = idx + 1
		flg = 0
	else:
		cnt = cnt + 1
		if idx != 1:
			flg = 1

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = list(map(int,input().split()))
index = a_list.index
sum = 0

if N ==1 and 1 in a_list:
    print(0)

for m in range(1,N):
    #print(m)
    if m in a_list:
        k = index(m)
        #print(k)
        sum += k -m + 1
        #print(sum)
        if k != 0:
            del a_list[m-1:k]
            #print(a_list)
        if m == len(a_list):
            print(sum)
            break

    elif m == 1:
        print(-1)
        break

    else:
        sum += len(a_list) - 1
        print(sum)
        break

Submission status: Wrong Answer

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

Ls = sorted(list(set(L)))
if len(Ls) == 1:
    pass
else:
    for i in range(len(Ls)):
        if Ls[i] != i + 1: 
            break
    Ls = Ls[0:(i)]

if (len(Ls) == 0):
    print("-1")
elif ((len(Ls) == 1) & (Ls[0] != 1)):
    print("-1")
else:
    out = [-1]
    for i in Ls:
        #idx = L.index(i)
        idxs = [j for j, x in enumerate(L) if x == i]
        idxs = [idxs[j] for j in range(len(idxs)) if idxs[j] > max(out)]
        if len(idxs) > 0:
            idx = min(idxs)
            out.append(idx)
            #L = L[idx:len(L)]

    out = out[1:len(out)]
    if (len(out) == 1) & (L[0] != 1):
        print("-1")
    else:
        print(n - len(out))

Submission status: Wrong Answer

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

y = False
d = 1
if a==list(range(1,len(a)+1)):
    print(c)
    exit(0)
p = -1
for i in range(len(a)):
    if d==a[i]:
        c+=i-p-1
        p = i
        d+=1
if 0==c:
    print(-1)
else:
    print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = 0
count = 0
sunuke = 0#すぬけさんが満足する場合1
for i in range(n):
    for j in range(b,len(a)):
        if a[j] == i+1:
            if a[j] != 1:  #見つけた要素より左側に1少ない数が無ければならない
                for k in range(0,j):
                    if a[k]+1 == a[j]:
                        count += 1
                        sunuke = 1
                        b = j
                        break
            elif a[j] == 1:
                count += 1
                sunuke = 1
                b = j
                break
        else:
            continue
        break

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

Submission status: Wrong Answer

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

for i in range(N-1):
  if a[i] == cnt:
    if cnt == K - 1:
      print(N-K)
      exit()
    else:
      cnt += 1

print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import deque


def main():
    N = int(input())
    a = [int(i) for i in input().split()]
    a = deque(a)
    b = deque()

    ans = -1
    flg = 0
    for i in range(N):
        if flg:
            break
        while len(a) > 0:
            if flg:
                break
            top_a = a.popleft()
            if top_a == i + 1:
                b.append(top_a)
                if len(a) == 0:
                    ans = N - len(b)
                    flg = 1
                    break
                else:
                    break

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
def check(a, ar):
    if a in ar:
        return True
    else:
        return False

def print_ans(N, ar):
    """Test Case
    >>> print_ans(3, [2, 1, 2])
    1
    >>> print_ans(3, [2, 2, 2])
    -1
    >>> print_ans(10, [3, 1, 4, 1, 5, 9, 2, 6, 5, 3])
    7
    >>> print_ans(1, [1]) 
    0
    """
    if not(check(1, ar)):
        print(-1)
    else:
        index = 0
        ans = 0
        counter = 1
        flg = True
        while flg:
            if check(counter, ar):
                index = ar.index(counter)
                ans += index
                ar = ar[index+1:]
                counter += 1
            else:
                break
        print(ans)




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

Submission status: Wrong Answer

Solution code:
N = int(input())
a_list = list(map(int,input().split()))
sum = 0

if N ==1 and 1 in a_list:
    print(0)

for m in range(1,N):
    #print(m)
    if m in a_list:
        k = a_list.index(m)
        #print(k)
        sum += k -m + 1
        #print(sum)
        if k != 0:
            del a_list[m-1:k]
            #print(a_list)
        if m == len(a_list):
            print(sum)
            break

    elif m == 1:
        print(-1)
        break

    else:
        sum += len(a_list) - 1
        print(sum)
        break

Submission status: Wrong Answer

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


ans = ([x for x in set(a) if a.count(x) > 0])

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

Submission status: Wrong Answer

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

for i in range(N):
    if split[i] == 1:
        count +=1
        break
if count != 0:
    for i in range(N):
        i -= count
        if i + 1 != split[i]:
            del split[i]
            count += 1
    print(count)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())

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

ind_list = []
num_list = []
for ind, num in enumerate(a_list):
    ind = ind + 1
    ind_list.append(ind)
    num_list.append(num)

#answer = len(set(ind_list) ^ set(num_list))
#print(num_list)

count = 0
for num, ind in zip(num_list, ind_list):
    if num > ind:
        num_list.pop(ind-1)
        num_list.insert(0,0)
        ind_list.pop(-1)
        ind_list.insert(0, 0)
        count += 1
#        print(num_list)
#        print(ind_list)


print(count if count < n else -1)

Submission status: Wrong Answer

Solution code:
n=int(input())
li = list(map(int,input().split()))
if li.count(1)==0:
    print(-1)
else:
    del li[0:li.index(1)]
    for k in range (2,n+1):
        if li.count(k)!=0:
            del li[k-1:li.index(k)]
    print(n-len(li))

Submission status: Wrong Answer

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

if K == 1:
  print(N-1)
  exit()

cnt = 1

for i in range(N-1):
  if a[i] == cnt:
    if cnt == K - 1:
      print(N-K)
      exit()
    else:
      cnt += 1

print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a = list(map(int, input().split()))
i = 0
crash_count = 0
while True:
  if i >= len(a)-2:
    break
  if a[i] >= a[i+1]:
    del a[i]
    crash_count+=1
    if (i>=1 and a[i] >= a[i-1]): i-=1
    continue
  else:
    i+=1
for offset,item in enumerate(a):
  if offset+1 != item:
    print('-1')
    exit()
print(str(crash_count))

Submission status: Wrong Answer

Solution code:
kazu=int(input())
mono=input().split()
n=0
cnt=1
prev=0
br=-1
for n in range(kazu):
  if mono[n]==str(cnt):
    if br==-1:
      br=0
    br=br+n-prev
    prev=n+1
    cnt=cnt+1
print(str(br))

Submission status: Wrong Answer

Solution code:
num = int(input())
input_data = input().split(' ')
if("1" in input_data):
    input_data[input_data.index('1'):]
    ref = 1
    for i in range(len(input_data)):
        if(int(input_data[i]) - ref == 1):
            ref += 1

    print(num - ref)
else:
    print(-1)