#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	int n, m, a, i;
	scanf("%d%d%d", &n, &m, &a);
	int *data[1000];
	data[0] = (int*)calloc(sizeof(int), 1000 * n);
	for (i = 1; i<1000; i++)
		data[i] = data[0] + n * i;
	while (1)
	{
		int h, from, to;
		scanf("%d%d%d", &h, &from, &to);
		if ((h | from | to) == 0)
			break;
		data[h - 1][from - 1] = to;
		data[h - 1][to - 1] = from;
	}
	for (i = 1000; i>0; i--)
	{
		if (data[i - 1][a - 1] == 0)
			continue;
		a = data[i - 1][a - 1];
	}
        free(data[0]);
	printf("%d\n", a);
	return 0;
}