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: Runtime Error

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

for i in range(1, N+1):
    if i not in A:
        j = i-1
        break
    k = A.index(i)
    A = A[k+1:]

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

Submission status: Runtime Error

Solution code:
N = int(Input[0])
a = list(map(int, Input[1].split()))

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

if target == 1:
    print(-1)
else:
    print(N-target+1)

Submission status: Runtime Error

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

ni = 1
remains = 0

for r in renga:
    if ni == r:
        ni += 1
        remains += 1
if ni == 1:
    print(-1)
else:
    print(renga - remains)

Submission status: Runtime Error

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

idx = 0
ans = 0
while 1:
  if idx >= len(A):
    exit()
  elif A[idx] == idx+1:
    idx += 1
  else:
    A.pop(idx)
    ans += 1

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

Submission status: Runtime Error

Solution code:

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

t = 1
for x in a:
    if x == t:
        t += 1
t -= 1

if t == 0:
    print(-1)
else:
    print(M-t)

Submission status: Runtime Error

Solution code:
class Stack:
  def __init__(self):
    self.count = 1
    self.lst = []
  
  def push(self, element):
    if element == count:
      self.lst.append(element)
      self.count += 1
  
  def length(self):
    return len(self.lst)

n = int(input())
a = list(map(int, input().split()))
stack = Stack()
for i in a:
  stack.push(i)

b = stack.length()
if b == 0:
  print('-1')
else:
  print(n - b)

Submission status: Runtime Error

Solution code:
import math
A, B = map(int, input().split())
print(int(A * B / math.gcd(A, B)))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def f(n):
  if(n<3):
    return n 
  return n*f(n-2)

N=int(input())
count0=0
ans=f(N)
ans=str(ans)
if(ans[-1]=="0"):
  count0+=1
  ans=ans[:-1]

print(count0)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
An = list(map(int, input().split()))
done = 0
for i in An:
    if done+1 == i:
        done = i
    print(done,An,i)

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
broken = 0
flag = [0, 0]
for i in range(1,N + 1):
  if flag[1] != 0:
    break
  for j in len(A):
    if A[j] == i:
      if len(A) != 1:
      	A = A[j + 1:]
      else:
        A = []
        flag[0] += 1
      break
    else:
      broken += 1
    if len(A) == i + 1:
      flag[1] += 1
if broken == 0 and flag[0] != 1:
  print(-1)
else:
  print(broken)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
c = -1
k = 0
for i in range(1,n+1):
  if not i in a_list:
    k = i
    break
  a_list = a_list[c+1:]
  c = a_list.index(i)
if k == 1:
  print(-1)  
elif k > 0:
  print(n-k+1)
else:
  print(0)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def solve(l, K=0):
  if K+1 in l:
    pos = l.index(K+1)
    return solve(l[pos:],K+1)
  else:
    return K
  
def judge(x):
  if x == 0:
    return -1
  else:
    return N-x


print(judge(solve(a)))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat May  9 16:33:03 2020

@author: shinba
"""


n = int(input())
a = list(map(input().split()))
cnt = 0
st = 1
for i in range(n):
    if a[i] == st:
        st += 1
    else:
        cnt += 1

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

Submission status: Runtime Error

Solution code:
N = int(input())
ls = list(map(int, input().split())
counter = 1
for elem in ls:
    if elem == counter:
        counter += 1
print(N - counter)

Submission status: Runtime Error

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

if asort[0] != 1:
  print(-1)
  exit()

for i in range(N - 1):
  if asort[i] == asort[i + 1]:
    pass
  elif asort[i] + 1 == asort[i + 1]:
    pass
  else:
    print(-1)
    break

for j in range(N):
  for k in range(len(a)):
    if a[j] != j + 1:
      del a[j]
      cnt += 1
    else:
      break
      
print(cnt)

Submission status: Runtime Error

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

if n == 1:
    if a[0] == 1:
        print(0)
    else:
        print(-1)
else:
    for i in range(1, n):
        if i not in a:
            break

        a = a[idx[i-1]: n]
        idx.append(a.index(i))

        k += 1

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

Submission status: Runtime Error

Solution code:
     if i_index >= N-1:
            break
        #print(brick_destroy)
        #print(i,end=" ")
        A_now = A[i_index :] #見つけた字の右側のみ見る

brick_destroy = N - len(brick_remain)
if brick_remain==[]:
    print(-1)
else:
    print(brick_destroy)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def func(idx,lst):
    if len(lst) == 0:
        return 0
    elif lst[0] == idx:
        return func(idx+1,lst[1:])
    else:
        return 1 + func(idx,lst[1:])

out = func(1,As)
if out == len(As):
    print(-1)
else:
    print(out)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
x = int(input())
li = list(map(int, input()))
l = []
for i in range(len(li)):
    l.append(i+1-li[i])
print(min(l))

Submission status: Runtime Error

Solution code:
x = int(input())
li = list(map(int, input()))
from collections import deque
l = deque(li)

def m(p, lis):
    for i in range(len(lis)):
        if lis.leftpop() == p:
            return i+1
    return -1

print(m(x, l))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

"""
def found(a,n):
    if(n==1 and n in a):
        return True
    elif(n==1 and n not in a):
        return False
    elif(n>len(a)):
        return False
    for i in range(len(a)-1,-1,-1):
        if(a[i]==n):
            return found(a[0:i],n-1)
    return False
            
            
            
n=int(input())
a=list(map(int,input().split()))
ans=-1
for i in range(n-1,-1,-1):
    x=a[i]
    if(found(a[0:i],x-1)):
        ans=max(ans,x)
        
if(n==1 and a==[1]):
    ans=1
if(ans!=-1):
    print(n-ans)
else:
    print(-1)

Submission status: Runtime Error

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

"""
def found(a,n):
    if(n==1 and n in a):
        return True
    elif(n==1 and n not in a):
        return False
    for i in range(len(a)-1,-1,-1):
        if(a[i]==n):
            return found(a[0:i],n-1)
    return False
            
            
            
n=int(input())
a=list(map(int,input().split()))
ans=-1
for i in range(n-1,-1,-1):
    x=a[i]
    if(found(a[0:i],x-1)):
        ans=max(ans,x)
        
if(n==1 and a==[1]):
    ans=1
if(ans!=-1):
    print(n-ans)
else:
    print(-1)

Submission status: Runtime Error

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

def なんか(a,n):
    if n==len(a)+1:
        return len(a)
    if a[n-1]==n:
        return なんか(a, n+1)
    else:
        a.pop(n-1)
        return なんか(a, n)

tmp=len(a)-なんか(a,1)
if len(a)==tmp:
    print(-1)
else:
    print(tmp)

Submission status: Runtime Error

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

def なんか(a,n):
    if n==len(a)+1:
        return len(a)
    if a[n-1]==n:
        return なんか(a, n+1)
    else:
        a.pop(n-1)
        return なんか(a, n)

tmp=len()-なんか(a,1)
if len(a)==tmp:
    print(-1)
else:
    print(tmp)

Submission status: Runtime Error

Solution code:
n = int(input())
R = list(map(int, input().split()))
c, i = 0, 1
for r in R:
  if r==i: i+=1
  else: c+=1
print(c if c<N else '-1')

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())
def fcd(a , b):
    if a>= b:
        while b != 0:
            a,b = b,a%b
        return a
    
    else:
        while a != 0:
            b,a = a,b%a
        return b
    
    
def lcm(a,b):
    return a*b//fcd(a , b)


print(lcm(a,b))

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]
j = 0
k = 0

if 1 not in a:
    print(-1)
else:   
    for i in range(1,n+1):
        if i in a[k:]:
            j = j + 1
            k = a[k:].index(i) + k
        else:
            break
    print(n-j)

Submission status: Runtime Error

Solution code:
n = int(input())
i = [int(input()) for i in range(n)]
j = 0
k = 0

if 1 not in a:
    print(-1)
else:   
    for i in range(1,n+1):
        if i in a[k:]:
            j = j + 1
            k = a[k:].index(i) + k
        else:
            break
    print(n-j)

Submission status: Runtime Error

Solution code:
#python3
cnt = 0
s = 1
def main():
    n = int(input())
    a = [int(i) for i in input().split()]

    def solve(ar):
        global cnt
        global s
        idx = 0
        f = False
        for i in range(len(ar)):
            if s == ar[i]:
                idx = i
                cnt += 1
                f = True
                break
        if (not f) or (idx>n):
            return 
        s += 1
        solve(ar[idx+1:])
        return cnt

    p = solve(a)
    print(n-p) if p else print(-1)
main()

Submission status: Runtime Error

Solution code:
#python3
cnt = 0
s = 1
def main():
    n = int(input())
    a = [int(i) for i in input().split()]

    def solve(ar):
        global cnt
        global s
        idx = 0
        f = False
        for i in range(len(ar)):
            if s == ar[i]:
                idx = i
                cnt += 1
                f = True
                break
        if (not f) or (idx>n):
            return 
        s += 1
        solve(ar[idx+1:])
        return cnt

    p = solve(a)
    print(n-p) if p else print(-1)
main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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


for i in range(n+1):
    #k=a.index(i+1)
    #print(k,i)
    try:
        k=a.index(i+1)
        a=a[k+1:]
        #print(a)
    except:
        if i==0:
            print(-1) 
            break
        else:
            print(n-i)
            break

Submission status: Runtime Error

Solution code:
def main():
	N = int(input())
	a = list(map(int, input().split()))
	if 1 not in a:
		print(-1)
	else:
		ans = solve(1,a,0)
		print(ans)


def solve(n,a,b):
	if n in a:
		idx = a.index(n)
		b += idx
		return solve(n+1, a[idx+1:],b)
	else:
		return b


if __name__ == '__main__':
	main()

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split(' ')))
broken = []
for i in range(1, N + 1):
    if i in a:
        for j in range(i - 1, a.index(i)):
            broken.append(a.pop(j))
    else:
        print(len(broken))
        exit(0)
print(len(broken))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import fractions
a,b = map(int,input().split())

def lcm(x,y):
    return (x*y) // fractions.gcd(x,y)

print(lcm(a,b))

Submission status: Runtime Error

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

def ccc(lists, a=1):
    if a not in lists:
        return a - 1
    for i, j in enumerate(lists):
        if j == a:
            return ccc(lists[i:], a + 1)

print(len(a) - ccc(a) if ccc(a) != 0 else -1)

Submission status: Runtime Error

Solution code:
def main():
	N = int(input())
	a = list(map(int, input().split()))
	if 1 not in a:
		print(-1)
		exit
	ans = test(1,a,0)
	print(ans)


def test(n,a,b):
	if n in a:
		idx = a.index(n)
		b += idx
		return test(n+1, a[idx+1:],b)
	else:
		return b


if __name__ == '__main__':
	main()

Submission status: Runtime Error

Solution code:
from collections import deque
n = int(input())
a = deque([int(i) for i in input().split()])
count = 1
while a:
    try:
        index = a.index(count)
        count += 1
        a = a[index:]
    except ValueError:
        if count == 1:
            ans = -1
        else:
            ans = n - (count - 1)
        a = []
print(ans)

Submission status: Runtime Error

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

r = 0
index = -1
ans = 0
for i in range(n) :
  index += 1
  r += 1
  if index >= n :
    break
  for j in range(n) :
    if a[index] == r :
      break
    ans += 1
    index += 1
      
if ans == n :
  print(-1)
else :
  print(ans)

Submission status: Runtime Error

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

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

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

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

Submission status: Runtime Error

Solution code:
from numpy import *

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

count = 0

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

ans = N - count

if count == 0:
    ans = -1

print(ans)

Submission status: Runtime Error

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

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

b = len(a) + 1

if a == list(range(1, b)):
    print(count)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
N =int(input())
a =map(int,input())
count = 1
ans = 0
for i in a:
  if i == count:
    count += 1
    ans += 1
if ans == 0:
  print(-1)
else:
  print(min(N - 1, N - ans))

Submission status: Runtime Error

Solution code:
N =int(input())
a =int(input())
count = 1
ans = 0
for i in a:
  if i == count:
    count += 1
    ans += 1
if ans == 0:
  print(-1)
else:
  print(min(N - 1, N - ans))

Submission status: Runtime Error

Solution code:
def snk_check(lst):
    flag = 0
    if len(lst) == 1:
        return lst

    for i in range(len(lst) - 1):
        if lst[i] + 1 != lst[i+1]:
            del lst[i+1]
            flag = 1
            break
    if flag == 1:
        ans = lst[i:]  # breakした = 不合格、再検査が必要
    else:
        ans = lst[-1]  # breakせずに完走した = 合格,末尾の数値がリストの長さと一致するので、それを返す
    return ans


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


def cut_before_first_one(lst):
    place = my_index(lst, 1)
    if place is not None:
        return lst[place:]
    else:
        return None


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

    cut_list = cut_before_first_one(An)
    if cut_list is not None:
        snk_safe_list = []
        while len(snk_safe_list) != 1:
            snk_safe_list = snk_check(cut_list)
        print(len(An) - len(snk_safe_list))
    else:
        print(-1)

main()

Submission status: Runtime Error

Solution code:
# coding:utf-8

import sys
import math
import time
import numpy as np
import collections
from collections import deque
import queue
import copy

# 最大公約数
def gcd(m,n):
  x = max(m,n)
  y = min(m,n)
  if x%y == 0:
    return y
  else:
    while x%y != 0:
      z = x%y
      x = y
      y = z
    else:
      return z

# 幅優先探索(BFS)でスタート座標(x, y)からの最大距離返す
def bfs(SS, x, y, H, W):#入力(座標系(迷路), x, y, xの端, yの端)
  x_next = [0, 0, -1, 1]
  y_next = [1, -1, 0, 0]
  tempx = x
  tempy = y
  mlength = 0
  q = deque()
  xytuple = (x, y, 0)
  q.append(xytuple)


  while q:
    xytuple = q.popleft()
    SS[xytuple[0]][xytuple[1]] = "x" #探索した場所は探索できないようにする
    for i in range(4): #このforで上下左右を調べてる
      tempx = xytuple[0] + x_next[i]
      tempy = xytuple[1] + y_next[i]
      #print(tempx, tempy, xytuple[2])
      #print(S)
      if(0 <= tempx and tempx < H and 0 <= tempy and tempy < W and SS[tempx][tempy] == "."):
        temptuple = (tempx, tempy, xytuple[2]+1)
        SS[temptuple[0]][temptuple[1]] = "x" #探索した場所は探索できないようにする
        mlength = max(mlength, xytuple[2] +1)
        q.append(temptuple)
      #print(q)
  return mlength

def brick(a,n,m,N):#ABC 148
  #n番目以降にmがあるか調べる
  if not m in a[n:]:
    print(N-(m-1))
  else:
    #b = copy.deepcopy(a)
    brick(a,a[n:].index(m),m+1,N)

#X = str(input()).split()
N = int(input())
A = str(input()).split()
a = []

for i in range(N):
  a.append(int(A[i]))

if not 1 in a:
  print(-1)
else:
  #b = copy.deepcopy(a)
  brick(a,a.index(1),2,N)

Submission status: Runtime Error

Solution code:
def snk_check(lst):
    flag = 0
    if len(lst) == 1:
        return lst

    for i in range(len(lst) - 1):
        if lst[i] + 1 != lst[i+1]:
            del lst[i+1]
            flag = 1
            break
    if flag == 1:
        ans = lst[:i] + snk_check(lst[i:])
    else:
        return lst
    return ans


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


def cut_before_first_one(lst):
    place = my_index(lst, 1)
    if place is not None:
        return lst[place:]
    else:
        return None


def main():
    N = int(input())
    An = list(map(int, input().split()))
    cut_list = cut_before_first_one(An)
    if cut_list is not None:
        snk_safe_list = snk_check(cut_list)
        print(len(An) - len(snk_safe_list))
    else:
        print(-1)


main()

Submission status: Runtime Error

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

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

	print(n - cnt + 1)

Submission status: Runtime Error

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

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

print(n - cnt + 1)

Submission status: Runtime Error

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

for i in range(N):
  if a[i] == number:
    brick.append(number)
    number += 1
print(N - max(brick))

Submission status: Runtime Error

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

for i in range(N):
  if a[i] == number:
	brick.append(number)
    number += 1
print(N - max(brick))

Submission status: Runtime Error

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

for i in range(N):
  if a[i] == number:
    for j in range(i):
      count += 1
      a.pop[number - 1]
    number += 1
print(count)

Submission status: Runtime Error

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

min_breaks = -1

count_num = 1
for i in range(N):
  check_num = count - nums[i]
  if nums[i] == count_num:
      if min_breaks == -1:
          min_breaks = 0
      min_break += i - count_num
      
if min_break < N:    
    print(min_break)
else:
    print(-1)

Submission status: Runtime Error

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

#消去順制御用
k = -1
#破壊数カウント
ans = 0

#数字の順番制御
for j in range(n):
    k += 1
    
    try:
        x = a[k]
    except(IndexError):
        break
        
#k+1見つかるまで消去
    for i in range(n-ans):
        if int(a[k]) == k+1:
            break
        else:
            a.remove(a[k])
            ans += 1
            
if ans != n:
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

if not 1 in an:
  print(-1)

count = 1
for i in an:
  if i == an[i]:
    count += 0
  elif i != an[i]:
    count += 1
print(count)

Submission status: Runtime Error

Solution code:

class Solution:

    def solve(self, n: int, a: List[int]) -> int:

        cursor = 0

        for an in a:
            if an == cursor + 1:
                cursor += 1

        if cursor == 0:
            return -1
        else:
            return len(a) - cursor


if __name__ == '__main__':

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

    # solve
    solution = Solution()
    print(solution.solve(n, a))

Submission status: Runtime Error

Solution code:
from typing import List


class Solution:

    def solve(self, n: int, a: List[int]) -> int:

        cursor = 0

        for an in a:
            if an == cursor + 1:
                cursor += 1

        if cursor == 0:
            return -1
        else:
            return len(a) - cursor


if __name__ == '__main__':

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

    # solve
    solution = Solution()
    print(solution.solve(n, a))

Submission status: Runtime Error

Solution code:
from typing import List


class Solution:

    def solve(self, n: int, a: List[int]) -> int:

        cursor = 0

        for idx, an in enumerate(a):
            if an == cursor + 1:
                cursor += 1

        if cursor == 0:
            return -1
        else:
            return len(a) - cursor


if __name__ == '__main__':

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

    # solve
    solution = Solution()
    print(solution.solve(n, a))

Submission status: Runtime Error

Solution code:
class Solution:

    def solve(self, n: int, a: List[int]) -> int:

        cursor = 0

        for idx, an in enumerate(a):
            if an == cursor + 1:
                cursor += 1

        if cursor == 0:
            return -1
        else:
            return len(a) - cursor


if __name__ == '__main__':

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

    # solve
    solution = Solution()
    print(solution.solve(n, a))

Submission status: Runtime Error

Solution code:
from sys import stdin
import fractions

N = stdin.readline().rstrip().split()
A = [int(x) for x in stdin.readline().rstrip().split()]
# print(N)
# print(A)
nex = 1
ans = 0

for i in range(N):
    if A[i] == nex:
        nex += 1
    else:
        ans += 1

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

Submission status: Runtime Error

Solution code:
import sys
list = input().split()
dic = {}
res = {}
for i in list:
    dic[str(i)] = list.index(i) + 1
for i in range(len(list)):
    k = 0
    j = i + 1
    if str(j) in dic and dic[str(j)] > k:
        res[str(j)] =  dic[str(j)]
        k = dic[str(j)]
print(len(res)