Problem:


Score : 400 points

Problem Statement

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

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 200000

1 \leq a_i \leq N

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

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

Sample Input 1

3
2 1 2

Sample Output 1

1

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

Sample Input 2

3
2 2 2

Sample Output 2

-1

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

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

7

Sample Input 4

1
1

Sample Output 4

0

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



Sample input:
3
2 1 2


Sample output:
1

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
import itertools#これは色々使える組み込み関数みたいなやつ
#import math#数学的計算はこれでいける。普通に0.5乗しても計算可能
#w=input()
i=0
N=int(input())
#N, L=input().split()
#S=input()
#T=input()
#S_list=list(S)
a_list=  list(map(int, input().split(" ")))
j=0
times=0
for i in range(N):
# print(i,j,times, a_list)

 if a_list[j]==j+1:
     j=j+1
     if j==len(a_list):
         print(times)
         sys.exit()

 #if a_list[i]==i+1:
 #i=j
 #print(i, a_list)
 #if i+1==len(a_list):
#    print(times)
#    sys.exit()
 if a_list[j]!=j+1:
     a_list.pop(j)
     #j=i
     #i=j
     times+=1
     if len(a_list)==0:
      print(-1)
      sys.exit()
     #elif j+1==len(a_list):
     #print(times)
    #     sys.exit()

#     elif
print(times)

Submission status: Runtime Error

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

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



print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
X = tuple(map(int, input().split()))

Y = X[::-1]

li = set(range(N))

for k in li:
    print(Y)
    try:
        i = Y.index(k+1)
        Y = Y[:i]
    except:
        i = -1
        leng = k
        break
    leng = k + 1

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

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=[[]for i in range(max(l)+1)]
for i in range(n):
    ans[l[i]].append(i+1)

    
c=1
tem=[]
ch=0
while 1:
    if len(ans[c])==0:
        break
    else:
        flag=0
        for i in ans[c]:
            if i>ch:
                tem.append(i)
                ch=i
                flag=1
                break
        if not flag:
            break
        else:
            c+=1
                
                
if len(tem)==0:
    print(-1)
else:
    print(n-len(tem))

Submission status: Runtime Error

Solution code:
n=int(input())
a = list(map(int, input().split()))
i = 0
crash_count = 0
while True:
  print(a)
  if i >= len(a)-2:
    break
  if a[i] >= a[i+1]:
    del a[i]
    crash_count+=1
    if (i>=1): i-=1
    continue
  else:
    i+=1
for offset,item in enumerate(a):
  if offset+1 != item:
    print('-1')
    exit()
print(str(crash_count))

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=[[]for i in range(n+1)]
for i in range(n):
    ans[l[i]].append(i+1)

    
c=1
tem=[]
ch=0
while 1:
    if len(ans[c])==0:
        break
    else:
        flag=0
        for i in ans[c]:
            if i>ch:
                tem.append(i)
                ch=i
                flag=1
                break
        if not flag:
            break
        else:
            c+=1
                
                
if len(tem)==0:
    print(-1)
else:
    print(n-len(tem))

Submission status: Runtime Error

Solution code:
kazu=int(input())
mono=input().split
n=0
cnt=1
prev=0
br=-1
for n in range(kazu):
  if mono[n]==str(cnt):
    if br==-1:
      br=0
    br=br+n-prev
    prev=n
    cnt=cnt+1
print(str(br))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:

from sys import stdin

n = int(stdin.readline().rstrip())
L = stdin.readline().rstrip()

j = 1
for i in range(n):
 if int(L[i]) == j:
  j = j + 1

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

Submission status: Runtime Error

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

tmp = a_list[n-1]
max_num = tmp
for i in range(n-1, -1, -1):
    if a_list[i] == tmp - 1:
        tmp -= 1

if tmp > 1:
    a_max = max(a_list)
    a_index = a_list.index(a_max)
    max_tmp = a_max
    for i in range(a_max, -1, -1):
        if a_list[i] ==  max_tmp - 1:
            max_tmp -= 1

    if max_tmp > 1:
        print(-1)
    else:
        print(n - a_max)
else:
    print(n - max_num)

Submission status: Runtime Error

Solution code:
num, 
bricks,
count, 
smash = int(input()), [int(x) for x in input().split(" ")], 1, 0
for x in bricks:
    if count == x: 
      count = 1
    else: 
      smash += 1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = input().split()
k = 1
try:
 for _ in range(200)
  #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: Runtime Error

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:]
       print(A, i)
    else :
       break
if i == 0:
   print( -1 )
else: 
   print( N - i )

Submission status: Runtime Error

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

i = 1
while i <= len(arr):
  while arr[i - 1] != i and i <= len(arr):
    arr.pop(i - 1)
  if arr[i - 1] == i:
    if i == len(arr):
      print(N - i)
      exit(0)
    else:
      i += 1
  else:
    print(-1)
    exit(0)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

def brickblock(lst, flag=False, target=1, res=0):
	if not lst:
		if flag:
			return res
		else:
			return -1
	elif lst[0] == target:
		return brickblock(lst[1:], True, target+1, res)
	else:
		return brickblock(lst[1:], flag, target, res+1)

res = brickblock(a)
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
n = input()    
m = n.split()
p = [int(i) for i in m]
j = 1
for i in range(N):
    if p[i] == j:
        j = j+1
if j = 1:
    print("-1")
else:
    print(N - j + 1)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
num, bricks, count, smash = int(input()), [int(x) for x in input().split("")], 1, 0
for x in bricks:
  if count == x: count += 1
  else: smash +=1
print(smash if smash != len(bricks) else 0 if len(bricks) == 1 else -1)

Submission status: Runtime Error

Solution code:
str = [input() for i in range(2)]
num = int(str[0])-1
A = [int(i) for i in str[1].split(" ")]
count = 0
i = 0
while i <= num:
    while A[i] != i+1:
        if i > num:
            break
        A.pop(i)
        num -= 1
        count += 1
    i += 1

if A == []:
    count = -1
print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
li = list(map(int,input().split()))
count = 0
ind = 0
try:
	if(count == n or n>200000 or len(li)>n):
   	li[2*n]
  while True:
   if(li[ind] == ind+1):
   	if(ind==len(li)-1):
   		break
   	ind+=1
   else:
   	count+=1
   	li.pop(ind)
except:
	print(-1)
else:
	print(count)

Submission status: Runtime Error

Solution code:
N = int(input())
l = (map(int,input().split()))
num = len(l)
l_first = min(l)
l_min = min(l)
x = l.index(l_min)

for _ in range(N):
    l = l[x:]
    if l_min+1 in l:
        l_min +=1
        x = l.index(l_min)
    else:
        break

if l_first == l_min:
    print(-1)
else:
    print(num-(l_min-l_first+1))

Submission status: Runtime Error

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

def brickblock(lst, acm=[], target=1, res=0):
	if len(lst) < 1:
		if len(acm)>0:
			return res
		else:
			return -1
	elif lst[0] == target:
		return brickblock(lst[1:], acm+[target], target+1, res)
	else:
		return brickblock(lst[1:], acm, target, res+1)

res = brickblock(a)
print(res)

Submission status: Runtime Error

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

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

Solution code:
n = int(input())
c = list(map(int, input().split()))
ln = []
if c.count(1) == 0:
    print(-1)
elif n == 1 and c == [1]:
    print(0)
if c.count(1) != 0 and n != 1:
    c2 = c
    for i in range (1, max(c)):
        if i in c2 :
            s = c2.index(i)
            ln.append(i)
            del c2[:s + 1]
            if len(c2) == 1:
                if i + 1 in c2:
                    ln.append(i + 1)
        else:
            break
    print(n - max(ln))

Submission status: Runtime Error

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

Solution code:
import sys

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

count = 0
if n == 1 and l[0] == 1:
  print(1)
  sys.exit()

for i in range(1, n):
  try:
    tmp = l.index(i)
    count += tmp - i + 1
    l = list(range(1,i)) + l[tmp:]
    if len(l) == i:
      print(count)
      sys.exit()
  except TypeError:
    print(-1)
    sys.exit()

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split(' ')))
if not  1 in a:
    print(-1)
else:
    r = a.index(1)
    r_ = 0
    if len(a) == 1:
        print(0)
    else:
        for i in range(2, len(a)):
            r_ = a.index(i)
            if r > r_:
                print(len(a)-i)
                break
            r = r_
        else:
            print(0)

Submission status: Runtime Error

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,i+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: Runtime Error

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

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default
cnt = 0
d = 0
for b in range(n):
  d = my_index(a, b+1, -1)
  if d == -1 and cnt == 0:
    cnt = -1
    break
  if d != -1:
    cnt += a.index(b+1)
    a = a[(a.index(b+1)+1)::]
  print(a)
  if len(a) < 1:
    break;
print(cnt)

Submission status: Runtime Error

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

cur=1
cnt=0
for x in xs:
    if x == cur:
        cnt += 1
        cur += 1

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

Submission status: Runtime Error

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

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

flag = False
if 1 in a:
    flag = True

a_copy = a
num = 0
i = 0
while True:
    num += 1
    if num in a_copy:
        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: Runtime Error

Solution code:
N=int(input())
A=[int(input()) for i in range(N)]
l=[]
count=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]:
            count+=1
            print(A[i],A[j])
            break

print(count)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import sys

input = sys.stdin.readline


def f(l, c, s, n):
    if not l:
        if s == n:
            return -1
        return s

    if l[0] != c:
        return f(l[1:], c, s + 1, n)
    return f(l[1:], c + 1, s, n)

def main():
    n = int(input()[:-1])
    l = list(map(int, input()[:-1].split(" ")))

    c = 1
    s = 0
    tmp = 0
    while not l:
        if l[tmp] != c :
            s += 1
        else:
            c += 1
        tmp += 1

    res = s
    if s == n:
        res = -1

    print(f(l, 1, 0, n))

main()

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]*n
a=list(map(int,input().split()))
num=1
bre=0
no=True
def search(pos):
    spos=pos
    global num,bre,no
    end=True
    while end:
        if spos >= n:
            break
        if a[spos]==num:
            no=False
            num+=1
            search(spos+1)
            end=False
        else:
            spos+=1
            bre+=1

search(0)
if no:
    bre=-1
print(bre)

Submission status: Runtime Error

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

c = 1
cp = 0

for i in N:
  ai = an[i]
  if ai != c:
    cp += 1
    
  elif ai == c:
    c += 1
    
print(cp)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    ll N; cin >> N;
    vector <ll> a(N + 1); for (int i = 1; i < N + 1; ++i) cin >> a[i];
    ll dp[200000 + 1][2];
    dp[0][0] = 0;
    dp[0][1] = 0;

    // 0 => 砕く, 1 => 砕かない

    for (int i = 1; i < N + 1; ++i) {
        // 砕く
        dp[i][0] = max(dp[i-1][0], dp[i-1][1]);
        // 砕かない
        if (dp[i-1][0] + 1 == a[i] || dp[i-1][1] + 1 == a[i]){
            dp[i][1] = a[i];
        }
        else {
            dp[i][1] = max(dp[i-1][0], dp[i-1][1]);
        }
    }
    // for (int i = 0; i < N + 1; ++i) {
    //     cout << dp[i][0] << " ";
    // }
    // cout << endl;
    // for (int i = 0; i < N + 1; ++i) {
    //     cout << dp[i][1] << " ";
    // }
    if (max(dp[N][0], dp[N][1]) == 0) {
        cout << -1 << endl;
    } 
    else {
        cout << N - max(dp[N][0], dp[N][1]) << endl;
    }
}

Submission status: Runtime Error

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

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

res = ers(a,cnt)

print(str(res))

Submission status: Runtime Error

Solution code:
import unittest
import math

from A04 import solve
from A04 import parse_input

class TestA04Solve(unittest.TestCase):


    def test_solve(self):

        lines_as_string = """
3
2 1 2
"""
        r = parse_input(lines_as_string)
        for i, e in enumerate(r):
            print("e[%d]=%s" % (i, e))
        print("")
        self.assertEqual(solve(*r), 1)


        lines_as_string = """
3
2 2 2
"""
        r = parse_input(lines_as_string)
        for i, e in enumerate(r):
            print("e[%d]=%s" % (i, e))
        print("")
        self.assertEqual(solve(*r), -1)

        lines_as_string = """
10
3 1 4 1 5 9 2 6 5 3
"""
        r = parse_input(lines_as_string)
        for i, e in enumerate(r):
            print("e[%d]=%s" % (i, e))
        print("")
        self.assertEqual(solve(*r), 7)

        lines_as_string = """
1
1
"""
        r = parse_input(lines_as_string)
        for i, e in enumerate(r):
            print("e[%d]=%s" % (i, e))
        print("")
        self.assertEqual(solve(*r), 0)


if __name__ == '__main__':
    unittest.main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
alist = list(map(int, input().split()))
left = 0
for i in range(1, N + 1):
    try:
        old_left = left
        left = alist[left:].index(i)
        p = left + old_left
    except ValueError:
        print(i, p)
        if i == 1:
            print(-1)
        else:
            print(N - i + (N - p))
        break
else:
    print(0)

Submission status: Runtime Error

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

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

ans = True
for i in range(int(len(a))-1):
    if i+1!=a[i]:
        ans = False

if ans:
    print(count)
else:
    print(-1)

Submission status: Runtime Error

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

while len(A) != 0:
    if A[i] != i + 1:
        A.remove(A[i])
        count += 1
    elif i < len(A) -1:
        i += 1
    else:
        ans = count
        break

print(ans)

Submission status: Runtime Error

Solution code:
# そのまま入力文字列の入力
n = int(input())

# 空白区切りの数値をリスト化
an = list(map(int, input().split()))

ans = 0


def search_and_break(blocks, target):
    global  ans
    position = 0
    if len(blocks) == 0:
        return ans

    for i in range(0, len(blocks)):
        if blocks[i] == target:
            return search_and_break(blocks[(position+1):], target + 1)
        else:
            ans += 1
            position += 1

    return -1


print(search_and_break(an, 1))

Submission status: Runtime Error

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

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

res = ers(a,cnt)

print(res)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

res = ers(a,cnt,n)

print(res)

Submission status: Runtime Error

Solution code:
n=int(input())
a=[]*n
a=list(map(int,input().split()))
num=1
bre=0
no=True
def search(pos,s):
    spos=pos
    global num,bre,no
    end=True
    while end:
        if spos >= n:
            break
        if a[spos]==s:
            no=False
            bre+=spos-pos
            num+=1
            search(spos+1,num)
            end=False
        else:spos+=1
search(0, num)
if no:
    bre=-1
print(bre)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys

n = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
cnt=0
while True:
    for i, a in enumerate(A):
        if a > i+1:
            A.pop(i)
            cnt+=1
        if A[i] != i+1:
            A.pop(i)
            cnt+=1
            break
    else:
        break

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

Submission status: Runtime Error

Solution code:
import sys

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

if numbers == []:
    print(-1)
    sys.exit(0)

index = 0
value = 1
breaks = 0
while True:
    while numbers[index] != value:
        if index >= len(numbers):
            print(-1)
            sys.exit(0)
        numbers.pop(index)
        breaks += 1
    index += 1
    value += 1
    if index >= len(numbers):
        print(breaks)
        sys.exit(0)

Submission status: Runtime Error

Solution code:
a,b=(int(x) for x in input().split())
gcc=1
i=2
while True:
    if(a<i or b<i):
        break
    elif (a%i==0 and b%i==0):
        gcc*=i
        a/=i
        b/=i
        i=2
    else:
        i+=1

print(int(a*b*gcc))

Submission status: Runtime Error

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


def destroy(ns):
    if len(ns) == 0:
        return None
    if len(ns) == 1:
        return 0 if ns[0] == 1 else None
    satisfies = True
    for i, s in enumerate(ns):
        if i + 1 != s:
            satisfies = False
            break
    if satisfies:
        return 0
    min_breaks = None
    if ns[0] != 1:
        ans = destroy(ns[1:])
        if ans is None:
            return None
        return 1 + ans
    for i in range(len(ns) - 1):
        breaks = destroy(ns[:i] + ns[i+1:])
        if breaks is None:
            continue
        breaks += 1
        if min_breaks is None:
            min_breaks = breaks
            continue
        if breaks < min_breaks:
            min_breaks = breaks
    return min_breaks

ans = destroy(numbers)
print(-1 if ans is None else ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
i = 0
while i < len(a):
    while a[i] != i + 1:
        cnt += 1
        a.pop(i)
        if len(a) == 0:
            break
    i += 1
if a == []:
    print(-1)
else:
    print(cnt)

Submission status: Runtime Error

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

for i in range(n):
  if a[i] == tmp:
    tmp += 1
  else:
    ans += 1

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

Submission status: Runtime Error

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


def destroy(ns):
    if len(ns) == 0:
        return None
    satisfies = True
    for i, s in enumerate(ns):
        if i + 1 != s:
            satisfies = False
            break
    if satisfies:
        return 0
    min_breaks = None
    if ns[0] != 1:
        ans = destroy(ns[1:])
        if ans is None:
            return None
        return 1 + ans
    for i in range(len(ns) - 1):
        breaks = destroy(ns[:i] + ns[i+1:])
        if breaks is None:
            continue
        breaks += 1
        if min_breaks is None:
            min_breaks = breaks
            continue
        if breaks < min_breaks:
            min_breaks = breaks
    return min_breaks

ans = destroy(numbers)
print(-1 if ans is None else ans)