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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

pos=0
for x in range(1,n+1):
  if a:
    t = a.index(x) if x in a else -1
    if t == -1:
      if pos==0:
        print(-1)
      else:
        print(n-pos)
      break
    else:
      pos+=1
      a=a[t+1:]
  else:
    continue
else:
  print(n-pos)

Submission status: Time Limit Exceeded

Solution code:
input1 = input()
input2 = input()
N = int(input1)
list_an = input2.split()
list_an = list(map(int,list_an))

list_an_sub = []


total = 0

def check():
    check_total = 0
    for i in range(len(list_an)):
        if list_an[i] == i+1:
            check_total += 0
        else:
            check_total += 1
    if check_total == 0:
        return 1
    if check_total == 0:
        return 0
                

while check() is None:
    for i in range(len(list_an)):
        if i <= len(list_an):
            if list_an[i] == i+1:
                total += 0
            else:
                del(list_an[i])
                total += 1
                break
        else:
            break
   
if list_an == []:
    print(-1)
else:
    print(total)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# process/output
if not 1 in alist:
    print(-1)

else:
    ans = 0
    i = 1
    while len(alist) > 0:
        if i in alist:
            index = alist.index(i)
            alist = alist[index+1:]
            ans += index
            i += 1
            
        else:
            ans += len(alist)
            break
        
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
numN = int(input())
input_line = input().split(' ')
brick = [int(i) for i in input_line]

bestSeq = 0
index2 = -1

for i, b in enumerate(brick):
  seq = 0
  if b == 1:
    if i < index2:
      continue
    seq += 1
    cur = 2
    for j, c in enumerate(brick[i:]):
      if c == cur:
        seq += 1
        cur += 1
        index2 = j
    if seq > bestSeq:
      bestSeq = seq

if bestSeq == 0:
  print(-1)
else:
  print(numN - bestSeq)

Submission status: Time Limit Exceeded

Solution code:
numN = int(input())
input_line = input().split(' ')
brick = [int(i) for i in input_line]

bestSeq = 0

for i, b in enumerate(brick):
  if numN - i < bestSeq:
    break
  seq = 0
  if b == 1:
    seq += 1
    cur = 2
    for c in brick[i:]:
      if c == cur:
        seq += 1
        cur += 1
    if seq > bestSeq:
      bestSeq = seq

if bestSeq == 0:
  print(-1)
else:
  print(numN - bestSeq)

Submission status: Time Limit Exceeded

Solution code:
numN = int(input())
input_line = input().split(' ')
brick = [int(i) for i in input_line]

bestSeq = 0

for i, b in enumerate(brick):
  seq = 0
  if b == 1:
    seq += 1
    cur = 2
    for c in brick[i:]:
      if c == cur:
        seq += 1
        cur += 1
    if seq > bestSeq:
      bestSeq = seq

if bestSeq == 0:
  print(-1)
else:
  print(numN - bestSeq)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = [0] + A
ans = 0
i = 1
while i < len(A):
    if A[i] == i:
        i += 1
    else:
        ans += 1
        A.pop(i)
if ans >= N:
    ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
ans = 0
index = 1
if min(A) != 1:
    print(-1)
else:
    for i in range(N):
        left = A.pop(0)
        if left == index:
            index += 1
        else:
            ans += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
count = int(input())

number = input().split()

number = list(number)

number = [int(i) for i in number]

#print(number)

position = 0

positionlist = []

for i in range(count+1):
    positionlist.append(-1)

#print(positionlist)

maxnumber = 0

for x in range(count):
    #print(x+1, "を探します。")
    for y in range(count):
        if x+1 == number[y]:
            positionlist[x] = y
        else:
            continue

#print(positionlist)
if positionlist[0] != -1:
    for i in range(count):
        if positionlist[i] < positionlist[i+1]:
            continue
        else:
            maxnumber = i+1
            break
else:
    maxnumber = count+1

#print(maxnumber)

answer = count - maxnumber

print(answer)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
i=1
j=0
b=[]
while True:
  if j>=len(a):
      break
  if a[j]==i:
      a=a[j:]
      b.append(i)
      i+=1
      j=0
  j+=1
for k in range(len(b)):
    if b[k]!=k+1:
        print(-1)
        exit()
if not b:
  print(-1)
  exit()

print(n-len(b))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
bricks = list(map(int,input().split(" ")))
K=0
if 1 not in bricks:
    K = -1

else:
    K = 0
    value = 1
    while(value in bricks):
        index = bricks.index(value)
        bricks[value-1:index]=[]
        K += (index+1-value)
        value += 1

    K += len(bricks[value-1:])
print(K)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
 
now = 1
ans = 0
match = 0
chuumoku = 0
for i in range(n):
    if a[chuumoku] != now:
        ans += 1
        a.pop(chuumoku)
    else:
        match += 1
        chuumoku += 1
        now += 1
 
    if chuumoku == len(a) and match == 0:
        print(-1)
        exit(0)
print(ans)

Submission status: Time Limit Exceeded

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

now = 1
ans = 0
match = 0
chuumoku = 0
for i in range(n):
    if a[chuumoku] != now:
        ans += 1
        a.pop(chuumoku)
    else:
        match += 1
        chuumoku += 1
        now += 1

    if chuumoku == len(a) and match == 0:
        print(-1)
        exit(0)

print(ans)

Submission status: Time Limit Exceeded

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

i = 0
cnt = 0
while len(A) > i:
    if A[i] != i + 1:
        A.pop(i)
        cnt += 1
    else:
        i += 1
if cnt == n:
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(N):
    if i >= len(a):
        break
    for j in range(len(a)):
        if a[i] == i + 1:
            break
        else:
            a.pop(i)

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

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
remain = 1  # 残す整数
index = 0

if remain not in a:
    print(-1)
    exit()

while remain in a[index:]:
    index = a[index:].index(remain)
    remain += 1

print(N - remain + 1)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_l = list(map(int, input().split()))
if min(a_l) !=1:
    print(-1)
else:
    i = 1
    a_l = a_l[a_l.index(i)+1:]
    i += 1
    while True:
        if a_l.count(i) > 0:
            a_l = a_l[a_l.index(i)+1:]
            i += 1
        else:
            break
    print(n - (i-1))

Submission status: Time Limit Exceeded

Solution code:
def run(N, a):
    ans = -1
    cnt = 0
    idx = 0
    match = False
    for i in range(1, N+1):
        if idx > N - 1:
            break
        for ai in a[idx:]:
            idx += 1
            if ai == i:
                # print('match', i, ai, idx)
                match = True
                break
            else:
                # print('break', i, ai, idx)
                cnt += 1
    if match:
        ans = cnt
    return ans


def main():
    N = int(input())
    a = list(map(int, input().split()))
    print(run(N, a))


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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


for i in range(N):
    index = j
    for a in A[index:]:
        j = j + 1
        if a == (i+1):
           sum = sum + 1
           break
    if prev == sum:
        break
    else:
        prev = sum

if sum == 0:
    print('-1')
else:
    print(N-sum)

Submission status: Time Limit Exceeded

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

for i in range(N):
    index = j
    for a in A[index:]:
        j = j + 1
        if a == (i+1):
           sum = sum + 1
           break

if sum == 0:
    print('-1')
else:
    print(N-sum)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

number = 1
count = 0

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

while len(nums) > 0:
    try:
        idx = nums.index(number)
    except ValueError:
        count += len(nums)
        break
    count += idx
    nums = nums[idx+1:]
    number += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())    #レンガの個数
num_line = [int(i) for i in input().split(" ")]	#レンガの数字
count = 0	#砕く回数
j = 0	#初期値

while(j != len(num_line)):
  if num_line[j] != j + 1:
    del num_line[j]
    count += 1
    j = 0
  else:
    j += 1

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())    #レンガの個数
num_line = [int(i) for i in input().split(" ")]	#レンガの数字
count = 0	#砕く回数
j = 0	#初期値

while(1):
  if num_line[j] != j + 1:
    del num_line[j]
    count += 1
    j = 0
  else:
    j += 1
  
  if count == N:
    print(-1)
    break
  elif(j == len(num_line)):
    print(count)
    break

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_lst = [int(elem) for elem in input().split()]

def calc(num, lst):
  if num in lst:
    return lst.index(num), lst[lst.index(num)+1:]
  else:
    return len(lst), []

if not 1 in a_lst:
  print(-1)
else:
  num = 1
  res = 0
  while a_lst != []:
    tmp, a_lst = calc(num, a_lst)
    res += tmp
    num += 1
  print(res)

Submission status: Time Limit Exceeded

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

cnt = 0
i = 0

while i < len(a):
  if a[i] != i+1:
    del a[i]
    cnt += 1
    if len(a) == 0:
      break
  else:
    i += 1

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

Submission status: Time Limit Exceeded

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

cnt = 0
i = 0

while i < len(a):
  if a[i] != i+1:
    del a[i]
    cnt += 1
  else:
    i += 1

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

Submission status: Time Limit Exceeded

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

if not(1 in a):
  print("-1")
else:  
    for i in range(1,N+1):
        for j in range(temp,N):
            if (a[j]==i and count+1 == i):
                count=count+1
                temp=j
                break           
    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()))
temp=0
count=0
for i in range(1,N+1):
    for j in range(temp,N):
        if (a[j]==i and count+1 == i):
            count=count+1
            temp=j
            break           
if count==0:
    print("-1")
else:
    print(N-count)

Submission status: Time Limit Exceeded

Solution code:
a=int(input())
lis=list(map(int,input().split()))
suu=0
ans=0
while len(lis)>0:
  if lis[0]==suu+1:
    suu+=1
  else:
    ans+=1
  del lis[0]
if ans==a:
  print(-1)
else:
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():
    N = int(input())
    a = list(map(int, input().split()))
    num = 0
    blocks = 0

    while True:
        flag=0
        num += 1

        try:
            i = a.index(num)
            blocks += i
            del a[:i+1]
            flag=1

        except:
            if(num==1):
                print('-1')
                sys.exit()
        if(flag==0):
            blocks += len(a)
            print(blocks)
            sys.exit()

main()

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():
    N = int(input())
    a = list(map(int, input().split()))
    num = 0
    blocks = 0

    while True:
        flag=0
        num += 1
        for i in range(0, len(a)):
            if(num==a[i]):
                blocks += i
                del a[:i+1]
                flag=1
                break


        if(flag==0):
            if(num==1):
                print('-1')
                sys.exit()

            blocks += len(a)
            print(blocks)
            sys.exit()

main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
left = 0
num = 1
while True:
    Flag = True
    for i in range(len(a)):
        if a[i] == num:
            left += 1
            a = a[i:]
            Flag = False
            num += 1
            break
    if Flag:
        break
if left == 0:
    ans = -1
else:
    ans = n - left
print(ans)

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(n):
        if i==0:
            for j in range(0,n):
                if i+1==a[j]:
                    break
                else:
                    a[j]=0
        else:
            if i in a:
                for j in range(a.index(i)+1,n):
                    if i+1==a[j]:
                        break
                    else:
                        a[j]=0
            else:
                break
    print(a.count(0))

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(n):
        if i==0:
            for j in range(0,n):
                if i+1==a[j]:
                    break
                else:
                    a[j]=0
        elif i in a:
            for j in range(a.index(i)+1,n):
                if i+1==a[j]:
                    break
                else:
                    a[j]=0
        else:
            break
    print(a.count(0))

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():

    # sys.stdin = open('input148d.txt')

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

    ni = n
    comp = True
    while comp:
        comp = False
        for j in range(n):
            if a[j] != j + 1:
                a.pop(j)
                n -= 1
                comp = True
                break

   
    if n == 0:
        print(-1)
    else:
        print(ni - n)
            
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
nm = 1
ans = 0
while a:
    x = a.pop(0)
    if x == nm:
        nm += 1
    else:
        ans += 1

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
min = -1
num = 0
for n in range(N):
    for m in range(len(A)):
        last = num
        if A[m] == n+1:
            num += 1
            A = A[m+1:]
            break
    if last == num or len(A)==0:
        break
if num != 0:
    min = N-num
print(min)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
min = -1
num = 0
for n in range(N):
    for m in range(len(A)):
        last = num
        if A[m] == n+1:
            num += 1
            A = A[m+1:]
            break
    if last == num:
        break
if num != 0:
    min = N-num
print(min)

Submission status: Time Limit Exceeded

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

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

print(N - (i - 1) if i != 1 else -1)

Submission status: Time Limit Exceeded

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

i = 0
ans = 0
while i < len(a):
    if not a[i] == i + 1:
        del a[i]
        ans += 1
        i -= 1
    i += 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(" ")))

def Lsearch(B,key,pt):
    for i in range(len(B)-pt):
        if B[i+pt] == key:
            return i
    return -1

num,pt,n_pt,count = 1,0,0,0
while Lsearch(A,num,pt) != -1:
    n_pt = Lsearch(A,num,pt) + 1
    count += n_pt - pt -1
    pt = n_pt
    num += 1
if pt != 0:
    count += len(A) - pt
    print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    N = int(input())
    a = list(map(int,input().split()))
    delsum = 0

    i = 1
    while True:
        try:
            point = a.index(i)

        except ValueError:
            if(i==1):
                print('-1')
                sys.exit()

            delsum += len(a)-i+1
            print(delsum)
            sys.exit()

        delsum += point-(i-1)

        del a[i-1:point]

        i+=1

    print('-1')

main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 1
cnt = 0

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lst = list(map(int, input().split()))
i, j = 0, 1
while i < len(lst):
    if lst[i] != j:
        lst.pop(i)
    else:
        i += 1
        j += 1

if not lst:
    print(-1)
else:
    print(n - len(lst))

Submission status: Time Limit Exceeded

Solution code:
import sys
n=int(input())
l=list(map(int,input().split()))
cnt=0
t=1
if not (t in l):
  print(-1)
  sys.exit()
else:
  c=l.index(t)
  cnt+=1
  t+=1
  s=l
  while(t in s[c:]):
    s=l[c:]
    c=s.index(t)
    t+=1
    cnt+=1
print(n-cnt)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
# renga = []
# renga = [int(x) for x in stdin.readline().rstrip().split()]
renga = list(map(int,stdin.readline().rstrip().split()))

count = 0
game = True
judge = False

while(game):
  for i in range(n):
    if(renga[i] == i+1):
      pass
    else:
      judge = False
      del renga[i]
      n-=1
      count += 1
      break

  if(len(renga) == 0):
    game = False
    print(-1)

  if(judge):
    game = False
    print(count)

  judge = True

Submission status: Time Limit Exceeded

Solution code:
import sys
n=int(input())
l=list(map(int,input().split()))
cnt=0
t=1
if not (t in l):
  print(-1)
  sys.exit()
else:
  c=l.index(t)
  cnt+=1
  t+=1
  while(t in l[c:]):
    s=l[c:]
    c=s.index(t)
    t+=1
    cnt+=1
print(n-cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = set(a)
ans = 0
l = []
for i in s:
  if i in a:
    ans += 1
    f = a.index(i)
    a = a[f:]
    l.append(i)
  else:
    break
ll = list(range(1, ans + 1))
if l == ll:
  ans = n - ans
else:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
# renga = []
# renga = [int(x) for x in stdin.readline().rstrip().split()]
renga = list(map(int,input().split()))

count = 0
game = True
judge = False

while(game):
  for i in range(n):
    if(renga[i] == i+1):
      pass
    else:
      judge = False
      del renga[i]
      n-=1
      count += 1
      break

  if(len(renga) == 0):
    game = False
    print(-1)

  if(judge):
    game = False
    print(count)

  judge = True

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
renga = []
renga = [int(x) for x in stdin.readline().rstrip().split()]

count = 0
game = True
judge = False

while(game):
  for i in range(n):
    if(renga[i] == i+1):
      pass
    else:
      judge = False
      del renga[i]
      n-=1
      count += 1
      break

  if(len(renga) == 0):
    game = False
    print(-1)

  if(judge):
    game = False
    print(count)

  judge = True

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
renga = []
renga = [int(x) for x in stdin.readline().rstrip().split()]

count = 0
game = True
judge = False

while(game):
  for i in range(n):
    if(renga[i] == i+1):
      pass
    else:
      judge = False
      renga.pop(i)
      n-=1
      count += 1
      break

  if(len(renga) == 0):
    game = False
    print(-1)

  if(judge):
    game = False
    print(count)

  judge = True

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin


n = int(stdin.readline().rstrip())
renga = []*n
renga = [int(x) for x in stdin.readline().rstrip().split()]

count = 0
game = True
judge = False

while(game):
  for i in range(n):
    if(renga[i] == i+1):
      judge = True
    else:
      judge = False
      renga.pop(i)
      n-=1
      count += 1
      break

  if(len(renga) == 0):
    game = False
    print(-1)

  if(judge):
    game = False
    print(count)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

renga = []
n = int(stdin.readline().rstrip())
renga = [int(x) for x in stdin.readline().rstrip().split()]

count = 0
game = True
judge = False

while(game):
  for i in range(n):
    if(renga[i] == i+1):
      judge = True
    else:
      judge = False
      renga.pop(i)
      n-=1
      count += 1
      break

  if(len(renga) == 0):
    game = False
    print(-1)

  if(judge):
    game = False
    print(count)

Submission status: Time Limit Exceeded

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

p = 1
ans = 0
while True:
    if p not in a_list:
        ans += len(a_list)
        break
    i = a_list.index(p)
    ans += i
    a_list = a_list[i+1:]
    p += 1
if ans == n:
    ans = -1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
lis = list(map(int,input().split()))
if not (1 in lis):
  print(-1)
  quit()
ans = 0
c = 1
while(lis):
  if lis[0] != c:
    ans += 1
  else: 
    c += 1
  del(lis[0])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
a = map(int,raw_input().split())
con = 0
i = 0
while i <= N:
  if len(a)==0:
    con = -1
    print con
    break
  elif len(a)==con:
    print N-con
    break
  elif a[con] == con+1:
    con = con + 1
  else:
    a.pop(con)
  i= i+1

Submission status: Time Limit Exceeded

Solution code:
N = int(raw_input())
a = map(int,raw_input().split())
con = 0
i = 0
while i <= N:
  if len(a)==0:
    con = -1
    print con
    break
  elif len(a)==con:
    print N-con
    break
  if a[con] == con+1:
    con = con + 1
  else:
    a.pop(con)
  i= i+1

Submission status: Time Limit Exceeded

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

def Lsearch(B,key):
    for i in range(len(B)):
        if B[i] == key:
            return i
    return -1

num,pt,n_pt,count = 1,0,0,0
while Lsearch(A[pt:],num) != -1:
    n_pt = Lsearch(A[pt:],num) + 1
    count += n_pt - pt -1
    pt = n_pt
    num += 1
if pt != 0:
    count += len(A) - pt
    print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

for i in range(N):
    if a[i] == 1:
        count += i
        k = i
        break
    elif min(a) >= 2:
        print(-1)
        exit()
        
M = 2

for i in range(k+1, N):
    if a[i] != M:
        count += 1
    else:
        M+= 1
        

print(count)

Submission status: Time Limit Exceeded

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

x=[]
flag = True

if 1 not in a:
  ans = -1
  flag = False

if flag == True:
  for i in range(1, n+1):
    if i in a:
      j = a.index(i)
      x.append(i)
      a=a[j:]
    else:
      break

  ans = n - len(x)
  

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a  = list(map(int, input().split()))
begin = -1
max = N+1

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

print(N-max)

Submission status: Time Limit Exceeded

Solution code:
n = input()
a = [int(i) for i in input().split()] 
count_up = 1
if count_up in a:
  pass
else:
  print(-1)
  exit()
num = len(a)

while count_up < 2000000:
  if count_up in a:
      for index,i in enumerate(a):
        if i == count_up:

          p = len(a)
          a = a[index:]

          while count_up in a:a.remove(count_up)
          break
        
  else:    
    break
  
  count_up += 1


print(num - count_up  + 1)

Submission status: Time Limit Exceeded

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

if 1 in a:
    ans = 1
    while ans in a:
        a = a[a.index(ans):]
        ans += 1
    print(N - ans + 1)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

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


for i in range(N-1):
    if A[i] not in l and l[c]+1 == A[i]:
        l.append(A[i])
        c+=1

if l[-1]+1==A[-1]:
    l.append(A[-1])

print(N+1-len(l))

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a_list=list(map(int,input().split()))
conclu_list=[]
index=0
for x in range(1, N+1):
  for j in a_list:
    if x==j:
      conclu_list.append(j)
      del a_list[:a_list.index(j)]
      index+=1
      break
  if index<x:
    break
if len(conclu_list)==0:
  print(-1)
else:
  print(N-len(conclu_list))

Submission status: Time Limit Exceeded

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

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


for i in range(N-1):
    if A[i] in l or l[c]+1 != A[i]:
        continue

    else:
        l.append(A[i])
        c+=1

if l[-1]+1==A[-1]:
    l.append(A[-1])
print(N+1-len(l))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
count = 0
A_len = len(A)
m = 1 in A
if not 1 in A:
    print(-1)
else:
    m = A.index(1)
    A = A[m:]
    count += 1
    for i in range(1,N):
        B = sorted(list(set(A)))
        if len(B) <= 1:
            break
        if B[1] == i+1:
            A = A[A.index(i+1):]
            count += 1
        else: 
            break
    print(A_len-count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
idx = 0
cnt = 0
for n in range(N):
    if A[idx] == idx+1:
        idx += 1
    else:
        A.pop(idx)
        cnt += 1
 
if cnt == N:
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

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

def Lsearch(B,key):
    B.append(key)
    for i in range(len(B)):
        if B[i] == key:
            if i != len(B)-1:
                B.pop() #pythonは参照渡しが基本なので元のリストに戻しておく
                return i
            else:
                B.pop()
                return -1

num,count,tmp,pt,flag = 1,0,0,0,0
if Lsearch(A,1) == -1:
    flag = 1
else:
    while pt < len(A):
        if Lsearch(A[pt:],num) != -1:
            tmp = Lsearch(A[pt:],num) + 1
            count += tmp - 1
            pt = pt + tmp
            num += 1
        else:
            count += len(A[pt:])
            break

if flag == 0:
    print(count)
else:
    print(-1)

Submission status: Time Limit Exceeded

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

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


for i in range(N-1):
    if A[i] in l or l[c]+1 != A[i]:
        continue

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

print(N+1-len(l))

Submission status: Time Limit Exceeded

Solution code:
####################################
N = int(input())
# スペース区切りの整数の入力
a = list(map(int, input().split()))

idx = 0
out = 0
for i in range(1, N + 1):
    a = a[idx:]
    if i in a:
        idx = a.index(i) + 1
        out += 1
    else:
        break
if out == 0:
    print(-1)
else:
    print(N - out)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
# スペース区切りの整数の入力
a = list(map(int, input().split()))

idx = 0
out = 0
for i in range(1, N + 1):
    if i in a[idx:]:
        idx = a[idx:].index(i) + 1
        out += 1
    else:
        break
if out == 0:
    print(-1)
else:
    print(N - out)

Submission status: Time Limit Exceeded

Solution code:
def hantei(n,data):
    last_num = 1
    if last_num not in data:
        return -1

    while True:
        data = data[data.index(last_num):]
        if last_num + 1 in data:
            last_num += 1
        else:
            break
        #print(data[index_point + 1:])

    return n - last_num

n = int(input())
data = [int(x) for x in input().split()]

print(hantei(n,data))