import scala.io.StdIn
import scala.annotation.tailrec

object Main{
  def main(args: Array[String]){
    StdIn.readLine()
    val line = StdIn.readLine().split(" ").toList.map(_.toInt)

    println(line.sorted)
    println(sSort(List(), line, 0))
  }

  @tailrec
  def sSort(sorted: List[Int], unsorted: List[Int], n: Int): Int={
    if(unsorted.length == 0) n
    else if(unsorted.sorted == unsorted) n
    else sSort(sorted :+ unsorted.min, deleteOneList(unsorted, unsorted.min), n+1)
  }

  def deleteOneList(list: List[Int], target: Int): List[Int] =
    list.filter(_ != target) ::: List.fill(list.filter(_ == target).length - 1)(target)
}