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

int main(void) {
	int n,m,a,h,p,q,i;
	char amida[1000][100] = {};
	scanf("%d %d %d",&n,&m,&a);
	while(1){
		scanf("%d %d %d",&h,&p,&q);
		if (h == 0 && p == 0 && q == 0){
			break;
		}
		h = m - h - 1,p = p - 1,q = q - 1;
		amida[h][p] = 1,amida[h][q] = 1;
	}
	n = n - 1,m = m - 1,a = a - 1;
	for (i = 0;i <= m;i++){
		if (a == 0 || a == n){
			if(a == 0){
				if(amida[i][a] == 1){
					a = a + 1;
				}
			}
			else{
				if(amida[i][a] == 1){
					a = a - 1;
				}
			}
		}
		else{
			if(amida[i][a] == 1){
				if(amida[i][(a + 1)] == 1){
					a = a + 1;
				}
				else{
					a = a - 1;
				}
			}
		}
	}
	a = a + 1;
	printf("%d\n",a);
	return 0;
}