import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)

  val N = in.next().toInt
  val P = new Array[Int](N+1)

  for(i <- 1 to N) {
    P(i) = in.next().toInt
  }

  var ans1 = 0
  var mx = N
  for(i <- (1 to N).reverse) {
    val now = P(i)

    if(mx == now) {
      mx -= 1
    }
    else {
      ans1 += 1
    }
  }


  var ans2 = 0
  var mn = 1
  for(i <- (1 to N)) {
    val now = P(i)

    if(mn == now) {
      var ind = mn + 1
      mn += 1
    }
    else {
      ans2 += 1
    }
  }

  println(Math.min(ans1, ans2))
}


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}

