import java.io.*; import java.util.*; class B { public static void main(String[] args) throws Exception { Scanner cin = new Scanner(System.in); while(true) { int n = cin.nextInt(); int m = cin.nextInt(); int a = cin.nextInt(); if(n == 0 && m == 0 && a == 0) { break; } Swapper[] s = new Swapper[m]; for(int i = 0; i < m; i++) { int y = cin.nextInt(); int u = cin.nextInt(); int v = cin.nextInt(); s[i] = new Swapper(u, v, y); } Arrays.sort(s); for(int i = m - 1; i >= 0; i--) { a = s[i].move(a); } System.out.println(a); } } private static class Swapper implements Comparable { private int u, v, y; public Swapper(int u, int v, int y) { this.u = u; this.v = v; this.y = y; } public int compareTo(Swapper that) { if(this.y != that.y) { return this.y - that.y; } if(this.u != that.u) { return this.u - that.u; } if(this.v != that.v) { return this.v - that.v; } return 0; } public int move(int a) { if(a == u || a == v) return a ^ u ^ v; return a; } } }