include[HTML_REMOVED]
using namespace std;
const int N = 100010;
int n;
int h1, h2, ne[N];
char e[N];
bool vis[N];
int main() {
cin >> h1 >> h2 >> n;
for(int i = 0; i < n; ++i) {
int address, next;
char data;
cin >> address >> data >> next;
e[address] = data, ne[address] = next;
}
for(int i = h1; ~i; i = ne[i])
vis[i] = true;
for(int i = h2; ~i; i = ne[i]) {
if(vis[i]) {
printf(“%05d\n”, i);
return 0;
}
}
puts(“-1”);
}