output_description: Print the minimum number of operations required.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N
      P_1
      :
      P_N
attempt: import sys
  import math
  import copy
  import random
  from heapq import heappush, heappop, heapify
  from functools import cmp_to_key
  from bisect import bisect_left, bisect_right
  from collections import defaultdict, deque, Counter
  
  # sys.setrecursionlimit(1000000)
  
  # input aliases
  input = sys.stdin.readline
  getS = lambda: input().strip()
  getN = lambda: int(input())
  getList = lambda: list(map(int, input().split()))
  getZList = lambda: [int(x) - 1 for x in input().split()]
  
  INF = float("inf")
  MOD = 10**9 + 7
  divide = lambda x: pow(x, MOD - 2, MOD)
  
  
  def nck(n, k, kaijyo):
      return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD
  
  
  def npk(n, k, kaijyo):
      if k == 0 or k == n:
          return n % MOD
      return (kaijyo[n] * divide(kaijyo[n - k])) % MOD
  
  
  def fact_and_inv(SIZE):
      inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
      fac = [0] * SIZE  # fac[j] = j! mod MOD
      finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
      inv[1] = 1
      fac[0] = fac[1] = 1
      finv[0] = finv[1] = 1
      for i in range(2, SIZE):
          inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
          fac[i] = fac[i - 1] * i % MOD
          finv[i] = finv[i - 1] * inv[i] % MOD
  
      return fac, finv
  
  
  def renritsu(A, Y):
      # example 2x + y = 3, x + 3y = 4
      # A = [[2,1], [1,3]])
      # Y = [[3],[4]] または [3,4]
      A = np.matrix(A)
      Y = np.matrix(Y)
      Y = np.reshape(Y, (-1, 1))
      X = np.linalg.solve(A, Y)
  
      # [1.0, 1.0]
      return X.flatten().tolist()[0]
  
  
  class BIT:
      # A1 ... AnのBIT(1-indexed)
      def __init__(self, n):
          self.n = n
          self.BIT = [0] * (n + 2)
  
      # A1 ~ Aiまでの和 O(logN)
      def query(self, idx):
          res_sum = 0
          while idx > 0:
              res_sum += self.BIT[idx]
              idx -= idx & (-idx)
          return res_sum
  
      # Ai += x O(logN)
      def update(self, idx, x):
          # print(idx)
          while idx <= self.n:
              # print(idx)
              self.BIT[idx] += x
              idx += idx & (-idx)
          return
  
      def show(self):
          print(self.BIT)
  
  
  def solve():
      n = getN()
      li = []
      for i in range(n):
          li.append(getN())
  
      slis = [0 for i in range(n + 10)]
  
      for num in li:
          slis[num] = slis[num - 1] + 1
  
      print(n - max(slis))
  
  
  def main():
      n = getN()
      for _ in range(n):
          s = "".join([random.choice(["F", "T"]) for i in range(20)])
          print(s)
          solve(s, 1, 0)
  
      return
  
  
  if __name__ == "__main__":
      # main()
      solve()
  
problem_description: Statement
  
  You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the
  integers from 1 through N. You would like to sort this sequence in ascending
  order by repeating the following operation:
  
    * Choose an element in the sequence and move it to the beginning or the end of the sequence.
  
  Find the minimum number of operations required. It can be proved that it is
  actually possible to sort the sequence using this operation.
samples: [{"input": "4\n    1\n    3\n    2\n    4", "output": "2\n    \n\nFor example, the sequence can be sorted in ascending order as follows:\n\n  * Move 2 to the beginning. The sequence is now (2,1,3,4).\n  * Move 1 to the beginning. The sequence is now (1,2,3,4).\n\n* * *"}, {"input": "6\n    3\n    2\n    5\n    1\n    4\n    6", "output": "4\n    \n\n* * *"}, {"input": "8\n    6\n    3\n    1\n    2\n    7\n    4\n    8\n    5", "output": "5"}]