fun main(args: Array<String>) {
    readLine()!!.toInt()
    val arr = readLine()!!.split(" ").map { it.toInt() }.toMutableList()
    val sorted = arr.sorted()
    var cnt = 0
    for(i in arr.indices) if(arr[i] != sorted[i]) cnt++
    if(cnt == 0) {
        println(0)
        return
    }
    val q = readLine()!!.toInt()
    for(i in 0 until q) {
        val (x, y) = readLine()!!.split(" ").map { it.toInt() - 1 }
        if(arr[x] != sorted[x]) cnt--
        if(arr[y] != sorted[y]) cnt--
        val tmp = arr[x]
        arr[x] = arr[y]
        arr[y] = tmp
        if(arr[x] != sorted[x]) cnt++
        if(arr[y] != sorted[y]) cnt++
        if(cnt == 0) {
            println(i + 1)
            return
        }
    }
    println(-1)
}
