
#include<bits/stdc++.h>
using namespace std;
int n,m,p,a[100001],b[100001],c[100001];
int main(){
cin>>n>>m>>p;
for(int q=0;q<n;q++) cin>>a[q]>>b[q];
while(!c[p]){
int op=0;
c[p]=1;
for(int q=0;q<n;q++) if(b[q]==p) {p=a[q];op=1;break;}
if(op==0){cout<<p;exit(0);}
}
cout<<"-1";
return 0;
}