#20131028
#n,m,a  |,-,num
#h,p,q  height,num1,num2

n,m,a = gets.split(" ").map{|e|e.to_i}
array=Array.new m
array.length.times{|e|array[e]=[]}

while true
	h,p,q = gets.split(" ").map{|e|e.to_i}
	break if h==0 && p==0 && q==0

	array[h].push [p,q]
	
end

(1..m).each{|height|
	height=m-height
	array[height].each{|e|
		if e[1]==a
			a=e[0]
		elsif e[0]==a
			a=e[1]
		end
	}
}
p a