An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increasing numbers. In one move he can make one of the cars disappear from its place and teleport it either to the beginning of the train, or to the end of the train, at his desire. What is the minimum number of actions David Blaine needs to perform in order to sort the train?


-----Input-----

The first line of the input contains integer n (1 ≤ n ≤ 100 000) — the number of cars in the train. 

The second line contains n integers p_{i} (1 ≤ p_{i} ≤ n, p_{i} ≠ p_{j} if i ≠ j) — the sequence of the numbers of the cars in the train.


-----Output-----

Print a single integer — the minimum number of actions needed to sort the railway cars.


-----Examples-----
Input
5
4 1 2 5 3

Output
2

Input
4
4 1 3 2

Output
2



-----Note-----

In the first sample you need first to teleport the 4-th car, and then the 5-th car to the end of the train.

Difficulty: competition

Starter code:

Solutions:
n, p = int(input()), list(map(int, input().split()))
a, c, v = [0] * n, 1, 1
for i, pi in enumerate(p):
    a[pi - 1] = i
for i in range(n - 1):
    if a[i] < a[i + 1]:
        c += 1
        if c > v:
            v = c
    else:
        c = 1
print(n - v)

n, p = int(input()), list(map(int, input().split()))
a, c, v = [0] * n, 1, 1
for i, pi in enumerate(p):
    a[pi - 1] = i
for i in range(n - 1):
    if a[i] < a[i + 1]:
        c += 1
        if c > v:
            v = c
    else:
        c = 1
print(n - v)

n = int(input())
l = list(map(int, input().split()))
l2 = [0]*len(l)
for i in range(len(l)):
    l2[l[i]-1] = i
prev = -1
ans = -1e9
curr = 0
for i in l2:
    if i>prev:
        curr+=1
        prev = i
    else:
        ans = max(curr, ans)
        curr = 1
    prev=i
print(n-max(curr, ans))


n = int(input())
cars = [int(x) for x in input().split()]
sorted_cars = sorted(enumerate(cars), key = lambda x : x[1])
max_sorted_length = 1
length = 1
for i in range(1, n):
    if sorted_cars[i][0] > sorted_cars[i-1][0]:
        length += 1
    else:
        if max_sorted_length < length:
            max_sorted_length = length
        length = 1
if max_sorted_length < length: max_sorted_length = length
print(n - max_sorted_length)


__author__ = 'Utena'
n=int(input())
m=list(map(int,input().split()))
t=0
l=[0]
r=[0]*(n+1)
for j in range(n):
    r[m[j]]=j+1
if n==1:
    print(0)
else:
    for i in range(1,n):
        t+=1
        if r[i]>r[i+1]:
            l.append(t)
            t=0
    if r[n]>r[n-1]:
        t+=1
        l.append(t)
    print(n-max(l))

a=[0 for i in range(100010)]
b=[0 for i in range(100010)]
c=[0 for i in range(100010)]
n, a[1:] = int(input()), list(map(int,input().split(' ')))
for i in range(1,n+1):
    if b[a[i]-1]:
        c[a[i]]=c[a[i]-1]+1
    else:
        c[a[i]]=1
    b[a[i]]=1
print(n-max(c[1:n+1]))


def main():
    n = int(input())
    l = [n] * (n + 1)
    for a in map(int, input().split()):
        l[a] = l[a - 1] - 1
    print(min(l))


def __starting_point():
    main()

__starting_point()

n = int(input())

p = list(map(int, input().split()))
q = [0] * n

for i in range(n):
    q[p[i] - 1] = i

ans = 1
cur = 1

for i in range(1, n):
    if q[i - 1] < q[i]:
        cur += 1
    else:
        cur = 1
    ans = max(ans, cur)

print(n - ans)


n=int(input())
l=[n]*(n+1)
for c in map(int,input().split()):l[c]=l[c-1]-1
print(min(l))

3
N = int(input())
l = [1 for x in range(N+1)]
m = [1 for x in range(N+1)]
usr_input = input().split()
ans = 1
for x in range(N):
	m[int(usr_input[x])] = x + 1
for x in range(2,N+1):
	if(m[x] > m[x-1]):
		l[x] += l[x-1]
	if(l[x] > ans):
		ans = l[x]
print(N-ans)



readInts=lambda: list(map(int, input().split()))
n=int(input())
a=readInts()
p=[0]*n
for i in range(n):
    p[a[i]-1]=i
inc=1;ret=n-1
#print(p)
for i in range(1,n):
    if p[i]>p[i-1]:
        inc+=1
    else:
        inc=1
    ret=min(ret,n-inc)
print(ret)


#!/usr/bin/env python3
import collections, itertools, functools, math

def solve():
    n = int(input())
    p = list(map(int, input().split()))
    pos = [(p, i) for i, p in enumerate(p)]
    pos.sort()

    longest = 1
    seq = 1
    for i in range(1, n):
        if pos[i-1][1] < pos[i][1]:
            seq += 1
        else:
            seq = 1
        longest = max(longest, seq)
    return n - longest


def __starting_point():
    print(solve())


__starting_point()

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

pos = [0] * n
for i, p in enumerate(ps):
    pos[p-1] = i

used = [False] * n

longest = 0
for v in range(n):
    if used[v]:
        continue
    i = pos[v]
    w = v
    used[w] = True
    while w < n - 1 and pos[w + 1] > i:
        i = pos[w + 1]
        w += 1
        used[w] = True
    longest = max(longest, w - v + 1)
print(n - longest)

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

l = {}
for x in p:
    if x-1 in l:
        l[x] = l[x-1] + 1
    else:
        l[x] = 1

print(n - max(l.values()))


n = int(input())
L = input().split()
IDX = (n + 2) * [0]

def max(a, b):
    if a > b:
        return a
    return b

mx, act, last = 0, 0, 0

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

for i in range(1, n + 1):
    if IDX[i] > last:
        last = IDX[i]
        act += 1
    else:
        last = IDX[i]
        act = 1
    mx = max(mx, act)


print(n - mx)


n=int(input())
lst = [0 for i in range(n)]
kereta = input().split()
for i in range(n):
	lst[int(kereta[i])-1]=i
ancol=1
terbanyak=0
for i in range(n-1):
	if lst[i]>lst[i+1]:
		if terbanyak<ancol:terbanyak=ancol
		ancol=0
	ancol+=1
if terbanyak<ancol:terbanyak=ancol
print(n-terbanyak)


n=int(input())
l=[n]*(n+1)
for c in [int(x) for x in input().split()]:l[c]=l[c-1]-1
print(min(l))


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

a = [0] * (n + 1)
k = 0

for x in l:
    a[x] = a[x - 1] + 1
    k = max(k, a[x])    

print(n - k)


n=int(input())
m=[0]*(n+1)
l=list(map(int,input().split()))
for i in l:
    m[i]=m[i-1]+1
print(n-max(m))

def __starting_point():
    n = int(input())
    line = list(map(int, input().split()))
    book = [0] * n
    for it in line:
        if it > 1:
            if book[it - 2] > 0:
                book[it - 1] = book[it - 2] + 1
            else:
                book[it - 1] = 1
        else:
            book[it - 1] = 1
    print(n - max(book))

__starting_point()

n=int(input())
l=[n]*(n+1)
for c in map(int,input().split()):l[c]=l[c-1]-1
print(min(l))


n=int(input())
l=[n]*(n+1)
for c in map(int,input().split()):l[c]=l[c-1]-1
print(min(l))


n=int(input())
l=[n]*(n+1)
for c in map(int,input().split()):l[c]=l[c-1]-1
print(min(l))


n=int(input())
l=[n]*(n+1)
for c in map(int,input().split()):l[c]=l[c-1]-1
print(min(l))


n=int(input())
l=[n]*(n+1)
for c in map(int,input().split()):l[c]=l[c-1]-1
print(min(l))