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(map(int, input().split()))
A = list(map(str, input().split()))

#　1の左側は砕く、以降数字が大きくなるほど右側にあればいいが、順番が違うと砕く必要あり。
from copy import deepcopy
#B =deepcopy(A)
B = "".join(A)
x = 0
y =0
z = []
for i in range(1,N+1):
    y = B.rfind(str(i) ) 
    if y ==-1:
        break
    else:
        z.append(y)
        x = y
        B_list =[i for i in B]
        del B_list[:x] # 文字を消すためにリスト
        B ="".join(B_list) # 再度リスト→文字（・・・悲しい作業）

#
renga_kudaku = 0
if len(z) ==0 and y==-1:
    renga_kudaku = -1
elif len(z) ==0:
    renga_kudaku = 0
else:
    renga_kudaku=sum(z)-len(z) +1 # 1の右側はリストの「０」あるので＋１
print(renga_kudaku)

Submission status: Wrong Answer

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

i = 1
j = 0

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

while j < N and i in set(a[j:]):
    j = a[j:].index(i) + j
    # print(i, j)
    i += 1

print(N - i + 1)

Submission status: Wrong Answer

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

i = 1
j = 0

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

while i in set(a[j:]) and j < N:
    j = a[j:].index(i) + j
    # print(i, j)
    i += 1

print(N - i + 1)

Submission status: Wrong Answer

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

i = 1
j = 0

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

while i in a[j:]:
    j = a[j:].index(i) + j
    # print(i, j)
    i += 1
    if j >= N:
        break

print(N - i + 1)

Submission status: Wrong Answer

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

i = 1
j = 0

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

while i in a[j:]:
    j = a[j:].index(i) + j
    # print(i, j)
    if j >= N:
        break
    i += 1

print(N - i + 1)

Submission status: Wrong Answer

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

i = 1
j = 0

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

while i in a[j:]:
    j = a[j:].index(i) + j
    print(i, j)
    i += 1
    if j >= N:
        break

Submission status: Wrong Answer

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

i = 1
j = 0

if not i in a[j:]:
    print(-1)
    
while i in a[j:]:
    j = a[j:].index(i) + j
    i += 1
    if j >= N:
        break
print(N - i + 1)

Submission status: Wrong Answer

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

i = 1
j = 0

while i in a[j:]:
    j = a[j:].index(i) + j
    i += 1
    if j >= N:
        break
print(N - i + 1)

Submission status: Wrong Answer

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

i = 1
j = 0

while i in a[j:] and j < N:
    j = a[j:].index(i) + j
    i += 1
print(N - i + 1)

Submission status: Wrong Answer

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

if 1 in a:
    b.append(1)
    for i in a:
        if i == b[-1] + 1:
            b.append(i)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
count = 0
i = 0
while i+1 != a[i]:
  a.pop(i)
  count += 1
  if i+1 == a[i]:
    i += 1
  if a == []:
    print(-1)
    exit()
  if len(a) == a[-1]:
    print(count)
    exit()

Submission status: Wrong Answer

Solution code:
import sys
n=int(input())
nums=input().rstrip().split(" ")
a=-1
if nums.count("1")==0:
    print("-1")
    sys.exit()
for i in range(1,n+1):
    j=a+1
    count=0
    while a<j<n:
        if int(nums[j])==i:
            a=j
            break
        elif int(nums[j])!=i:
            count+=1
            j=j+1
    if count==n-a:
        break
answer=n-i+1

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
arr = list(map(int, input().split()))
exp = 1
for num in arr:
  if exp == num:
    exp += 1
if exp == 1:
  print('-1')
else:
  print(N-exp-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
arr = list(map(int, input().split()))
exp = 1
for num in arr:
  if exp == num:
    exp += 1
if exp == 1:
  print('-1')
else:
  print(N-exp)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

if brickno == 1:
    print(-1)
elif brickno == N+1:
    print(N)
else:
    print(N+1-brickno)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N):
    if a[j] ==searchNum:
        searchNum += 1
        continue
    else:
        count +=1
print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N+1):
    if searchNum ==1:
        if not searchNum in a:
            searchNum = N +1
            break
    if not searchNum in a[index:N]:
        searchNum -= 1
        break
    else:
        index = a.index(searchNum)
        searchNum += 1
        continue

print(int(N -searchNum))

Submission status: Wrong Answer

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

print(int(N -searchNum))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N+1):
    if searchNum ==1:
        if not searchNum in a:
            searchNum = N +1
            break
    if not searchNum in a[index + j:N-1]:
        searchNum -= 1
        break
    if j + index < N:
        if a[index + j] == searchNum:
            index = index + j
            searchNum += 1
            continue
    else:
        break

print(int(N -searchNum))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N+1):
    if searchNum ==1:
        if not searchNum in a:
            searchNum = N +1
            break
    if not searchNum in a:
        searchNum -= 1
        break
    if j + index < N:
        if a[index + j] == searchNum:
            index = index + j
            searchNum += 1
            continue
    else:
        break

print(int(N -searchNum))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N):
    if searchNum ==1:
        if not searchNum in a:
            searchNum = N +1
            break
    if not j + 1 in a:
        break
    if j <= N:
        if a[index + j] == searchNum:
            index = index + j
            searchNum += 1
            continue
    else:
        break

print(int(N -searchNum))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N):
    if searchNum ==1:
        if not searchNum in a:
            searchNum = N +1
            break
    if not j + 1 in a:
        searchNum -= 1
        break
    if j <= N: 
        if a[index + j] == searchNum:
            index = index + j
            searchNum += 1
            continue
    else: 
        continue

print(int(N -searchNum))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = []
a = list(map(int,input().split()))
index = 0
count = 0
searchNum = 1
for j in range(N):
    if searchNum ==1:
        if not searchNum in a:
            searchNum = N +1
            break
    if not j + 1 in a:
        break
    if a[index + j] == searchNum:
        index = index + j
        searchNum += 1
        if searchNum == N+1:
            searchNum = N
        continue

print(int(N -searchNum))

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
count=0
b=max(a)
f=1
i=1
while len(a)>0:
  if i not in a:
    f=0
    break
  elif a[0]!=i :
    count+=1
    a=a[1:]
  elif a[0]==i:
    a=a[1:]
    i+=1
if f:
  print(count)
else:
  print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

tmp = [int(s) for s in input().split()]

A = {}
for i in range(n):
    A[i] = tmp[i]

def solve(A,n):
    keys = [k for k, v in A.items() if v == 1]
    
    if(len(keys) == 0):
        print(-1)
        return
    else:
        index = min(keys)
    
    #print(index)
    val = 2
    while(index <= n-1):
        #print(val)
        keys = [k for k, v in A.items() if v == val]
        if(len(keys) != 0):
            if(index < min(keys)):
                index = min(keys)
                val = A[index] + 1
            else:
                break
        else:
            val -= 1
            break
    #print(val,index)
    print(n-val)

solve(A,n)

Submission status: Wrong Answer

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

    ans = 0
    if 1 not in A:
        print(-1)
        return
    elif len(A) == 1:
        print(ans)
        return

    for i in range(1, N):
        if len(A) == 0:
            print(ans)
            return
        if i in A:
            ans += A.index(i)
            del A[: A.index(i) + 1]
        else:
            ans += len(A)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

def f(bs):
        print(bs)
        if bs == []:
            return 0
        elif bs[0] == 1:
            if len(bs) == 1:
                return 0
            else:
                return int(f([x-1 for x in bs][1:]))
        else:
            return int(1 + f([x for x in bs][1:]))

if 1 not in bs:
    print(-1)
else:
    print(f(bs))

Submission status: Wrong Answer

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

if 1 not in bs:
    print(-1)
else:
    def f(bs):
        print(bs)
        if bs[0] == 1:
            if len(bs) == 1:
                return 0
            else:
                return f([x-1 for x in bs][1:])
        else:
            return 1 + f([x for x in bs][1:])

    print(f(bs))

Submission status: Wrong Answer

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


def solve(N: int, a: "List[int]"):
    if 1 not in a:
        print(-1)
        return
    ans = 0
    a = a[::-1]
    for i in range(1, N):
        try:
            idx = a.index(i)
            st = len(a) - 1 - idx
            a = a[:idx+1]
            ans += st if i == 1 else st - 1
        except Exception:
            ans += len(a) - 1
            break
    print(ans)
    return


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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split(' ')))
pos = []
for i in range(1, N + 1):
    if len(pos) == 0 and i in a:
        pos.append(a.index(i))
    elif len(pos) > 0 and i in a[i-1:]:
        pos.append(a[i-1:].index(i) + i - 1)
    else:
        break
if len(pos) > 0:
    print(N - len(pos))
else:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
i = 0
j = 1
ans = 0
while i < N:
    try:
        k = a.index(j,i)
    except:
        if i == N-1:
            print(ans)
        else:
            print(-1)
        exit(0)
    if i == 0:
        ans += k-i
    elif k != i+1:
        ans += k-i-1
    i = k
    j += 1

Submission status: Wrong Answer

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

no_break = 1
point = a[-1]
for i in range(len(a) - 1, -1, -1):
    if a[i] == point - 1:
        point = a[i]
        no_break += 1
        if point == 1:
            break

if not point == 1:
    print('-1')
else:
    print(len(a) - no_break)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
arr = list(map(int, input().split()))
ans = 0
target = 1
tmp_cnt = 0
for x in arr:
    if x == target:
        target += 1
        ans += tmp_cnt
        tmp_cnt = 0
    else:
        tmp_cnt += 1

if target == 1 and ans == 0:
    ans = -1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
if n==1:
  if a[0]==1:
    print(0)
  else: print(-1)
if n>=2:
  if 1 not in a:
    print(-1)
    exit()
  else:
    for i in range(2,n+1):
      if i in a and a.index(i-1)<a.index(i):
        continue
      else:
        print(n-i)
        exit()

Submission status: Wrong Answer

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

B = set(A)
st = 0
num = 1
count = 0

def destroy(st,num,nlist,count):
  add = 0
  tmp = count
  for n in range(st,N):
    if nlist[n] == num:
      add += 1
      break
    elif nlist[n] != num and n == N-1 :
      add += 1 
      count = count
    else:
      add += 1 
      count += 1
   
  st += add
  return count,st

for i in range(1,N-1):
  if st >= N:
    break
  else:
    count,st = destroy(st,i,A,count)
    
    
  
if count == 0:
  print(-1)
else :
  print(count)

Submission status: Wrong Answer

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

B = set(A)
st = 0
num = 1
count = 0

def destroy(st,num,nlist,count):
  add = 0
  for n in range(st,N):
    if nlist[n] == num:
      add += 1
      break
    else :
      add += 1 
      count += 1
  st += add
  return count,st

for i in range(1,N-1):
  if st >= N:
    break
  else:
    count,st = destroy(st,i,A,count)
  
if count == 0:
  print(-1)
else :
  print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
count = 1
broke = 0
suceed = False
while True:
    if count in a:
        suceed = True
        broke += a.index(count)
        a = a[a.index(count) + 1:]
        count += 1
    else:
        break
if suceed:
    print(broke)
else:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

i = 1
bb = 0
ANS = 1
while True:
    if (i == 1) and (nums == l):
        ANS = 0
        break
    if i in nums:
        i_index = nums.index(i)
        bb += len(nums[:i_index])
        nums = nums[i_index + 1:]
        i += 1
    else:
        break

if (bb > 0) or ((ANS == 0)):
    print(bb)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

i = 1
bb = 0
same = 0
while True:
    if i in nums:
        i_index = nums.index(i)
        if i_index == i - 1:
            same += 1
        bb += len(nums[i - 1:i_index])
        nums = nums[:i - 1] + nums[i_index:]
        i += 1
    else:
        break

if (bb > 0) or ((bb == 0) and (same == n)):
    print(bb)
else:
    print(-1)

Submission status: Wrong Answer

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

l = [x for x in a_list if x == -1]
print(len(l))

Submission status: Wrong Answer

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

from collections import deque

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

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

Submission status: Wrong Answer

Solution code:

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

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

Submission status: Wrong Answer

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

print(cnt)

Submission status: Wrong Answer

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

print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

i=0
index=0

if 1 in a:
    while len(a)>0 and index<N-1:
        if i+1 in a[index:]:
            #print(a[index:])
            index=a[index:].index(i+1)
            i+=1
            #print(index)
        else:
            break
    #print(i)
    print(N-i)
else:
    print(-1)

Submission status: Wrong Answer

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

i=0
count=0

if 1 in a:
    while len(a)>0:
        if i+1 in a[i::]:
            i+=1

        else:
            break
    #print(i)

    print(N-i)
else:
    print(-1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


def mach(N, nums):
    is_True = False
    K = 0
    # 1,2,3...を順に探していく
    for n in range(1, N + 1):
        n = int(n)
        # リストを回す
        for idx in range(len(nums)):
            if n == nums[idx]:
                K += 1
                nums = nums[idx:]
                is_True = True
                break
        if is_True:
            continue
        else:
            return - 1

    return N - K


print(mach(N, nums))

Submission status: Wrong Answer

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

ans = 0

for i in range(1,N) :
    if not i in list_A :
        if i == 1 :
            print(-1)
            exit()
        else :
            print(ans)
            exit()

    num = list_A.index(i)
    if i == 1 :
        ans += num
    else :
        ans += num - 1
    list_A = list_A[num:]

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    if n%2==0:
        print(0)
        return 
    
    ans = 0
    bunbo = 10
    while bunbo <= n:
        ans += n // bunbo
        bunbo *= 5
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

count = 0
num = 1
for i in range(0,n):
  if block[i] == num:
    num += 1
  else:
    count += 1
if count == 0 and num != 2:
  print(-1)
else:
  print(count)

Submission status: Wrong Answer

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

count = 0
num = 1
for i in range(0,n):
  if block[i] == num:
    num += 1
  else:
    count += 1
if count == 0:
  print(-1)
else:
  print(count)

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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


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

Submission status: Wrong Answer

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

count = 0
num = 1

for i in range(len(n)):
  if n[i] == num:
    num += 1
  else:
    count += 1

if count == 0:
  count = -1

print(count)

Submission status: Wrong Answer

Solution code:
t = int(input())
n = [ int(i) for i in input().split(" ") ]
temp_n = n
ans = 0
cur = 0

for i in range(0, len(n)-1):
  if i == 0:
    if n[0] == 1:
      cur = 1
    else:
      ans += 1
  else:
    if cur+1 == n[i]:
      cur += 1
    else:
      ans += 1

if ans == len(n)-1 and n[-1] != 1:
  print(str(-1))
else:
  print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

i = 0
count = 0
while True:
    if i == N-1 or a == []:
        break
    if a[i] != i+1:
        del a[i]
        N -= 1
        count += 1
    else:
        i += 1

b = len(a) + 1

if a != [] and a == list(range(1, b)):
    print(count)
else:
    print(-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
an = list(map(int, input().split()))
num = 1
listnum = len(an)
for a in an:
    if a == num:
        an.remove(a)
        num += 1
if listnum == len(an):
    print(-1)
else:
    print(len(an))

Submission status: Wrong Answer

Solution code:
N = int(input())

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

count = 0
while count <= N:
    for i in range(N):
        try:
            if line[i] == i + 1:
                True
            else:
                line.pop(i)
                break
        except:
            break
    count += 1
    
print(N - len(line))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(n):
    if A[i-1] != x:
        ans += 1
    elif A[i-1] == x:
        x += 1

if n == 1 and A[0] == 1 or A == range(1, n+1):
    print("0")
elif x == 1:
    print("-1")
else:
    print(ans-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# 沼にはまった

Submission status: Wrong Answer

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

start = 1

for num in nums:
    if num != start:
        continue
    start += 1

if start == 1:
    print(-1)
print(n-start+1)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
d = 1
c = 0

for i in a:
    if d == i:
        d += 1
    else:
        c += 1
print(c)

Submission status: Wrong Answer

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

if min != 1:
  result = -1
else:
  result = N - count
print(result)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a_list = list(map(int, input().split()))

search_num = 1
count = 0

for i in range(n):
    if a_list[i] == search_num:
        search_num = search_num + 1
        count = count + 1

print(n-count)

Submission status: Wrong Answer

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

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

def is_big(l, x):
    temp = [y for y in l if y > x]
    if temp:
        return False
    else:
        return True

place = []
isfail = False
if 1 not in a:
    print("-1")
    sys.exit()
if n == 1 and a[0] == 1:
    place.append(1)
elif n == 1 and a[0] != 1:
    print("-1")
    sys.exit()

for x in range(1, n):
    try:
        where = printindex(a, x)
        if is_big(place, where):
            place.append(where)
        else:
            break
    except:
        break
    

if isfail:
    print("-1")
else:
    print(n - len(place))

Submission status: Wrong Answer

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

place = []
isfail = False
if 1 not in a:
    print("-1")
    sys.exit()
if n == 1 and a[0] == 1:
    place.append(1)
elif n == 1 and a[0] != 1:
    print("-1")
    sys.exit()

for x in range(1, n):
    try:
        if x in a:
            place.append(a.index(x))
            a = a[a.index(x):]
        else:
            break
    except:
        isfail = True
        break

if isfail:
    print("-1")
else:
    print(n - len(place))

Submission status: Wrong Answer

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