loop {
    n, m, a = gets.split.map &:to_i
    break if n == 0
    result = (0..n).to_a
    h_lines = (1..m).map{ gets.split.map &:to_i }.sort_by(&:first).reverse
    h_lines.each{|h, p, q|
        tmp = result[p]
        result[p] = result[q]
        result[q] = tmp
    }
    p result.index(a)
}