Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

p = [int(input()) for _ in range(n)]


# 連続している最大値を求める

max_count = 0
for starti in range(len(p)):
    previous = p[starti]
    count = 1
    for i in range(starti, len(p)):
        if previous + 1 == p[i]:
            previous += 1
            count += 1

    if max_count < count:
        max_count = count

    
print(n-max_count)


Fixed submission:
n = int(input())

p = [int(input()) for _ in range(n)]


# 連続している最大値を求める

lengths = {}
for pn in p:
    if pn in lengths:
        lengths[pn+1] = lengths[pn] + 1
    else:
        lengths[pn+1] = 1
    
max_count = max(lengths.values())
print(n-max_count)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

p = [int(input()) for _ in range(n)]


# すでに並んでる列
ordered = []
unordered = []

previous = 0
for i in p:
    if previous < i:
        ordered.append(i)
        previous = i
    else:
        unordered.append(i)
        

count = 0

center = ordered[len(ordered)//2]
unordered.sort(key=(lambda x: abs(x - center)))

for t in unordered:
    count += 1
    index = 0
    if center > t:
        index = 0
        for i in ordered:
            if i < t:
                count +=1
                index +=1
            else:
                break
    else:
        index = -1
        for i in reversed(ordered):
            if i > t:
                count +=1
                index -=1
            else:
                break


    ordered.insert(index, t)

print(count)




Fixed submission:
n = int(input())

p = [int(input()) for _ in range(n)]


# 連続している最大値を求める

lengths = {}
for pn in p:
    if pn in lengths:
        lengths[pn+1] = lengths[pn] + 1
    else:
        lengths[pn+1] = 1
    
max_count = max(lengths.values())
print(n-max_count)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

p = [int(input()) for _ in range(n)]


# すでに並んでる列
ordered = []
unordered = []

previous = 0
for i in p:
    if previous < i:
        ordered.append(i)
        previous = i
    else:
        unordered.append(i)
        

count = 0

loopc = len(unordered)
for _ in range(loopc):

    center = ordered[len(ordered)//2]

    # センターに近いものを選択
    t = min(unordered, key=(lambda x: abs(x - center)))

    count += 1
    index = 0
    if center > t:
        index = 0
        for i in ordered:
            if i < t:
                count +=1
                index +=1
            else:
                break
    else:
        index = -1
        for i in reversed(ordered):
            if i > t:
                count +=1
                index -=1
            else:
                break


    unordered.remove(t)
    ordered.insert(index, t)

print(count)




Fixed submission:
n = int(input())

p = [int(input()) for _ in range(n)]


# 連続している最大値を求める

lengths = {}
for pn in p:
    if pn in lengths:
        lengths[pn+1] = lengths[pn] + 1
    else:
        lengths[pn+1] = 1
    
max_count = max(lengths.values())
print(n-max_count)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

p = [int(input()) for _ in range(n)]


# すでに並んでる列
ordered = []
unordered = []

previous = 0
for i in p:
    if previous < i:
        ordered.append(i)
        previous = i
    else:
        unordered.append(i)
        

count = 0

loopc = len(unordered)
for _ in range(loopc):

    center = ordered[len(ordered)//2]

    # センターに近いものを選択
    t = min(unordered, key=(lambda x: abs(x - center)))


    count += 1
    if center > t:
        for i in ordered:
            if i < t:
                count +=1
            else:
                break
    else:
        for i in reversed(ordered):
            if i > t:
                count +=1
            else:
                break


    unordered.remove(t)
    ordered.append(t)
    ordered.sort()


print(count)




Fixed submission:
n = int(input())

p = [int(input()) for _ in range(n)]


# 連続している最大値を求める

lengths = {}
for pn in p:
    if pn in lengths:
        lengths[pn+1] = lengths[pn] + 1
    else:
        lengths[pn+1] = 1
    
max_count = max(lengths.values())
print(n-max_count)