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()))
b = list()
count= -1
def my_index(l, x, default=-1):
  if x in l:
    return l.index(x)
  else:
        return default
for i in range(1,N+1):
  now = my_index(a,i)
  if now != -1:
    count = i
    a = a[now + 1:]
  else:
    break
if count == -1:
  print(-1)
else:
  print(N-count)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
li = list(map(int,input().split()))
if li.count(1)==0:
    print(-1)
else:
    del li[0:li.index(1)]
    for k in range (2,n+1):
        if li.count(k)==0:
            del li[k-1:]
            break
        else:
            del li[k-1:li.index(k)]
    print(n-len(li))

Submission status: Time Limit Exceeded

Solution code:
import sys
n = int(input())
a = list(input().split())
b =list(map(int,a))
aa = list(set(b))
x = []
y = []
c = 1
for i in range(1,n+1):
    x.append(i)
for j in aa:
    if j in x and 1 in aa:
        pass
    else:
        print("-1")
        sys.exit()
for k in a:
    if int(k) == c:
        y.append(k)
        c = c + 1
print(len(a)-len(y))

Submission status: Time Limit Exceeded

Solution code:
from sys import exit

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

try:
    k = a.index(1)
except:
    print(-1)
    exit()

while k < N:    
    try:
        k = a[k+1:].index(num) 
    except:
        break
    num += 1  
print(N-num+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
input_nums = list(map(int, input().split()))
search_num = 1
flag = False

for i in range(N):
    try:
        search_num_index = input_nums.index(search_num)
    except:
        search_num_index = -1
        
    if(search_num_index is not -1):
        del input_nums[0:search_num_index + 1]
        search_num += 1
        flag = True

if(search_num == 1 and flag == False):
    print(-1)
elif(search_num == 1 and flag == True):
    print(0)
else:
    print(N - search_num + 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:
        continue
    if 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())
bricks = [int(a) for a in input().split()]

result = 0
numbers = []
for i in range(1, n + 1):
  try:
    number_index = bricks.index(i)
    numbers.append(i)
    result = result + number_index
    del bricks[0:number_index+1]
  except:
    if len(numbers) != 0:
      result = result + len(bricks)
    break

if len(numbers) == 0:
  print(-1)
else:
  print(result)

Submission status: Time Limit Exceeded

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

    while True:
        if last_num + 1 in data[data.index(last_num):]:
            last_num += 1
            data = data[data.index(last_num):]
        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))

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:
n = int(input())
c = list(map(int, input().split()))
ln = []
if c.count(1) == 0:
    print(-1)
else:
    c2 = c
    for i in range (1, max(c)+1):
        if i in c2 :
            s = c2.index(i)
            ln.append(i)
            del c2[:s + 1]
            if len(c2) == 1 and  i + 1 in c2:
                    ln.append(i + 1)
        else:
            break
    print(n - max(ln))

Submission status: Time Limit Exceeded

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

x = 0
i = 0

while i < len(a):
    if a[i] != i+1:
        a.pop(i)
        x+=1
        i-=1
    i+=1
    
print(x if x != n else -1)

Submission status: Time Limit Exceeded

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

S=0
ans =-1
B=A

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

print(ans)

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

l = list(range(1, 200001))
count = 0
for i in range(n):
    if a[i] == l[0]:
        l.remove(l[0])
        count += 1

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)
    exit()

for i in range (1, 200000):
    if i not in A:
        break
    t=A.index(i)
    A=A[t+1::]

print(N-i+1)

Submission status: Time Limit Exceeded

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


def main():
    N = int(input())
    a = [int(i) for i in input().split()]
    b = []

    ans = -1
    tmp_index = 0
    for i in range(N):
        if index(a[tmp_index:], i + 1) is None:
            break
        else:
            tmp_index = index(a[tmp_index:], i + 1)
            b.append(i + 1)

    if len(b) == 0:
        print(ans)
        return None
    for i, e in enumerate(b):
        if e != i + 1:
            break
    else:
        ans = N - len(b)

    print(ans)


if __name__ == "__main__":
    main()

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

if k in a:
    s = a.index(k)
    a = a[s:]
    k += 1

if k in a:
    s = a.index(k)
    a = a[s:]
    k += 1


for i in range(len(a)):
    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()))

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()))
count=0

if 1 not in a:
    count=-1
else:
    for i in range(1,N):
        if len(a) == 1:
            break
        while a[0]!=i:
            count+=1
            a.pop(0)
            if len(a) == 1:
                if a[0]!=i:
                    count+=1
                break
        if len(a) == 1:
            break
        a.pop(0)
print(count)

Submission status: Time Limit Exceeded

Solution code:

n = int(input())
a = list(map(int,input().split(' ')))
hi = len(a)
if not  1 in a:
    print(-1)
else:
    for i in range(1, hi+1):
        if i in a:
            r = a.index(i)
            a = a[r:]
        else:
            print(hi - (i-1))
            break
    else:
        print(0)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
c = 0
if 1 not in a:
    print(-1)
    exit()
i = 0
for nn in range(1, n + 1):
    if nn in a[i:]:
        ind = a[i:].index(nn)
        if ind == 0:
            i += 1
            continue
        else:
            c += len(a[i:i + ind])
            i += ind + 1
    else:
        c +=  len(a[i:])
        break
print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

for i in range(N):
    try:
        num = a.index(i + 1)
        a = a[num + 1:]
    except ValueError:
        if i == 0:
            print(-1)
        else:
            print(N - i)
        sys.exit()

print(0)

Submission status: Time Limit Exceeded

Solution code:
from sys import exit

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

try:
    k = a.index(1)
except:
    print(-1)
    exit()

while k<N:    
    try:
        k = a[k+1:].index(num) 
    except:
        break
    num += 1  
print(N-num+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
# chech with no break
finish = False
def check(A, cnt=0):
  N = len(A)
  #print(A)
  for i, a in enumerate(A):
    if a != N and i != N - 1:
      continue
    if a == N and i != N - 1:
      return False
    if A[N - 1] == N:
      #print("debug")
      if cnt == 0:
        print(0)
      return True
  return False
finish = check(A)
cnt = 0
while finish is False:
  # Miss break from the large number
  #m = max(A)
  #i = A.index(m)
  i = 0
  if finish is False and len(A) > 1:
    A.pop(i) # remove
    cnt += 1
    finish = check(A, cnt)
  else:
    break
  i += 1
if finish is True and cnt != 0:
  print(cnt)
if finish is False:
  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:
        continue
    if 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] not in l:
                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()))

if(1 in a == False):
  print(-1)
  exit()
if(a == list(map(lambda x: x+1, range(len(a))))):
  print(0)
  exit()

count = 0
for i in range(n):
  if(i+1 in a == False):
    break
  if(i < len(a)):
    while(len(a) > i and a[i] != i+1):
      a.pop(i)
      count += 1
 
if count == 0 or len(a) == 0:
  print(-1)
else:
  print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    a = int(input())
    b = list(map(int, input().split()))
    l = []
    h = 0
    z = 0
    break_loop = False
    if a == 1:
        if b[0] == 1:
            print(0)
        else:
            print(-1)
    else:
        for i in range(a):
            for j in range(z+1, a):
                l.append(i+1 == b[j])
                if l.count(True) > 0:
                    z = j
                    h += 1
                    l = []
                    break
                elif j == a-1 and l.count(True) == 0:
                    break_loop = True
                    break
            if break_loop:
                break
        if break_loop:
            print(-1)
        else:
            print(a-h)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
n_index = list(range(1, n+1))
a_list_dash = sorted(a_list)
d_list = [a_list_dash[i+1] - a_list_dash[i] for i in range(len(a_list_dash)-1)]
k = [idx for idx,val in enumerate(d_list) if val>=2]
if len(k) > 0:
    trymax = a_list_dash[k[0]] +1
else:
    trymax = len(n_index)

breaks = 0
if min(a_list) > 1:
    print(-1)
else:
    for n in range(1, trymax+1):
        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 = list(map(int, input().split()))
# chech with no break
finish = False
def check(A, cnt=0):
  N = len(A)
  #print(A)
  for i, a in enumerate(A):
    if a != N and i != N - 1:
      continue
    if a == N and i != N - 1:
      return False
    if A[N - 1] == N:
      #print("debug")
      if cnt == 0:
        print(0)
      return True
  return False
finish = check(A)
cnt = 0
while finish is False:
  # Miss break from the large number
  #m = max(A)
  #i = A.index(m)
  i = 0
  if len(A) > 1:
    A.pop(i) # remove
    cnt += 1
    finish = check(A, cnt)
  else:
    break
  i += 1
if finish is True and cnt != 0:
  print(cnt)
if finish is False:
  print(-1)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a_list=list(map(int,input().split()))
MAX=max(a_list)
counter=0
if 1 not in a_list:
    print(-1)
else:
    for i in range(MAX):
        if 1+i in a_list:
            del_n=a_list.index(1+i)
            del a_list[0:del_n]
            counter+=1
        else:
            break
    print(N-counter)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
c = 0
if 1 not in a:
    print(-1)
    exit()
i = 0
for nn in range(1, n + 1):
    if a[i:] == range(nn,n - c + 1):
        break
    if nn in a[i:]:
        ind = a[i:].index(nn)
        if ind == 0:
            i += 1
            continue
        else:
            c += len(a[i:i + ind])
            i += ind + 1
    else:
        c +=  len(a[i:])
        break
print(c)

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[sum(idx):].index(n))
    except:
        idx.append(len(A[sum(idx):]))
        break
if N == A[0] == 1:
    print(0)
elif (N == 1 and A[0] != 1) or (len(idx) == 2):
    print(-1)
else:
    print(sum(idx)-len(idx)+2)

Submission status: Time Limit Exceeded

Solution code:
import sys
n=int(input())
hantei=0
sli = list(map(int,input().split()))
count=0
while(hantei==0):
    for i in range(len(sli)):
        if not(sli[i]==i+1):
            del sli[i]
            count=count+1
            break
    
    for i in range(len(sli)):
        hantei=1
        if not(sli[i]==i+1):
            hantei=0
            break
    if(len(sli)==0):
        print(-1)
        sys.exit()
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
while 1:
    if i >= len(a):
        break
    if a[i] != i+1:
        a.pop(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 = list(map(int, input().split()))

if(1 in a == False):
  print(-1)
  exit()
if(a == list(map(lambda x: x+1, range(len(a))))):
  print(0)
  exit()

count = 0
for i in range(n):
  if(i < len(a)):
    while(len(a) > i and a[i] != i+1):
      a.pop(i)
      count += 1
 
if count == 0 or len(a) == 0:
  print(-1)
else:
  print(count)

Submission status: Time Limit Exceeded

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

del_count = 0
success = False
while True:
    for j, n in enumerate(A):
        if j+1 != n:
            del_count += 1
            A.pop(j)
            break
    if not A:
        break
    if A == list(range(1, len(A)+1)):
        success = True
        break

if success:
    print(del_count)
else:
    print(-1)

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)
i=-1
while(True):
    i=i+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=r-1
print(cnt)

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 :
       if i == 0: 
          print( -1 )
       else:    
          print(N-i)
       exit()

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)
i=-1
while(True):
    i=i+1
    if i==r:
        break
    if(h[i]!=i+1):
        cnt=cnt+1
        del h[i]
        r=r-1
        i=i-1
print(cnt)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

Solution code:
a = int(input())
b = list(map(int, input().split()))
l = []
h = 0
z = 0
break_loop = False
if a == 1:
    if b[0] == 1:
        print(0)
    else:
        print(-1)
else:
    for i in range(a):
        for j in range(z+1, a):
            l.append(i+1 == b[j])
            if l.count(True) > 0:
                z = j
                h += 1
                l = []
                break
            elif j == a-1 and l.count(True) == 0:
                break_loop = True
                break
        if break_loop:
            break
    if break_loop:
        print(-1)
    else:
        print(a-h)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
a_list=list(map(int,input().split()))
counter=0
if 1 not in a_list:
    print(-1)
else:
    for i in range(N):
        if 1+i in a_list:
            del_n=a_list.index(1+i)
            del a_list[0:del_n]
            counter+=1
        else:
            break
    print(N-counter)

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(len(a)):
  t = i + 1
  try:
    idx = a.index(t)
    a = a[(idx+1):]
    a = [i for i in a if i != t]
    r = t
  except:
    break

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
ans = -1
cnt = 0
i = 1
k = 0

while(1):
    if i in a[k:N]:
        k = a[k:N].index(i)
        i += 1
        cnt += k
        k = cnt + i - 1
    else:
        cnt += N - k
        if k != 0:
            print(cnt)
        else:
            print(ans)
        exit()

Submission status: Time Limit Exceeded

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

bucket = [0] * N
del_count = 0
success = False
while True:
    for j, n in enumerate(A):
        if j+1 != n:
            del_count += 1
            del A[j]
            break
    if not A:
        break
    if A == list(range(1, len(A)+1)):
        success = True
        break

if success:
    print(del_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:
        continue
    for j in range(i+1,N):
        if A[i]>A[j]:
            if A[j] not in l:
                continue
        else:
            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:
import numpy as np
N = int(input())
a = np.array([int(s) for s in map(int, input().split())])

i = 0
j = 1
k = 0
n = 0
    
if len(np.where(a[k:]==j)[0]) == 0:
  n = -1
  i = 1

while i == 0:
  l = len(np.where(a[k:]==j)[0])
  if l != 0:
    k = np.where(a[k:]==j)[0][0] + 1
    j += 1
  else:
    i = 1  

  if len(a[k:]) == 0:
    i = 1

if n == -1:
  print(n)
else:
  print(N-j+1)

Submission status: Time Limit Exceeded

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

target_number=1
i=0

while len(numbers)>i+1:
  if(target_number in numbers):
    del numbers[i:numbers.index(target_number)]
    i+=1
    target_number+=1
  else:
    del numbers[i:]
  
if(len(numbers)>1):
  print(N-len(numbers))
  
elif(len(numbers)==1):
  if(numbers[0]==1):
    print(N-1)
  elif(numbers[0]!=1):
    print(-1)
    
elif(len(numbers)==0):
  print(-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:
            #sum = sum + A.index(count)
            del A[0:A.index(count)+1]
            count = count + 1
        else:
            #sum = sum + len(A)
            break
    print(N - count + 1)

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)
i=-1
while(True):
    i=i+1
    if i==r:
        break
    if(h[i]!=i+1):
        cnt=cnt+1
        del h[i]
        r=len(h)
        i=i-1
print(cnt)

Submission status: Time Limit Exceeded

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

result = 0
numbers = []
for i in range(1, n + 1):
  try:
    number_index = bricks.index(i)
    numbers.append(i)
    result = result + number_index
    for j in range(number_index + 1):
      bricks.pop(0)
  except:
    if len(numbers) != 0:
      result = result + len(bricks)
    break

if len(numbers) == 0:
  print(-1)
else:
  print(result)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

if a == [i + 1 for i in range(N)]:
    print(0)
    sys.exit()

# 1以上に関して
for i in range(N):
    try:
        num = a.index(i + 1)
        a = a[num + 1:]
    except ValueError:
        if i == 0:
            print(-1)
        else:
            print(N - i)
        break

Submission status: Time Limit Exceeded

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

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

ans = N - i + 1
if i == 1:
  ans = -1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
num = int(input())
s = input().split()
s = [int(i) for i in s]
 
count = 0
 
for i in range(num):
    if s[i]==1:
        f_point=i
        count=1
    for j in range(i+1,num):
        if s[j]==count+1:
            count = count+1
if count == 0:
    print(-1)
else:
    print(len(s)-count)

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:
n=int(input())
h=list(map(int,input().split()))
mn=min(h)
cnt=0
if(mn!=1):
    print("-1")
    exit()
r=len(h)
i=-1
while(True):
    i=i+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 = input().split(" ")
a = [int(i) for i in a]

#m = max(a)
r = -1

for i in range(len(a)):
  t = i + 1
  try:
    idx = a.index(t)
    a = a[(idx+1):]
    r = t
  except:
    break

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

Submission status: Time Limit Exceeded

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

c = [0] * (n+1)
j = 0
if 1 not in s:
    print(-1)
elif len(s) == 1:
    print(0)
else:
    for i in range(1,n+1):
        if i in s:
            c[i] = s.index(i)
            del s[j:c[i]]
            j += 1
            #print(s)
        elif i not in s:
            break
    print(n - j)

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
  else:
    break

if num == 1:
  print(-1)
else:
  print(n-num+1)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
List_Bricks = [int(i) for i in input().split()]
#print(List_Bricks)
Search = 1
Kudaku = 0
i = 0
Boole = False
Count = 0
while len(List_Bricks) != 0:
    if Boole:
        Search = Search + 1
        Boole = False
    elif List_Bricks[0] != Search:
        del List_Bricks[0]
        Kudaku += 1
        Boole = False
        #print(List_Bricks)
    elif List_Bricks[0] == Search:
        del List_Bricks[0]
        Boole = True
        Count = Count + 1
        #print(List_Bricks)
if Count == 0:
    print(-1)
else:
    print(Kudaku)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
a = np.array([int(s) for s in map(int, input().split())])

i = 0
j = 1
k = 0
n = 0
    
if len(np.where(a[k:]==j)[0]) == 0:
  n = -1

while i == 0:
  l = len(np.where(a[k:]==j)[0])
  if l != 0:
    k = np.where(a[k:]==j)[0][0]
    j += 1
  else:
    i = 1  

  if len(a[k:]) == 0:
    i = 1

if n == -1:
  print(n)
else:
  print(N-j+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 = 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
print(ers(n,m,a,cnt,s))

Submission status: Time Limit Exceeded

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

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:
n = int(input())
s = list(map(int, input().split(" ")))

c = [0] * (n+1)
j = 0
if 1 not in s:
    print(-1)
elif len(s) == 1:
    print(0)
else:
    for i in range(1,n+1):
        if i in s:
            c[i] = s.index(i)
            del s[j:c[i]]
            j += 1
            #print(s)
        elif i not in s:
            if len(s) == j:
                break
            else:
                while len(s) != j:
                    s.pop()
    print(n - len(s))

Submission status: Time Limit Exceeded

Solution code:
import sys
count=0

def aui(sli):
    global count
    for i in range(len(sli)):
        if not(sli[i]==i+1):
            del sli[i]
            count=count+1
            return 0

n=int(input())
hantei=0
sli = list(map(int,input().split()))
while(hantei==0):
    aui(sli)
    for i in range(len(sli)):
        hantei=1
        if not(sli[i]==i+1):
            hantei=0
            break
    if(len(sli)==0):
        print(-1)
        sys.exit()
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():
    flg = False
    cnt = 0
    k = int(input())
    a = tuple(input().split(" "))
    an = [0]*k
    for i in range(len(a)):
        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)


if __name__ == '__main__':
    input = sys.stdin.readline
    main()

Submission status: Time Limit Exceeded

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