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:
import sys
sys.setrecursionlimit(10**6)

n = int(input())
m = int(n)
a = list(map(int,input().split()))
cnt = 0
s = 0

def ers(n,m,a,cnt,s):
    if len(a) == 0:
        cnt = -1
        return cnt
    for i in range(s,n):
        if i+1 != a[i]:
            a.pop(i)
            cnt+=1
            n -= 1
            if cnt > m-1:
                cnt = -1
                return cnt
            return ers(n,m,a,cnt,s)
        else:
            s += 1
    return cnt

res = ers(n,m,a,cnt,s)

print(str(res))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
# a = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]

target = 1
res = 0
flag = False

for i in range(N):
	x = a.pop(0)
	if x == target:
		target += 1
		flag = True
	else:
		res += 1

if flag:
	print(res)
else:
	print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int( input() )
A = list( map( int, input().split() ) )
#dp = [ 10**4 for x in range(N) ]
#dp[i] i+1まで数える最短のmonotonically increasing列の位置

for i in range(N+1) :
    if i+1 in A : 
#       dp[i]=A.index(i+1)
       A = A[A.index(i+1)+1:]
    else :
       break
if i == 0:
   print( -1 )
else: 
   print( N - i )

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))
count = 0
k=0

while k<len(A):
    if A[k] == k+1:
            k += 1
    else:
            count += 1
            A.pop(k)
if count == N:
    print(-1)
else:
    print(count)

Submission status: Time Limit Exceeded

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

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

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

if limitNum == 0:
    print(-1)
elif limitNum == 1:
    print(N - 1)
else:
    for i in range(limitNum):
        if maxNum == -1:
            before = [l for l, x in enumerate(a) if x == i + 1]
        else:
            before = [maxNum]
        after = [m for m, x in enumerate(a) if x == i + 2]
        #print(before)
        #print(after)
        #print()
        for j in range(len(before)):
            for k in range(len(after)):
                if before[j] < after[k]:
                    loopFlag = True
                    ans = i + 2
                    if maxNum != -1:
                        if maxNum < after[k]:
                            loopFlag = False
                    maxNum = after[k]
                    if loopFlag == True:
                        break
            if loopFlag == True:
                    break
                    
        else:
            if loopFlag == True:
                loopFlag = False
            else:
                break
    print(N - ans)

Submission status: Time Limit Exceeded

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

for i in range(n):
  if (i+1) not in a:
    break
  idx = a.index(i+1)
  b += 1
  a = a[idx:]

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int( input() )
A = list( map( int, input().split() ) )
dp = [ 10**4 for x in range(N) ]
#dp[i] i+1まで数える最短のmonotonically increasing列の位置

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
idx = [0]
for n in range(1, N+1):
    try:
        idx.append(A[idx[-1]:].index(n) + len(A[:idx[-1]]))
    except:
        break
idx.append(N-1)
if len(idx) == 2:
    print(-1)
else:
    a = [i-j for i,j in zip(idx[1:], idx[:-1])]
    print(sum(a)-(len(idx)-2)+1)

Submission status: Time Limit Exceeded

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


k = 1
s = 0
count = 0


for i in range(len(a)):
    if k in a:
        s = a.index(k)
        a = a[s:]
        k += 1
        count += 1
    else:
        break

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

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

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

def ers(a,cnt,s):
    if len(a) == 0:
        cnt = -1
        return cnt
    for i in range(s,len(a)):
        if i+1 != a[i]:
            a.pop(i)
            cnt+=1
            if cnt > n-1:
                cnt = -1
                return cnt
            return ers(a,cnt,s)
        else:
            s += 1
    return cnt

res = ers(a,cnt,s)

print(str(res))

Submission status: Time Limit Exceeded

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

for i in range(n):
  if (i+1) not in a:
    break
  idx = a.index(i+1)
  b.append(a[idx])
  a = a[idx:]

if b != []: 
  print(n - len(b))
else:
  print(-1)

Submission status: Time Limit Exceeded

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

Submission status: 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):
    for j in range(i+1,N):
        if A[i]>A[j]:
            continue
        else:
            if A[i] in l:
                continue
            if 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(map(int, input().split()))

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

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

if limitNum == 0:
    print(-1)
elif limitNum == 1:
    print(N - 1)
else:
    for i in range(limitNum):
        before = [l for l, x in enumerate(a) if x == i + 1]
        after = [m for m, x in enumerate(a) if x == i + 2]
        #print(before)
        #print(after)
        #print()
        for j in range(len(before)):
            for k in range(len(after)):
                if before[j] < after[k]:
                    loopFlag = True
                    ans = i + 2
                    if maxNum != -1:
                        if maxNum < after[k]:
                            loopFlag = False
                    maxNum = after[k]
                    if loopFlag == True:
                        break
            if loopFlag == True:
                    break
                    
        else:
            if loopFlag == True:
                loopFlag = False
            else:
                break
    print(N - ans)

Submission status: Time Limit Exceeded

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

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

if split == []:
    print(-1)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
def input():
    import sys
    return sys.stdin.readline().rstrip()
  
def main():
  N = int(input())
  a = list(map(int, input().split()))
  index = 0
  break_ = 0
  #ans = []
  while True:
    if index == len(a) or break_ == N - 1:
      break
    elif index + 1 != a[index]: #一致しなかったら壊す
      a.pop(index)
      break_ += 1
    else: #一致したら次
      #ans.append(index + 1)
      index += 1
  #print(index)
  #print(break_)
  #print([i + 1 for i in range(N - break_)])
  #print(ans)
  #print(a)
  if index + break_ != N:
    if N == 1 and a[0] == 1:
      print(0)
    else:
      print(-1)
  else:
    print(break_)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

#m = max(a)
r = -1
c = 1
b = []

while 1:
  
  tmp = a
  a = [a[i] for i in range(len(a)) if a[i] <= i+1]
  
  if len(tmp) == len(a):
    break  
  elif c > 1000000:
    print("finite loop")
    break
  else:
    c = c + 1

#print(a)

if len(a) == 0:
  r = -1
else:
  b = [a[0]]
  for i in range(len(a) - 1):
    if a[i+1] == a[i]:
      pass
    elif a[i+1] > a[i] + 1:
      b.append(-1)
    else:
      b.append(a[i+1])

  #print(b)

  for i in range(len(b)):
    t = i + 1
    if t in b:
      idx = b.index(t)
      b = b[(idx+1):]
      r = t
    else:
      break

if r < 0:
  print(r)
else:
  print(n - r)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
n_index = list(range(1, n+1))

breaks = 0
if min(a_list) > 1:
    print(-1)
else:
    for n in n_index:
        if len(a_list) > 0:
            if n in a_list:
                i = a_list.index(n)
                a_list = a_list[i+1:]
                breaks += i
                breaks += len([q for q in a_list if q <= n])
                a_list = [p for p in a_list if p > n]
            else:
                breaks += len(a_list)
                print(breaks)
                exit()
    print(breaks)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
A = [int(s) for s in A]
count = 1
sum = 0
if 1 not in A:
    print('-1')
else:
    for i in range(N):
        if count in A:#Aにiが含まれてたら、i-1までを砕く（i-1)個
            sum = sum + A.index(count)
            del A[0:A.index(count)+1]
            count = count + 1
        else:
            sum = sum + len(A)
            break
    print(sum)

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):
    tmp = a[idx:]
    if i in tmp:
        idx = tmp.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()))) 
b=[0]

 
for i in range(n):
  if a[0]-b[-1]==1:
    b.append(a.pop(0))
  else:
    a.pop(0)
if n-len(b)+1==n:
  print(-1)
else:
  print(n-len(b)+1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
A=[int(i) for i in input().split()]
  
if 1 not in A:
  ans=-1
else:
  ans=0
  for i in range(1,n+1):
    if i in A:
      k=A.index(i)
      ans+=k
      A=A[k+1:]
    else:
      ans+=len(A)
      break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect
n = int(input())
m = list(map(int,input().split()))
num=1
for i in range(n):
  if num in m:
    t = m.index(num)
    del m[:t]
    num+=1
if num == 1:
  print(-1)
else:
  print(n-num+1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

while A:
  a = A.pop(0)
  if i != a:
    count += 1
  else:
    i += 1
    
print(-1 if count == n else count)

Submission status: Time Limit Exceeded

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

count = 0
max_index = 0
ans = 0

while count <= N:
  count += 1  
  order = [i for i, x in enumerate(a) if x == count]
  
  if len(order) == 0:
    pass
  else:
    order1 = order[-1]+1
    if max_index < order1:
      ans = ans+(order1 - max_index - 1)
      max_index = order1
    else:
      max_index = max_index

if a.count(1) == 0:
  print(-1)
else:
  print(ans)

Submission status: Time Limit Exceeded

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

j = 0
m = 1
for i in range(1, (N + 1)):
    i = m
    if i != a[i - 1]:
        a.pop(i - 1)
        N = N - 1
        j = j + 1
    else:
        m = m + 1

if a:
    print(j)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
l=list(map(int,input().split()))
c=0
pos=0
temp=[]
flag=1
if l.count(1)==0:
    print(-1)
else:
        
    for i in range(1,max(l)+1):
        if i==1:
            
            x=l.index(i)
            temp.append(x)
        else:
            try:
                x=l[temp[-1]::].index(i)+temp[-1]
                temp.append(x)
            except:
    
                break
    
    print(n-len(temp))

Submission status: Time Limit Exceeded

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

cnt = 0
num = 0
b = []
while cnt < n:
  if (cnt+1) not in a:
    break
  idx = a.index(cnt+1)
  b.append(a[idx])
  a = a[idx:]
  num += 1
  cnt += 1

if b != []: 
  print(n - len(b))
else:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
number = list(map(int,input().split()))
if not 1 in number:
    print(-1)
else:
    for i in range(N):
        now = i+1
        if now in number:
            #number.index(now)が左から数えたときのnowのindex番号
            number[:number.index(now)+1]=[]
            if i == N-1:
                count=0

        else:
            count = N-i
            break
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
m = list(map(int, input().split()))
count = []
for i in range(1, n+1):
  if i not in m:
    break
  j = m.index(i)
  count += [i]
  m = m[j+1:]

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

Submission status: Time Limit Exceeded

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

k = 1
s = 0
count = 0


for i in range(n):
    if k in a:
        s = a.index(k)
        a = a[s:]
        k += 1
        count += 1
    else:
        break

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
A = [int(s) for s in A]
count = 1
sum = 0
if 1 not in A:
    print('-1')
else:
    for i in range(N):
        if count in A:#Aにiが含まれてたら、i-1までを砕く（i-1)個
            first_index = A.index(count)
            del A[0:first_index+1]
            sum = sum + first_index
            count = count + 1
        else:
            sum = sum + len(A)
            break
    print(sum)

Submission status: Time Limit Exceeded

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

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

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

if limitNum == 0:
    print(-1)
elif limitNum == 1:
    print(N - 1)
else:
    for i in range(limitNum):
        before = [l for l, x in enumerate(a) if x == i + 1]
        after = [m for m, x in enumerate(a) if x == i + 2]
        #print(before)
        #print(after)
        #print()
        for j in range(len(before)):
            for k in range(len(after)):
                if before[j] < after[k]:
                    loopFlag = True
                    ans = i + 2
                    if maxNum != -1:
                        if maxNum < after[k]:
                            loopFlag = False
                    maxNum = after[k]
                    
        else:
            if loopFlag == True:
                loopFlag = False
            else:
                break
    print(N - ans)

Submission status: Time Limit Exceeded

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

L=len(numbers)
target_number=1
i=0
while L>i+1:
  if(numbers[i]!=target_number):
    numbers.pop(target_number-1)
    L-=1
  elif(numbers[i]==target_number):
    i+=1
    target_number+=1

if(L>1):
  print(N-target_number)
  
elif(L==1):
  if(numbers[0]==1):
    print(N-1)
  elif(numbers[0]!=1):
    print(-1)

Submission status: Time Limit Exceeded

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

m = max(a)
r = -1

for i in range(m):
  t = i + 1
  if t in a:
    pass
  else:
    break

  idx = a.index(t)
  a = a[(idx+1):]
  r = t

if r < 0:
  print(r)
else:
  print(n - r)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
h=list(map(int,input().split()))
mn=min(h)
cnt=0
if(mn!=1):
    print("-1")
    exit()
r=len(h)
for i in range(r-1):
    if i==r:
        break
    for j in range(r-1):
        if(h[i]==i+1):
            break
        else:
            cnt=cnt+1
            del h[i]
            r=len(h)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
total_breaks = 0
position = 0
for i in range(1, n + 1):
    try:
        breaks = a[position::].index(i)
        total_breaks += breaks
        position = position + breaks + 1
    except ValueError:
        if position == 0:
            total_breaks = -1
            break
        else:
            total_breaks = total_breaks + n - position
            break
print(total_breaks)

Submission status: Time Limit Exceeded

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

S=0
ans =-1

if 1 in A:    
    for i in range(1,N+1):
        if i in A[S::]:
            S=A.index(i,S)
            ans =N-i
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

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

k = 1
s = 0
count = 0


for i in range(n):
    if k in a:
        s = a.index(k)
        a = a[s:]
        k += 1
        count += 1
    else:
        break

if 1 in m:
    print(n-(k-1))
else:
    print(-1)

Submission status: Time Limit Exceeded

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

if 1 in A:
    num = 1
    c = len(A)
    while(len(A) > num):
        if A[num-1] == num:
            num += 1
        else:
            del A[num-1]
    print(c - len(A))
else:
    print(-1)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
j = 0
d = 0
c0 = 0
l = sorted(list(set(a)))
for i in l:
    if i == j+1:
        j = i
        if i in a[c0:]:
            for k in range(len(a[c0:])):
                if i == a[c0:][k]:
                    p=k
                    break
            c = p+(n-len(a[c0:]))
        else:break
        if c >= c0:
            d += 1
            c0 = c
        else:break
    else:break
print(n-d if d !=0 else -1)

Submission status: Time Limit Exceeded

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

cnt = 0
num = 0
b = []
idx0 = 999999
while cnt < n:
  try:
    idx = a.index(cnt+1)
  except ValueError:
    num -= 1
    break
  
  b.append(a[idx])
  a = a[idx:]
  num += 1
  cnt += 1

if b != []: 
  print(n - len(b))
else:
  print(-1)

Submission status: Time Limit Exceeded

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

while k<len(A):
    if A[k] == k+1:
            k += 1
    else:
            count += 1
            A.pop(k)
if count == N:
    print(-1)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
 
maxi = A[0]
for  i in range(N):
    if A[i] >= maxi:
        maxi = A[i]
    else:
        maxi = maxi
        
count = 0
position = 0
while position < N:
  for j in range(1,maxi+1):
    for i in range(position,N):
      if A[i] == j:
        count = count + 1
        break
      else:
        position = position + 1
 
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()))
ans=0
i=0
while i+1 <= len(a):
    if a[i]!=i+1:
        del a[i]
    else:
        i+=1
if len(a)==0:
    print(-1)
else:
    print(n-len(a))

Submission status: Time Limit Exceeded

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

is_over = False
count = 0
for i in range(1, n+1):
    # print('i:', i)
    while True:
        # print('    A:', A)
        # print('    count:', count)
        if len(A) == 0:
            is_over = True
            break
        if A[0] == i:
            A = A[1:]
            break
        else:
            A.pop(0)
            count += 1
    if is_over:
        break

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

Submission status: Time Limit Exceeded

Solution code:
def main():
    flg = False
    cnt = 0
    k = int(input())
    a = input().split(" ")
    an = [0] * k
    for i in range(k):
        an[i] = int(a[i])
    oldtmp = 0
    newtmp = 0
    for i in range(k-1):
        oldtmp = newtmp
        try:
            newtmp = an.index(i+1)
            if newtmp < oldtmp:
                if an.count(i+1) >= 1:
                    tmp2 = [j for j, x in enumerate(an) if x == i+1]
                    for j in tmp2:
                        if oldtmp < j:
                            flg = True
                            cnt += 1
                            newtmp = j
                if not flg:
                    break
            else:
                cnt += 1
            flg = False
        except ValueError:
            break
    if cnt == 0 and an[0] != 1:
        res = -1
    elif cnt == 0 and an[0] == 1:
        res = 0
    else:
        res = k-cnt
    print(res)


main()

Submission status: Time Limit Exceeded

Solution code:
import sys
N = int(input())
a_list = list(map(int, input().split())) #[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 1]

ans = 0
value = 1


try:
    idx = a_list.index(value)
    ans += a_list.index(1)
    a_list = a_list[idx:]
    value += 1
except:
    print(-1)
    sys.exit(0)

while True:
    try:
        idx = a_list.index(value)
        ans += idx - 1
        a_list = a_list[idx:]
        value += 1
    except:
        ans += len(a_list) - 1
        break

print(ans)

Submission status: Time Limit Exceeded

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

i = 0
r = 0

while i<len(a):
    if a[i] == i+1:
        i += 1
    else:
        a.pop(i)

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

cnt = 0
n = 1
i = 0

while(True):
    if a[i] != n:
        a.pop(i)
        cnt += 1
    else:
        n += 1
        i += 1

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

    if i >= len(a):
        break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
data = input().split(' ')
n = 1
key = 0
while(str(n) in data):
    key = 1
    data = data[data.index(str(n)):]
    n += 1
if(key == 0):
    print(-1)
else:
    print(num - n + 1)

Submission status: Time Limit Exceeded

Solution code:
N, *L,  = [list(map(int, s.split())) for s in open(0)]
L = L[0]
i = 0
l = N[0]
cnt = 0
while l > 0 and l - 1 >= i:
    if L[i] != i + 1:
        L.pop(i)
        cnt += 1
    else:
        i += 1
    l = len(L)
if cnt == N[0]:
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = input().split()
A = [int(s) for s in A]
count = 1
sum = 0
if 1 in A:
    for i in range(N):
        if count in A:#Aにiが含まれてたら、i-1までを砕く（i-1)個
            first_index = A.index(count)
            del A[0:first_index+1]
            sum = sum + first_index
            count = count + 1
        else:
            sum = sum + len(A)
            break
    print(sum)
else:
    print('-1')

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
j = 0
d = 0
c0 = 0
l = sorted(list(set(a)))
for i in l:
    if i == j+1:
        j = i
        if i in a[c0:]:
            c = a[c0:].index(i)+(n-len(a[c0:]))
        else:break
        if c >= c0:
            d += 1
            c0 = c
        else:break
    else:break
print(n-d if d !=0 else -1)

Submission status: Time Limit Exceeded

Solution code:
# 21:19
n = int(input())
a = list(map(int,input().split()))
ans = 0
count = 1
i = 0
while True:
    if a[i] != count:
        ans += 1
        a.pop(i)
    else:
        count += 1
        i += 1
    if n == ans + i:
        break
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()))
cnt = 0
i = 1

while a:
    # print(a)
    for _ in range(len(a)):
        # print(i, a, cnt, a[0] != i)
        if a[0] != i:
            cnt += 1
            a.pop(0)
        else:
            a.pop(0)
            i += 1
            break
if cnt == n:
    print(-1)
else:
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python

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

flag = False
a_copy = a
num = 0
i = 0
while True:
    num += 1
    if num in a_copy:
        if num == 1:
            flag = True
        # print(num, 'is in a_copy', a_copy.index(num))
        a_copy = a_copy[a_copy.index(num):]
        # print('a_copy', a_copy)
    else:
        # print('break')
        # print('num = ', num)
        break

if flag:
    print(len(a) - (num - 1))
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
number = list(map(int,input().split()))
count = 0
if not 1 in number:
    print(-1)
else:
    for i in range(N):
        now = i+1
        if now in number:
            indexnumber = number.index(now)
            #number.index(now)が左から数えたときのnowのindex番号
            count += indexnumber
            number[:indexnumber+1]=[]

        else:
            count += len(number)
            break
    print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# submission # - User: herp_sy
# https://atcoder.jp/contests/
#
# lang: Python3 (3.4.3)

import math
import statistics
import numpy as np
import queue

n = int(input())
a = list(int(i) for i in input().split())
p = 0
err_avoid = 0

a.append(-1)
for i in range(n):
  if a[p] == -1:
    break
  elif p != (a[p] - 1):
    err_avoid = a.pop(p)
  else:
    p += 1

if len(a) == 1 and a[0] == -1:
  print(a.pop())
else:
  print(n - len(a) + 1)

Submission status: Time Limit Exceeded

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


N = int(input())
A = list(map(int,input().split()))
B = []
count = 0
m = my_index(A,1)
if m == -1:
    print(-1)
else:
    B.append(m)
    for i in range(1,N):
        m = my_index(A[B[-1]+1:],i+1)
        if m !=-1:
            B.append(m)
        else:
            break

    k = len(A) - len(B)
    print(k)

Submission status: Time Limit Exceeded

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

if 1 in a:
    ind=a.index(1)
    stop=1
    for i in range(2,N+1):
        if i in a[ind+1:]:
            ind+=a[ind+1:].index(i)+1
            continue
        else:
            stop=i-1
            break
    print(N-stop)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import math


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


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


def main():
    N = inputInt()
    a = inputIntList()
    ans = 0
    index = 0
    while index < len(a):
        if a[index] - 1 != index:
            a.remove(a[index])
            ans += 1
        else:
            index += 1
    return ans if ans != N else -1


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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
aL = list([int(i) for i in input().split()])
i = 0
while True:
    if i == len(aL):
        break
    else:
        if aL[i] == i+1:
            i += 1
        else:
            del aL[i]

L = len(aL)
flag = 0
for i in range(L):
    if aL[i] != i+1:
        flag = 1

if flag == 1 or L == 0:
    print(-1)
else:
    print(N-L)

Submission status: Time Limit Exceeded

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

while True:
    if A[k] == k+1:
            k += 1
    else:
            count += 1
            A.pop(k)
    if k == len(A):
        break
if count == N:
    print(-1)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
n_index = list(range(1, n+1))

breaks = 0
if min(a_list) > 1:
    print(-1)
else:
    for n in n_index:
        if len(a_list) > 0:
            if n in a_list:
                i = a_list.index(n)
                a_list = a_list[i+1:]
                breaks += i
            else:
                breaks += len(a_list)
                print(breaks)
                exit()
    print(breaks)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = input().split()
j=0
c=0
for i in range(len(a)):
    if(int(a[j])!=j+1):
        del(a[j])
        c+=1
    else:
        j+=1
if(n==c):
    print(-1)
else:
    print(c)