#
# Amida, the City of Miracle
#

while line=gets

  n, m, a=line.split.map(&:to_i)

  if (n|m|a)==0
    next
  end

  lots=Array.new(n+1).map { Array.new(m+1) }

  m.times do
    h, p, q=gets.split.map(&:to_i)
    lots[q][h]=p
    lots[p][h]=q
  end

  m.downto(1) do |i|
    unless (lots[a][i]).nil?
      a=lots[a][i]
    end
  end

  puts a

end