import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;

void main()
{
	while (1) {
		auto x = readln.chomp.split.map!(to!int);
		if (!x[0]) break;
		auto m = x[1], a = x[2];
		auto lines = new int[][](m, 3);
		foreach (i; 0..m) {
			lines[i] = readln.chomp.split.map!(to!int).array;
		}
		lines = lines.sort!("a[0] > b[0]").array;
		int h;
		foreach (e; lines) {
			if (e[1] == a) {
				a = e[2];
			} else if (e[2] == a) {
				a = e[1];
			}
		}
		a.writeln;
	}
}import std.stdio;
import std.conv;
import std.string;
import std.typecons;
import std.algorithm;
import std.array;
import std.range;
import std.math;
import std.container;
import std.datetime;

void main()
{
	while (1) {
		auto x = readln.chomp.split.map!(to!int);
		if (!x[0]) break;
		auto m = x[1], a = x[2];
		auto lines = new int[][](m, 3);
		foreach (i; 0..m) {
			lines[i] = readln.chomp.split.map!(to!int).array;
		}
		lines = lines.sort!("a[0] > b[0]").array;
		int h;
		foreach (e; lines) {
			if (e[1] == a) {
				a = e[2];
			} else if (e[2] == a) {
				a = e[1];
			}
		}
		a.writeln;
	}
}