Note
Code
#include<bits/stdc++.h>
using namespace std;
struct Node{
char data;
int next;
bool flag;
Node(){
flag=false;
}
}node[100005];
int main(){
#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif
int w1,w2,n;
char ch;
cin>>w1>>w2>>n;
int add1,add2;
while(n--){
scanf("%05d %c %05d",&add1,&ch,&add2);
node[add1].data=ch;
node[add1].next=add2;
}
while(w1!=-1){
node[w1].flag=true;
w1=node[w1].next;
}
while(w2!=-1){
if(node[w2].flag==true) break;
w2=node[w2].next;
}
if(w2!=-1) printf("%05d",w2);
else printf("-1");
return 0;
}