tailrec fun solve():Unit{
    val (n, m, a) = readLine()!!.split(' ').map(String::toInt)
    if (n == 0 && m == 0 && a == 0) return
    var pos = a
    Array(m){
        val (h, p, q) = readLine()!!.split(' ').map(String::toInt)
        Border(h, p, q)
    }.sortedByDescending(Border::height).forEach {
        if (it.a == pos){
            pos = it.b
        }else if (it.b == pos){
            pos = it.a
        }
    }
    println(pos)
    solve()
}
fun main(args:Array<String>):Unit {
    solve()
}
data class Border(val height:Int, val a:Int, val b:Int)
