Problem:


Score : 400 points

Problem Statement

We have N bricks arranged in a row from left to right.

The i-th brick from the left (1 \leq i \leq N) has an integer a_i written on it.

Among them, you can break at most N-1 bricks of your choice.

Let us say there are K bricks remaining. Snuke will be satisfied if, for each integer i (1 \leq i \leq K), the i-th of those brick from the left has the integer i written on it.

Find the minimum number of bricks you need to break to satisfy Snuke's desire. If his desire is unsatisfiable, print -1 instead.

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of bricks that need to be broken to satisfy Snuke's desire, or print -1 if his desire is unsatisfiable.

Sample Input 1

3
2 1 2

Sample Output 1

1

If we break the leftmost brick, the remaining bricks have integers 1 and 2 written on them from left to right, in which case Snuke will be satisfied.

Sample Input 2

3
2 2 2

Sample Output 2

-1

In this case, there is no way to break some of the bricks to satisfy Snuke's desire.

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

There may be no need to break the bricks at all.



Sample input:
3
2 1 2


Sample output:
1

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)

input = sys.stdin.readline

setrecursionlimit(1000000)


def main():
	n = int(input())
	a = list(map(int,input().split()))
	if(1 not in a):
		print(-1)
	c = 1
	for i in a:
		if(i==c):
			c+=1
	print(n-c+1)

def factorization(n):
	if n<2:
		return []
	arr = []
	temp = n
	for i in range(2, int(-(-n**0.5//1))+1):
		if temp%i==0:
			cnt=0
			while temp%i==0:
				cnt+=1
				temp //= i
			arr.append([i, cnt])

	if temp!=1:
		arr.append([temp, 1])

	if arr==[]:
		arr.append([n, 1])

	return arr

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

def zip(a):
	mae = a[0]
	ziparray = [mae]
	for i in range(1,len(a)):
		if(mae != a[i]):
			ziparray.append(a[i])
			mae = a[i]
	return ziparray

def is_prime(n):
	if n < 2: return False

	for k in range(2, int(math.sqrt(n)) + 1):
		if n % k == 0:
			return False

	return True

def list_replace(n,f,t):
	return [t if i==f else i for i in n]

def base_10_to_n(X, n):
	X_dumy = X
	out = ''
	while X_dumy>0:
		out = str(X_dumy%n)+out
		X_dumy = int(X_dumy/n)
	if(out == ''): return '0'
	return out

def gcd(numbers):
	return reduce(math.gcd, numbers)

def lcm(numbers):
    return reduce(lcm_base, numbers, 1)

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

class Queue():
	def __init__(self):
		self.q = deque([])
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.popleft()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class Stack():
	def __init__(self):
		self.q = []
	def push(self,i):
		self.q.append(i)
	def pop(self):
		return self.q.pop()
	def size(self):
		return len(self.q)
	def debug(self):
		return self.q

class graph():
	def __init__(self):
		self.graph = defaultdict(list)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
		self.graph[t].append(f)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
		self.graph[t].remove(f)
	def linked(self,f):
		return self.graph[f]

class dgraph():
	def __init__(self):
		self.graph = defaultdict(set)
	def addnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].append(t)
	def rmnode(self,l):
		f,t = l[0],l[1]
		self.graph[f].remove(t)
	def linked(self,f):
		return self.graph[f]

class PriorityQueue():
	def __init__(self):
		self.queue = []
	def push(self,i):
		heapq.heappush(self.queue,-i)
	def pop(self):
		return -1 * heapq.heappop(self.queue)
	def sum(self):
		return -sum(self.queue)

main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

n = int(input())
lis = list(map(int,input().split()))
ans = 0
cnt = 1
for num in lis:
    if num == cnt:
        ans += 1
        cnt += 1
if ans == 0:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=len(a)
result=1
i=0
j=0
while True:
    if a[i]!=result:
        del a[i]
    else:
        result+=1
        i+=1
    j+=1
    if j>=b:
        break
if a==[]:
    print(-1)
else:
    print(b-len(a))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int, input().split()))
flag=0
i=0
now=1
cnt=0
ans=0

if 1 not in A:
	print(-1)
	
else:
	while flag==0:
		if A[i]!=now:
			A.pop(i)
			cnt+=1
			ans+=1
		else:
			i+=1
			cnt+=1
			now+=1
		
		if cnt==N:
			flag=1
	
	print(ans)

Submission status: Time Limit Exceeded

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

ans = 1
cnt = 0

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

Submission status: Time Limit Exceeded

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

seta = set(a)
ans = 0
if 1 not in seta:
  print(-1)
  sys.exit()
for i in range(1,n+1):
  if i in seta:
    idx = a.index(i)
  else:
    ans += len(a) 
    break 
  ans += idx
  a = a[idx+1:]
  seta = set(a)
  
print(ans)

Submission status: Time Limit Exceeded

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

ans = 0
if 1 not in a:
  print(-1)
  sys.exit()
for i in range(1,n+1):
  if i in a:
    idx = a.index(i)
  else:
    ans += len(a) 
    break 
  ans += idx
  a = a[idx+1:]
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
N = int(input())
a = np.array(list(map(int,input().split())))
a_ori = a
num = 1
sum = 0
tot_sum = 0
num_list = list(zip(*np.where(a == num)))
if len(num_list ) ==0:
    print(-1)
    sys.exit()
else:
    cut_num = num_list[0][0]
    sum += cut_num
    a = a[cut_num:]
    while True:
        num += 1
        num_list = list(zip(*np.where(a == num)))
        if len(num_list) == 0:
            sum += len(a) - num+1
            break
        cut_num = num_list[0][0]
        now_len= len(a)
        a1 = a[:num-1]
        a2 = a[cut_num:]
        a = np.concatenate([a1,a2])
        sum += ( now_len - len(a))
print(sum)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
arr = list(map(int,input().split()))
if 1 not in arr:
    print(-1)
else:
    i = 1
    while i in arr:
        l = arr.index(i)
        arr = arr[l:]
        i += 1
    print(n-i+1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a=list(map(int, input().split()))
l = [i for i in range(1,N+1)]
c=0
for j in range(N):
  if l[0]==a[j]:
    del l[0]
  elif l[0]!=a[j]:
    c+=1
if len(l)==N:
  print(-1)
  exit()
print(c)

Submission status: Time Limit Exceeded

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

st = 0
cnt = 0
mx = max(A)

for i in range(1, mx+1):
    if i in A:
        cnt += 1
        st = A.index(i)
        A = A[st:]
    elif N == N - cnt:
        print(-1)
        exit()
    else:
        print(N-cnt)
        exit()
print(N-cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

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

cnt = 0
c = 0

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

if cnt == 0:
  print('-1')

else:
  print(n - cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
 
a = list(map(int, input().split()))
 
def myindex(l, x, default=-1):
    if x in l:
        return l.index(x)
    else:
        return default
 
for i in range(1, n+1):
  b = myindex(a, i, default=-1)
  if b == -1:
    del a[(i-1):]
    break
  else:
    del a[(i-1):b]
 
 
if len(a) == 0:
  print('-1')
 
else:
  print(n - len(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

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

for i in range(1, n+1):
  if a.count(i) == 0:
    del a [(i-1):]
    break
  else:
    index = a.index(i)
    del a[(i-1):index]

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

else:
  print(n - len(a))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = tuple(map(int, input().split()))
pos = 0
j = 1

while True:
    for i, data in enumerate(a[pos:]):
        if data == j:
            pos += i + 1
            j += 1
            break
    else:
        print((-1, n - j + 1)[j != 1])
        break

Submission status: Time Limit Exceeded

Solution code:
from copy import deepcopy

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

    for bit in range(1<<N):
        broken = []
        for i in range(N):
            if bit>>i&1:
                broken.append(i)
        A_copy = deepcopy(A)
        for b in broken[::-1]:
            del A_copy[b]
        cnt = 0
        for i, a in enumerate(A_copy):
            if i+1==a:
                cnt += 1
        if cnt==len(A_copy) and cnt!=0:
            print(len(A)-len(A_copy))
            quit()
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(input())
A=list(map(int,input().split()))
c=0
for i in range(1,N+1):
    for j in range(N):
        if A[j]!=i:
            c+=1
        elif A[j]==i:
            break
    if c+i==N:
        print(c)
        sys.exit()
    continue
print(-1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
isError = True
cnt = 1
while len(a)>=1:
    if cnt == a.pop(0):
        isError = False
        cnt += 1
if isError:
    print(-1)
else:
    print(n-(cnt-1))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N=int(input())
L=tuple(map(int,input().split()))
if 1 not in L:
  print(-1)
  sys.exit()
for i in range(1,N+1):
  if i not in L:
    print(N+1-i)
    sys.exit()
  else:
    a=L.index(i)
    L=L[a:]
print(0)

Submission status: Time Limit Exceeded

Solution code:
import sys
N=int(input())
A=list(map(int,input().split()))
j=1
i=0
if min(A)!=j:
  print(-1)
  sys.exit()
else:
  i=A.index(j)
  j+=1
while i<N:
  A=A[i:]
  if j not in A:
    print(N-j+1)
    sys.exit()
  elif j in A:
    i=A.index(j)
    j+=1
print(i,j)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import bisect
readline = sys.stdin.readline


def main():
    n = int(readline().rstrip())
    A = list(map(int, readline().rstrip().split()))
    listA = A
    k = 0

    if listA.count(1) == 0:
        print(-1)
        return

    for i in range(1, n + 1):
        cnt = listA.count(i)
        if cnt == 0:
            break

        idx = listA.index(i)
        listA = listA[idx: len(listA)]
        k += 1

        if len(listA) == 0:
            break
    print(n - k)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys

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

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

for a in A:
  if a == check:
    c_ls.append(a)
    check += 1
  else:
    cnt += 1
  if c_ls == ls[:(N-cnt)] and c_ls != []:
    bool1 = True
    break

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans_list=[]
for i in range(2**n):
    num_list=[]
    for j in range(n):
        if (i >> j) & 1:
            num_list.append(a[j])
    if list(range(1,len(num_list)+1))==num_list:
        ans_list.append(len(num_list))

if (len(ans_list)==1)and(ans_list[0]==0):
    print(-1)
else:
    print(n-max(ans_list))

Submission status: Time Limit Exceeded

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

cnt = 0
idx = 0
while 1:
  if (cnt+1) in A[idx:]:
    cnt += 1
    idx = A.index(cnt)

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
idx = 0
ans = 0
while 1:
  if idx >= len(A):
    break
  elif A[idx] == idx+1:
    idx += 1
  else:
    A.pop(idx)
    ans += 1

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

Submission status: Time Limit Exceeded

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

if N==1 and A[0]==1:
  print(0)
elif 1 not in A:
  print(-1)
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
A=[int(x) for x in input().rstrip('\n').split()]
res = 0
now = 0
for i in range(1,N+1):
  if i in A:
    res += 1
    now = A.index(i)
    A = A[now:]
  else:
    break
if res == 0:
  print(-1)
else:
  print(N-res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
A=[int(x) for x in input().rstrip('\n').split()]
res = 0
now = 0
for i in range(1,N+1):
  if i in A:
    res += 1
    now = A.index(i)
    A = A[now:]
  else:
    break
if res == 0:
  print(-1)
else:
  print(N-res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

N=int(input().rstrip('\n'))
A=np.array([int(x) for x in input().rstrip('\n').split()])
res = 0
for i in range(N):
  if i+1 in A:
    a = np.where(A==i+1)
    b = a[0][0]
    A = A[b:]
    res += 1
  else:
    break
if res == 0:
  print(-1)
else:
  print(N-res)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

N=int(input().rstrip('\n'))
A=[int(x) for x in input().rstrip('\n').split()]
res = 0
for i in range(N):
  if i+1 in A:
    A = A[A.index(i+1):]
    res += 1
  else:
    break
if res == 0:
  print(-1)
else:
  print(N-res)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    a_s = sorted(set(a), key=a.index)
    cnt = 0
    order = 1
    if not 1 in a_s:
        print(-1)
        exit()
    else:
        for i in range(len(a)):
            if a[i] != order:
                cnt += 1
            if a[i] == order:
                order += 1
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:

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

n = -1
for i in range(1,N+1):
    try:
        n = a.index(i)
    except:
        i = i-1
        break
    a = a[n:]

if n > -1:
    M = N - i
else:
    M = -1

print(M)

Submission status: Time Limit Exceeded

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

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

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

tempA = A.copy()
i = 1
while i in set(tempA):
    k = tempA.index(i) + 1
    tempA = tempA[k:]
    i += 1

print(N-(i-1))

Submission status: Time Limit Exceeded

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

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

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

tempA = A.copy()
i = 1
while i in tempA:
    k = tempA.index(i) + 1
    tempA = tempA[k:]
    i += 1

print(N-(i-1))

Submission status: Time Limit Exceeded

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

import sys
input = sys.stdin.readline

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

start = 0
n = 0
for i in range(N):
    if i + 1 in A[start:]:
        start = len(A[:start]) + A[start:].index(i+1) + 1
        n = i + 1
    else:
        break
if n != 0:
    print(N-n)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

length = len(A)
head = 0
i = 1

while True:
    if i in A:
        head = A.index(i)
        A = A[head:]
        i += 1
    else:
        break

if i == 1:
    print(-1)
else:
    print(length - i + 1)

Submission status: Time Limit Exceeded

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


head = 0
i = 1

while True:
    if i in A[head:]:
        head = A[head:].index(i)
        i += 1
    else:
        break

if i == 1:
    print(-1)
else:
    print(len(A) - i + 1)

Submission status: Time Limit Exceeded

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

# In[24]:


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


# In[25]:


mylist = [x for x in a]
flag = True
num = 1
ans = 0
while flag:
    if mylist[num-1] != num:
        mylist.pop(num-1)
        ans += 1
    else:
        num += 1
    if len(mylist) == 0:
        flag = False
        print(-1)
    elif mylist == list(range(1,num)):
        flag = False
        print(ans)


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
L = np.array(input().split(),dtype = int)

m = min(N,max(L))

r = np.array(np.linspace(1,m,m),int)


dp = [[0 for i in range(N+1)] for j in range(m+1)]

for i in range(m):
    for j in range(N):
        dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j])
        if r[i] == L[j]:
            dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j]+1)

ans = np.max(dp,axis=1)

c = np.array(np.linspace(0,m,m+1))
#print(c)
#print(ans)
d = np.where(ans==c,ans,0)
#print(d)
e = max(d)
if e == 0:
    print(-1)
else:
    print(N-e)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
S=list(map(int,input().split()))
first=len(S)
count=0
pos=0
mokuteki=1
while count<N:
    if S[pos]!=mokuteki:
        ret=S.pop(pos)
    else:
        mokuteki+=1
        pos+=1
    count+=1

if len(S)>0:
    print(first-len(S))
else:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

def LII_split():
    return list(map(int, input().split()))

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N = input_int()
a_list = LII_split()

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

ans = 0
pre_idx = -1  # 前の数字のindex

for i in range(1, N + 2):
    i_idx = -1
    for idx, x in enumerate(a_list[pre_idx+1:]):
        if x == i:
            i_idx = idx
    # print(f'{i_idx=}')
    # return
    if i_idx >= 0:
        ans += i_idx - pre_idx - 1  # 砕く数
        # if i == 1:
        #     ans -= 1
        pre_idx = i_idx
    elif i == 1:
        ans = -1
        break
    else:
        ans += N - pre_idx - 1
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)  # 再帰関数の再帰の深さを設定
to_index = lambda x: int(x) - 1  # 入力した数字に1を引いたものを返す
print_list_in_2D = lambda x: print(*x, sep="\n")  # リストの要素を改行を挟んで表示する関数

# 入力を整数に変換して受け取る
def input_int():
    return int(input())

def map_int_input():
    return map(int, input())

MII = map_int_input

def MII_split():
    return map(int, input().split())

def MII_to_index():
    return map(to_index, input())

def MII_split_to_index():
    return map(to_index, input().split())

# 入力全てを整数に変換したものの配列を受け取る
def list_int_inputs():
    return list(map(int, input()))

LII = list_int_inputs

def LII_split():
    return list(map(int, input().split()))

# 2次元リスト化
def LII_2D(rows_number):
    return [LII() for _ in range(rows_number)]

def LII_split_2D(rows_number):
    return [LII_split() for _ in range(rows_number)]

N = input_int()
a_list = LII_split()

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

ans = 0
pre_idx = -1  # 前の数字のindex

for i in range(1, N + 2):
    i_idx = -1
    for idx, x in enumerate(a_list):
        if x == i and idx > pre_idx:
            i_idx = idx
    # print(f'{i_idx=}')
    # return
    if i_idx >= 0:
        ans += i_idx - pre_idx - 1  # 砕く数
        # if i == 1:
        #     ans -= 1
        pre_idx = i_idx
    elif i == 1:
        ans = -1
        break
    else:
        ans += N - pre_idx - 1
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
def main():
  N=int(input())
  L=list(map(int,input().split()))
  if 1 not in L:
    print(-1)
    sys.exit()
  for i in range(1,N+1):
    if i not in L:
      print(N+1-i)
      sys.exit()
    else:
      a=L.index(i)
      L=L[a:]
  print(0)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

ans=0
for i in range(N):
    #print(ans)
    flag='on'
    if ans+i==N:
        break
    for j in range(len(a)):
        if a[j]==i+1:
            break
        elif a[j]==0:
            continue
        elif a[j]==i and flag=='on':
            a[j]=0
            flag='off'
        else:
            a[j]=0
            ans+=1
        #print(a)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

idx = my_index(arr, 1)

if idx == -1:
    print(-1)
else:
    search = 2
    while idx < n:
        s_arr = arr[idx:]
        n_idx = my_index(s_arr, search)
        if n_idx == -1:
            break
        idx += n_idx
        search += 1
    
    print(n - search + 1)

Submission status: Time Limit Exceeded

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+1:]:
    x = A[x+1:].index(i)
    count += 1
  else:
    break
if count != 0:
  print(N-count)
else:
  print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

b = 1
i = 0
pos = 0
while pos < n - 1:
    for j in a[pos:]:
        pos += 1
        if j == b:
            b += 1
            break
        else:
            i += 1
    if b == 1:
        print(-1)
        sys.exit()
print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
a = [int(i) for i in input().split()]
if 1 not in a:
    print(-1)
    sys.exit()
    
b = 1
i = 0
pos = 0
while pos < n - 1:
    for j in a[pos:]:
        pos += 1
        if j == b:
            b += 1
            break
        else:
            i += 1
print(i)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
def haitteru(l, x):
	if x in set(l):
		return l.index(x)
	else:
		return -1


cnt = n+2
x = haitteru(a, 1)

if x != -1:
	cnt = 2
	while True:
		x = haitteru(a[x+1:], cnt)
		if x == -1:
			break
		cnt += 1

print(n-cnt+1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[0]*n
def int1(x):
  return int(x)-1
a[:]=map(int1,input().split())
#print(a)
import numpy as np
l=np.argsort(a)
#print(l)
del a[0:l[0]]
r=0
for ii in range(n):
  if ii in a:
    del a[0:a.index(ii)]
    r+=1
    #print(a,r)
  else:
    #print(a)
    if r==0:
      print(-1)
    else:
      print(n-r)
    exit()
print(n-r)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=[0]*n
def int1(x):
  return int(x)-1
a[:]=map(int1,input().split())
#print(a)
r=0
for ii in range(n):
  if ii in a:
    del a[0:a.index(ii)]
    r+=1
    #print(a,r)
  else:
    #print(a)
    if r==0:
      print(-1)
    else:
      print(n-r)
    exit()
print(n-r)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a_list = list(map(int, input().split()))
cnt = 0
ref_num = 1
if 1 not in a_list:
    print(-1)
    exit()
while True:
  if a_list == []:
      print(cnt)
      exit()
  for i in range(len(a_list)):
    if ref_num not in a_list:
      cnt += len(a_list)
      print(cnt)
      exit()
    elif ref_num == a_list[i]:
      red = len(a_list[:i])
      a_list = a_list[i+1:]
      cnt += red
      ref_num += 1
      break

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

    
def findBrick(n,a,counter=0):
  if len(a) == 0:
    return counter
  elif a[0] == counter + 1:
    return findBrick(n,a[1:],counter+1)
  else:
    return findBrick(n,a[1:],counter)

c = findBrick(N,a)
  
ans = -1 if c == 0 else N - c
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = tuple(map(lambda n: int(n) - 1, input().split()))

if not 0 in A:
    print(-1)
else:
    nf = 0
    for j in range(N+1):
        # jは残す数
        try:
            nf = A[nf:].index(j)
        except ValueError:
            break

    print(N - j)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = tuple(map(lambda n: int(n) - 1, input().split()))

if not 0 in A:
    print(-1)
else:
    for j in range(N+1):
        # jは残す数
        try:
            nf = A.index(j)
            A = A[nf:]
        except ValueError:
            break

    print(N - j)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = tuple(map(lambda n: int(n) - 1, input().split()))

if not 0 in A:
    print(-1)
else:
    for j in range(N+1):
        # jは残す数
        if j in A:
            nf = A.index(j)
            A = A[nf:]
        else:
            break

    print(N - j)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = tuple(map(lambda n: int(n) - 1, input().split()))

minlen = 10**9
for k in range(N, 0, -1):
    # kは残す数
    A_ = A[:]
    for j in range(k):
        if j in A_:
            nf = A_.index(j)
            A_ = A_[nf:]
        else:
            break
    else:
        minlen = min(minlen, N - k)
        break

if minlen >= N:
    print(-1)
else:
    print(minlen)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = tuple(map(lambda n: int(n) - 1, input().split()))

minlen = 10**9
for k in range(1, N+1):
    # kは残す数
    A_ = A[:]
    for j in range(k):
        if j in A_:
            nf = A_.index(j)
            A_ = A_[nf:]
        else:
            break
    else:
        minlen = min(minlen, N - k)

if minlen >= N:
    print(-1)
else:
    print(minlen)