#include <stdio.h>
int main(){

  int n,m,a,i,j,h,p,q,c=1;
  int  chart[2000][300]={{}};
  int  count[2000][300]={{}};

  for(;;){
    scanf("%d %d %d",&n,&m,&a);

    if(n==0&&m==0&&a==0){
      break;
    }
    for(i=1;i<=m;i++){
      scanf("%d %d %d",&h,&p,&q);

      chart[h][p]=1;
      chart[h][q]=1;
      count[h][p]=c;
      count[h][q]=c;
      c++;
    }
    for(i=1;i<=4;i++){
      for(j=1;j<=4;j++){
	// printf("%d %d\n",chart[i][j],count[i][j]);
      }
    }


 
    for(i=4;i>=0;i--){
      if(chart[i][a]==1&&chart[i][a+1]==1&&count[i][a]==count[i][a+1]){
	a+=1;
	continue;
      }
      else if(chart[i][a]==1&&chart[i][a-1]==1&&count[i][a]==count[i][a-1]){
	a-=1;
	continue;
      }
      //printf("%d\n",a);
    }

    printf("%d\n",a);
    c=1;
    for(i=1;i<=1001;i++){
      for(j=1;j<=101;j++){
	chart[i][j]=0;
	count[i][j]=0;
      }
    }   
  }
  return 0;
}