def move
  $arr.sort { |a, b| b[0] <=> a[0] }.each do |e|
    if $pos == e[1]
      $pos = e[2]
    elsif $pos == e[2]
      $pos = e[1]
    end
  end
end

loop do
  n, m, $pos = gets.split.map(&:to_i)
  break if n == 0 && m == 0 && $pos == 0
  $arr = []
  m.times do
    $arr << gets.split.map(&:to_i)
  end
  move
  puts $pos
end