#include
#include "cstring"
const int maxn=100010;
struct NODE{
char data;
int next;
bool flag;
}node[maxn];
int main(){
for(int i=0;i<maxn;i++){
node[i].flag= false;
}
int s1,s2,n;
scanf("%d%d%d",&s1,&s2,&n);
int address,next;
char data;
for(int i=0;i<n;i++){
scanf("%d %c %d",&address,&data,&next);
node[address].data=data;
node[address].next=next;
}
int p;
for(p=s1;p!=-1;p=node[p].next){
node[p].flag= true;
}
for(p=s2;p!=-1;p=node[p].next){
if(node[p].flag==true) break;
}
if(p!=-1){
printf("%05d\n",p);
} else{
printf("-1\n");
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49