program prob6
    implicit none
    integer :: n
    integer, allocatable :: a(:)
    integer ::  i, tmp, ans, x
    read(*,*) n
    allocate(a(n))
    do i = 1, n
        read(*,*) x
        a(x) = i
    end do


    tmp = 1
    ans = 1
    do i = 1, n-1
        if(a(i) < a(i+1)) then
            tmp = tmp + 1
        else
            ans = max(tmp, ans)
            tmp = 1
        end if
    end do
    ans = max(tmp, ans)

    write(*,*) n - ans

    deallocate(a)

    stop
contains
end program prob6