import scala.annotation.tailrec

object Main extends App {
  import scala.io.StdIn._

  val n = readLine.trim.toInt
  val num = readLine.trim.split(' ').map(_.toInt)
  val sorted = num.sorted
  val diffCount = num.indices.count(i ⇒ num(i) != sorted(i))
  val q = readLine.trim.toInt
  solve(num, sorted, diffCount, q, 0)
  @tailrec def solve(num: Array[Int], sorted: Array[Int], diffCount: Int, q: Int, count: Int): Unit = {

    if (q == count && diffCount != 0) println(-1)
    else if (diffCount == 0) println(count)
    else {
      val a@Array(x, y) = readLine.trim.split(' ').map(_.toInt - 1)
      val diff = a.count(i ⇒ num(i) != sorted(i))
      val temp = num(x)
      num(x) = num(y)
      num(y) = temp
      solve(num, sorted, diffCount - diff + a.count(i ⇒ num(i) != sorted(i)), q, count + 1)
    }
  }
}
