import Data.Ord
import Data.List

main = interact $ unlines . map (show . amida) . read' . lines
 where
  read' ("0 0 0":_) = []
  read' (x:xs) = (a,hpqs) : read' xs'
   where
    [n,m,a] = map read (words x)
    (hpqs', xs') = splitAt m xs
    hpqs = map ((\[h,p,q]->(h,(p,q))) . map read . words) hpqs'

amida (a,hpqs) = election a ((reverse . sortBy (comparing fst)) hpqs)
 where
  election a' [] = a'
  election a' ((h,(p,q)):hpqs)
   | p == a' = election q (dropWhile ((==h) . fst) hpqs)
   | q == a' = election p (dropWhile ((==h) . fst) hpqs)
   | otherwise = election a' hpqs