n = gets.to_i
arr = gets.chomp.split.map(&:to_i)
arr2 = arr.sort
wrong = 0
arr.zip(arr2){|x, y| wrong += 1 if x != y}
sorted = false
if wrong == 0
    sorted = true
    puts 0
else
    gets.to_i.times{|i|
        x, y = gets.chomp.split.map{|x| x.to_i - 1}
        arr[x], arr[y] = arr[y], arr[x]
        wrong -= 1 if arr2[x] == arr[x]
        wrong += 1 if arr2[x] == arr[y]
        wrong -= 1 if arr2[y] == arr[y]
        wrong += 1 if arr2[y] == arr[x]
        if wrong == 0
            sorted = true
            puts i + 1
            break
        end
    }
end
puts -1 unless sorted

