class Amida
  def initialize n
    @vlines = Array.new(n+1).map{Array.new(1000, nil)}
  end

  def set_hline h, v1, v2
    @vlines[v1][h] = v2
    @vlines[v2][h] = v1
  end

  def solve a
    now = a
    start = @vlines[now].size
    start.downto(0) do |i|
      next if @vlines[now][i].nil?
      now = @vlines[now][i]
    end
    puts now
  end
end

def main
  loop do
    n,m,a = gets.chomp.split.map(&:to_i)
    break if (n|m|a) == 0
    amida = Amida.new(n)
    m.times do
      h,p,q = gets.chomp.split.map(&:to_i)
      amida.set_hline(h, p, q)
    end
    amida.solve(a)
  end
end

main