#include <stdio.h>
#include <stdlib.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)

int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}

ll a[1010];
int main(){
	ll n,s;
	while(scanf("%*lld%lld%lld",&n,&s),n){
		rep(i,0,n){
			ll h,p,q;
			scanf("%lld%lld%lld",&h,&p,&q);
			a[i]=h*101*101+p*101+q;
		}
		sortdown(a,n);
		rep(i,0,n){
			ll p=a[i]/101%101;
			ll q=a[i]%101;
			if(p==s)s=q;
			else if(q==s)s=p;
		}
		printf("%lld\n",s);
	}
}


