fun main(args:Array<String>):Unit{
    val n = readLine()!!.toInt()
    val nums = Array(n){i -> Pair(i, readLine()!!.toInt())}.sortedBy(Pair<Int, Int>::second).toTypedArray()
    var left = 0
    var right = 0
    var max = 0
    while (right + 1 < n){
        if (nums[right].first < nums[right + 1].first){
            ++right
        }else{
            if (max < (right - left + 1)){
                max = (right - left + 1)
            }
            left = ++right
        }
    }
    if (max < (right - left + 1))
        max = (right - left + 1)
    println(n - max)
}