#include <stdio.h>

int main (void){
    
    int judge = 0;
    int define_x,define_y,search;
    int i = 0;
    int position_x[1050] = {-1};
    int position_y[1050] = {-1};
    int bridge_number[1050] = {-1};
    
    while(scanf("%d%d%d",&define_y,&define_x,&search), define_y+define_x+search){
    
        for(i = 0; i<define_x;i++)
        {
            scanf("%d%d%d",&position_y[i],&position_x[i],&bridge_number[i]);
            if((position_y[i] == 0 && position_x[i] == 0) && bridge_number[i] == 0)
                {
                    judge = i;
                }
        }
            
    define_y = define_y-1;
    while(define_y != 0)
    {
        for(i = 0;i<define_x;i++)
        {
            if(position_y[i] == define_y)
            {
                if (position_x[i] == search)
                {
                    search = bridge_number[i];
                }
                
            }
        }
        define_y --;
    }
    printf("%d\n",search);
    }
    return 0;
    
}