import java.io.{BufferedReader, InputStreamReader}

import scala.annotation.tailrec
import scala.collection.mutable.ArrayBuffer

object Main {
  def main(args: Array[String]): Unit = {
    val in = new BufferedReader(new InputStreamReader(System.in))
    val n = in.readLine.toInt
    val as = in.readLine.split("\\s+").map(_.toInt)

    val nums = Array.ofDim[ArrayBuffer[Int]](n + 1)
    for {
      i <- 0 to n
    } {
      nums(i) = new ArrayBuffer[Int]
    }
    for {
      i <- (n - 1) to 0 by -1
    } {
      nums(as(i)) += i
    }

//    System.err.println("----")
//    for {
//      i <- 1 to n
//    } {
//      System.err.println(i + "=" + nums(i))
//    }

    def blocks(k: Int): Option[Int] = {
      @tailrec
      def haveLessOne(currentIndex: Int): Boolean = {
        val num = as(currentIndex)
        if (num == 1) {
          true
        } else {
          nums(num - 1).find(_ < currentIndex) match {
            case Some(lessIndex) => haveLessOne(lessIndex)
            case None => false
          }
        }
      }

      nums(k).reverse.find(haveLessOne)
    }

    for {
      k <- n to 1 by -1
    } {
      blocks(k) match {
        case Some(index) =>
          System.out.println(n - k)
          return
        case None =>
      }
    }

    System.out.println(-1)
  }
}
