n = gets.to_i
a = gets.split.map(&:to_i)
sorted = a.sort
s = (0..n-1).count {|i| a[i] == sorted[i]}
if s == n
    p 0
else
    ans = -1
    gets.to_i.times do |k|
        i, j = gets.split.map(&:to_i)
        i -= 1
        j -= 1
        s -= 1 if a[i] == sorted[i]
        s -= 1 if a[j] == sorted[j]
        a[i], a[j] = a[j], a[i]
        s += 1 if a[i] == sorted[i]
        s += 1 if a[j] == sorted[j]
        if s == n
            ans = k + 1
            break
        end
    end
    p ans
end
