import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;


alias Tuple!(int, int, int) bar;
void main() {
	auto nma = map!(to!int)(readln().strip().split());
	while(nma[0]|nma[1]|nma[2]) {
		int n = nma[0];
		int m = nma[1];
		int a = nma[2];
		auto t = redBlackTree!(bar)();
		foreach(i; 0..m){
			auto hpq = map!(to!int)(readln().strip().split());
			int h = hpq[0];
			int p = hpq[1];
			int q = hpq[2];
			t.insert(bar(h,p,q));
		}
		foreach_reverse(b; t) {
			int p = b[1];
			int q = b[2];
			if(a == p || a == q) {
				a ^= p;
				a ^= q;
			}
		}
		writeln(a);
		nma = map!(to!int)(readln().strip().split());
	}
}