You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. Sort this sequence in ascending order by choosing an element and moving it to the beginning or the end of the sequence. Find the minimum number of operations required. Constraints: 1 ≤ N ≤ 2×10^5, (P_1,P_2,...,P_N) is a permutation of (1,2,...,N), and all values in input are integers. Input is given in the format: N, P_1, P_2, ..., P_N. Output the minimum number of operations required. Example: For input 4, the output is 2.

# Buggy version:
from collections import Counter

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

lst = [False] * (n + 1)
for num in p:
    bfo = lst[num - 1]
    if bfo:
        lst[num] = num
    else:
        lst[num] = bfo
cnt = Counter(lst).values()
print(n - max(cnt))


# Fixed version:
from collections import Counter

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

lst = [False] * (n + 1)
for num in p:
    bfo = lst[num - 1]
    if bfo:
        lst[num] = bfo
    else:
        lst[num] = num
cnt = Counter(lst).values()
print(n - max(cnt))

# Buggy version:
from collections import Counter

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

lst = [-1] * (n + 1)
for num in p:
    bfo = lst[num - 1]
    if bfo == -1:
        lst[num] = num
    elif bfo > 0:
        lst[num] = num - 1
cnt = Counter(lst).values()
print(n - max(cnt))


# Fixed version:
from collections import Counter

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

lst = [-1] * (n + 1)
for num in p:
    bfo = lst[num - 1]
    if bfo == -1:
        lst[num] = num
    elif bfo > 0:
        lst[num] = bfo
cnt = Counter(lst).values()
print(n - max(cnt))

# Buggy version:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())

Q = [0]*N
for i in range(N):
    p = int(input())
    Q[p-1] = i

cnt = 1
now_idx = -1
ans = 0
for i in range(N):
    idx = Q[i]
    if idx>now_idx:
        cnt += 1
        ans = max(ans, cnt)
    else:
        cnt = 1
    now_idx = idx
print(N-ans)
    

# Fixed version:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())

Q = [0]*N
for i in range(N):
    p = int(input())
    Q[p-1] = i

cnt = 0
now_idx = -1
ans = 0
for i in range(N):
    idx = Q[i]
    if idx>now_idx:
        cnt += 1
        ans = max(ans, cnt)
    else:
        cnt = 1
    now_idx = idx

print(N-ans)

# Buggy version:
n = int(input())
p = [int(input()) for _ in range(n)]

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

print(q - max(q))


# Fixed version:
n = int(input())
p = [int(input()) for _ in range(n)]

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

print(n - max(q))

# Buggy version:
N = int(input())
P = [0]
for i in range(N):
  P.append(int(input()))
Q = [0]*(N+1)
for i in range(1,N+1):
  Q[P[i]] = i
res = Q[1]
cnt = 0  
c = 1
for i in range(2,N+1):
  if res < Q[i]:
    c += 1
    res = Q[i]
  if i == N:
    cnt = max(cnt,c)
  else:
    cnt = max(cnt,c)
    c = 1
    res = Q[i]
print(N-cnt)      

# Fixed version:
N = int(input())
P = [0]
for i in range(N):
  P.append(int(input()))
Q = [0]*(N+1)
for i in range(1,N+1):
  Q[P[i]] = i
res = Q[1]
cnt = 0  
c = 1
for i in range(2,N+1):
  if res < Q[i]:
    c += 1
    res = Q[i]
    if i == N:
      cnt = max(cnt,c)
  else:
    cnt = max(cnt,c)
    c = 1
    res = Q[i]
ans = N-cnt
if N ==1:
  ans = 0    
print(ans)

# Buggy version:
n = int(input())
lis = [int(input()) for i in range(n)]
li = [-1 for i in range(n)]
for i in range(n):
    li[lis[i]-1] = i
cnt = 0
ans = n-1
for i in range(n-1):
    if li[i] > li[i+1]:
        cnt += 1
    else:
        ans = min(ans,n-1-cnt)
        cnt = 0
ans = min(ans,n-1-cnt)
print(ans)

# Fixed version:
n = int(input())
lis = [int(input()) for i in range(n)]
li = [-1 for i in range(n)]
for i in range(n):
    li[lis[i]-1] = i
cnt = 0
ans = n-1
for i in range(n-1):
    if li[i] < li[i+1]:
        cnt += 1
    else:
        ans = min(ans,n-1-cnt)
        cnt = 0
ans = min(ans,n-1-cnt)
print(ans)

# Buggy version:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    p = IR(n)
    q = [None for i in range(n + 1)] + [-1]
    for i in range(n):
        q[p[i]] = 1
    ans = 0
    tmp = 0
    for i in range(2, n + 2):
        if q[i - 1] < q[i]:
            continue
        ans = max(ans, i - tmp)
        tmp = i
    print(n-ans)
    return
#main
if __name__ == '__main__':
    solve()


# Fixed version:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    p = IR(n)
    q = [None for i in range(n + 1)] + [-1]
    for i in range(n):
        q[p[i]] = i
    ans = 0
    tmp = 1
    for i in range(2, n + 2):
        if q[i - 1] < q[i]:
            continue
        ans = max(ans, i - tmp)
        tmp = i
    print(n-ans)
    return
#main
if __name__ == '__main__':
    solve()

# Buggy version:
n=int(input())
p=[int(input()) for _ in range(n)]
dp=[0]*(n+10)
for i in p:
  dp[p]=dp[p-1]+1
print(n-max(dp))


# Fixed version:
n=int(input())
p=[int(input()) for _ in range(n)]
dp=[0]*(n+10)
for i in p:
  dp[i]=dp[i-1]+1
print(n-max(dp))

# Buggy version:
N=int(input())

q=[-1]*N
for i in range(N):
    k = int(input())
    print(k)
    q[k-1]=i


max_con=1
count=0
for i in range(N-1):
    if q[i]<q[i+1]:
        count+=1
        max_con=max(max_con,count)
    else:
        count=1

print(N-max_con)

# Fixed version:
N=int(input())

q=[-1]*N
for i in range(N):
    k = int(input())
    #print(k)
    q[k-1]=i


max_con=1
count=1
for i in range(N-1):
    if q[i]<q[i+1]:
        count+=1
        max_con=max(max_con,count)
    else:
        count=1

print(N-max_con)

# Buggy version:
#N, *a = map(int, open(0).read().split())
def main():
    N = int(input())
    a=[]
    for i in range(N):
        a.append(int(input()))

    b = [0 for _ in range(N+1)]
    cnt = 1
    ans = 0
    flag = 1
    i = 0
    for i in reversed(range(N)):
        if b[a[i]+1] > 0:
            b[a[i]] = b[a[i]+1] + 1
        else:
            b[a[i]] = 1
        
    return N-max(b)
print(main())


# Fixed version:
#N, *a = map(int, open(0).read().split())
def main():
    N = int(input())
    a=[]
    for i in range(N):
        a.append(int(input()))

    b = [0 for _ in range(N+2)]
    cnt = 1
    ans = 0
    flag = 1
    i = 0
    for i in reversed(range(N)):
        if b[a[i]+1] > 0:
            b[a[i]] = b[a[i]+1] + 1
        else:
            b[a[i]] = 1
        
    return N-max(b)
print(main())

# Buggy version:
import sys
input = sys.stdin.readline
n = int(input())
memo = [0 for i in range(n)]
for i in range(n):
  memo[int(input())-1] = i
cand = 0
tmp = 0
for n1, n2 in range(n - 1):
  if n2 > n1:
    tmp += 1
    if tmp > cand:
      cand = tmp
  else:
    tmp = 0
print(n - cand - 1)

# Fixed version:
import sys
input = sys.stdin.readline
n = int(input())
memo = [0 for i in range(n)]
for i in range(n):
  memo[int(input())-1] = i
cand = 0
tmp = 0
for n1, n2 in zip(memo[:], memo[1:]):
  if n2 > n1:
    tmp += 1
    if tmp > cand:
      cand = tmp
  else:
    tmp = 0
print(n - cand - 1)

# Buggy version:
N = int(input())
L = []
for i in range(N):
  L.append(int(input()))
TF = [False]*(N+1)
dp = [0]*(N+1)
for i in range(N):
  TF[L[i]] = True
  if TF[L[i]-1] == True:
    dp[L[i]] = dp[L[i]-1]+1
print(max(dp)+1)

# Fixed version:
N = int(input())
L = []
for i in range(N):
  L.append(int(input()))
TF = [False]*(N+1)
dp = [0]*(N+1)
for i in range(N):
  TF[L[i]] = True
  if TF[L[i]-1] == True:
    dp[L[i]] = dp[L[i]-1]+1
print(N-max(dp)-1)

# Buggy version:
N = int(input())
P = [int(input()) for i in range(N)]


Q = sorted([(p, i) for i, p in enumerate(P, start=1)])
Q = [i for p, i in Q]

dp = [1] * (N + 1)
for i in range(1, N):
    print(i)
    if Q[i] > Q[i - 1]:
        dp[i] = dp[i - 1] + 1

print(N - max(dp))


# Fixed version:
N = int(input())
P = [int(input()) for i in range(N)]


Q = sorted([(p, i) for i, p in enumerate(P, start=1)])
Q = [i for p, i in Q]

dp = [1] * (N + 1)
for i in range(1, N):
    if Q[i] > Q[i - 1]:
        dp[i] = dp[i - 1] + 1

print(N - max(dp))

# Buggy version:
N = int(input())
dp = [0]* (N)
for i in range(N):
  dp[int(input())-1] = i
ans = 0
cc = 0
for i in range(N-1):
  if dp[i] < dp[i+1]:
    cc += 1
  else:
    ans = max(cc, ans)
    cc = 0
ans = max(cc, ans)
print(N-ans+1)

# Fixed version:
N = int(input())
dp = [0]*N
for i in range(N):
  dp[int(input())-1] = i
ans = 0
cc = 0
for i in range(N-1):
  if dp[i] < dp[i+1]:
    cc += 1
  else:
    ans = max(cc, ans)
    cc = 0
ans = max(cc, ans)
print(N-ans-1)

# Buggy version:
N = int(input())
a = [0 for i in range(N)]
chain = [0 for i in range(N)]
for i in range(N):
  a[i] = int(input())
  chain[a[i]] = chain[a[i]-1]+1
print(max(chain))

# Fixed version:
N = int(input())
a = [0 for i in range(N)]
chain = [0 for i in range(N+1)]
for i in range(N):
  a[i] = int(input())
  chain[a[i]] = chain[a[i]-1]+1
print(N-max(chain))

# Buggy version:
N = int(input())
a = [0]
for _ in range(N):
  a.append(int(input()))

Q = [0 for i in range(N + 1)]
for i in range(N + 1):
    Q[a[i]] = i
ans = 1
num = 0

for idx in range(2, len(a)):
    if a[idx - 1] < a[idx]:
        num += 1
    else:
        ans, num = max(num, ans), 1

print(N - max(num, ans))


# Fixed version:
N = int(input())
a = [0]
for _ in range(N):
  a.append(int(input()))

Q = [0 for i in range(N + 1)]
for i in range(N + 1):
    Q[a[i]] = i
ans = 1
num = 1

for idx in range(2, len(Q)):
    if Q[idx - 1] < Q[idx]:
        num += 1
    else:
        ans, num = max(num, ans), 1

print(N - max(num, ans))

# Buggy version:
n = int(input())
p = [int(input()) for _ in range(n)]
q = [0] * (n+1)

for i in range(n):
    q[p[i]] = i
maxl = 0
cnt = 0
for i in range(n):
    if q[i+1] > q[i]:
        cnt += 1
    else:
        maxl = max(maxl, cnt)
print(n - maxl)


# Fixed version:
n = int(input())
p = [int(input()) for _ in range(n)]
q = [0] * (n+1)

for i in range(n):
    q[p[i]] = i
maxl = 1
cnt = 1
for i in range(1, n):
    if q[i+1] > q[i]:
        cnt += 1
    else:
        maxl = max(maxl, cnt)
        cnt = 1
print(n - max(maxl, cnt))

# Buggy version:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Grand Contest 024 B問題 WA
Author     : Kitaura Hiromi
LastUpdate : 20180520
Since      : 20180520
"""
N = int(input())
p = [int(input()) for i in range(N)]
q = [0] * N
for i in range(N):
    q[p[i]-1] = i

cnt = 1
for i in range(N-1):
    if q[i+1] - q[i] > 0:
        cnt += 1
    else:
        max_cnt = max(max_cnt, cnt)
        cnt = 1
print(N - max_cnt)

# Fixed version:
# -*- coding: utf-8 -*-
"""
Contents   : AtCoder Grand Contest 024 B問題 WA
Author     : Kitaura Hiromi
LastUpdate : 20180520
Since      : 20180520
"""
N = int(input())
p = [int(input()) for i in range(N)]
q = [0 for i in range(N)]
for i in range(N):
    q[p[i]-1] = i

cnt = 1
max_cnt = 1
for i in range(N-1):
    if q[i+1] > q[i]:
        cnt += 1
    else:
        max_cnt = max(max_cnt, cnt)
        cnt = 1
max_cnt = max(max_cnt, cnt)
print(N - max_cnt)

# Buggy version:
N=int(input())
P=[int(input()) for _ in range(N)]
Q=[0 for _ in range(N)]

if N=1:
    print(1)
else:
    for i in range(N):
        Q[P[i]-1]=i

    cnt=0
    tmp=0
    for i in range(N-1):
        if Q[i]<Q[i+1]:
            cnt+=1
        else:
            if tmp<cnt+1:
                tmp=cnt+1
            cnt=0

    print(N-tmp)


# Fixed version:
N=int(input())
P=[int(input()) for _ in range(N)]
Q=[0 for _ in range(N)]

for i in range(N):
    Q[P[i]-1]=i

cnt=0
tmp=0
for i in range(N-1):
    if Q[i]<Q[i+1]:
        cnt+=1
    else:
        if tmp<cnt+1:
            tmp=cnt+1
        cnt=0

if tmp==0:
    tmp=cnt+1

print(N-tmp)

# Buggy version:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
p=[]
q=[0 for i in range(n)]
for i in range(n):   
 p.append(i1())
 q[p[-1]-1]=i
k=1
s=0
while n-1>j:
  if q[i+1]-q[i]>0:
     k+=1
  else:
     s=max(s,k)
     k=1
  j+=1
s=max(s,k)
print(n-s)

# Fixed version:
def i1():
 return int(input())
def i2():
 return [int(i) for i in input().split()]
n=i1()
p=[]
q=[0 for i in range(n)]
for i in range(n):   
 p.append(i1())
 q[p[-1]-1]=i
k=1
s=0
for i in range(n-1):
  if q[i+1]-q[i]>0:
     k+=1
  else:
     s=max(s,k)
     k=1
s=max(s,k)
print(n-s)

# Buggy version:
n = int(input())
ppp = list(map(int, (input() for _ in range(n))))

tmp = [0] * n
for p in ppp:
    tmp[p] = tmp[p - 1] + 1

print(n - max(tmp))


# Fixed version:
n = int(input())
ppp = list(map(int, (input() for _ in range(n))))

tmp = [0] * (n + 1)
for p in ppp:
    tmp[p] = tmp[p - 1] + 1

print(n - max(tmp))

# Buggy version:
import sys
if __name__ == "__main__":
    N = int(input())
    P = []
    Q = [0]*(N+1)
    for i in range(N):
        p = int(input())
        Q[p] = i

    # 1 4 5 2 3 -> 4 5を移動
    # 1 4 5 3 2 -> 3 4 5 を移動
    # 5 2 1 3 4 -> 1 5 を移動
    cur = 1
    max_cur = 1
    for i in range(1, N):
        if Q[i] < Q[i+1]:
            cur += 1
            max = max(cur, max_cur)
        else:
            cur = 1
    print(N - max_cur)

# Fixed version:
import sys
if __name__ == "__main__":
    N = int(input())
    P = []
    Q = [0]*(N+1)
    for i in range(1, N+1):
        p = int(input())
        Q[p] = i

    # 1 4 5 2 3 -> 4 5を移動
    # 1 4 5 3 2 -> 3 4 5 を移動
    # 5 2 1 3 4 -> 1 5 を移動
    cur = 1
    max_cur = 1
    for i in range(1, N):
        if Q[i] < Q[i+1]:
            cur += 1
            max_cur = max(cur, max_cur)
        else:
            cur = 1
    print(N - max_cur)

# Buggy version:
n=int(input())
a=[int(input()) for i in range(n)]

al= {}
ds=[]
ans=0
for ai in a:
  ds=
  if ai - 1 in al.keys():
    al[ai]=al[ai-1]+1
    ans=max(ans,al[ai-1]+1)   
    del al[ai-1]
  else:
    al[ai]=0
    if ai + 1 in al.keys():
      del al[ai]
print(n-ans-1)

# Fixed version:
n=int(input())
a=[int(input()) for i in range(n)]

al= {}
ans=0
for ai in a:
  if ai - 1 in al.keys():
    al[ai]=al[ai-1]+1
    ans=max(ans,al[ai-1]+1)   
    del al[ai-1]
  else:
    al[ai]=0
    if ai + 1 in al.keys():
      del al[ai]
print(n-ans-1)

# Buggy version:
N = int(input())

Ps = [(int(input()), i) for i in range(N)]
Ps.sort()

print(Ps)

num = 0
ans = 0
for i in range(N - 1):
    if Ps[i][1] < Ps[i + 1][1]:
        num += 1
    else:
        ans = max(ans, num)
        num = 0
    
ans = max(ans, num)
print(N - ans - 1)


# Fixed version:
N = int(input())

Ps = [(int(input()), i) for i in range(N)]
Ps.sort()

num = 0
ans = 0
for i in range(N - 1):
    if Ps[i][1] < Ps[i + 1][1]:
        num += 1
    else:
        ans = max(ans, num)
        num = 0
    
ans = max(ans, num)
print(N - ans - 1)

# Buggy version:
def main():
    N=int(input())
    P=[0]*N
    p=[False]*(N+1)
    P[0]=int(input())
    for i in range(N-1):
        P[i+1=int(input())
        if P[-1]-1 in P[:-1]:
            p[P[-1]-1] = True
    ans=0
    j=0
    for i in range(N):
        if p[i]==True:
            j+=1
        else:
            ans=max(ans,j)
            j=0
    print(N-ans-1)


if __name__ == "__main__":
    main()


# Fixed version:
def main():
    N=int(input())
    p=[False]*(N+1)
    c=[False]*(N+1)
    for i in range(N):
        x=int(input())
        c[x]=True
        if c[x-1]:
            p[x-1] = True
    ans=0
    j=0
    for i in range(1,N+1):
        if p[i]:
            j+=1
        else:
            ans=max(ans,j)
            j=0
    ans=max(ans,j)
    print(N-ans-1)

if __name__ == "__main__":
    main()

# Buggy version:


def main():
    n = int(input())
    pp = []
    for _ in range(n):
        pp.append(int(input()))
    dd = {}
    for i in range(n):
        dd[pp[i]] = i

    xx = [-1] * n
    for s in range(n - 1, -1, -1):
        if xx[s] >= 0:
            continue
        count = 1
        pre = pp[s]
        while pre + 1 in dd:
            i = dd[pre + 1]
            if xx[i] < 0:
                xx[s] = xx[i] + count
                break
            pre = pp[i]
            count += 1
        else:
            xx[s] = count
    print(n - max(xx))


if __name__ == '__main__':
    main()


# Fixed version:


def main():
    n = int(input())
    pp = []
    for _ in range(n):
        pp.append(int(input()))
    dd = {}
    for i in range(n):
        dd[pp[i]] = i

    xx = [-1] * n
    for s in range(n - 1, -1, -1):
        if xx[s] >= 0:
            continue
        count = 1
        pre = pp[s]
        while pre + 1 in dd and dd[pre + 1] > s:
            i = dd[pre + 1]
            if xx[i] >= 0:
                xx[s] = xx[i] + count
                break
            pre += 1
            count += 1
        else:
            xx[s] = count
    print(n - max(xx))


if __name__ == '__main__':
    main()