def move
  $arr.sort.reverse.each do |list|
    tmp = list[2] if $pos == list[1]
    tmp = list[1] if $pos == list[2]
    $pos = tmp unless tmp.nil?
  end
end

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