#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
const double PI = 3.141592653589793;
string s[10000];
int m;
int d[10000];
int v[10000];
vector <int> e[10000];
int inf = 1e9;
void dfs(int u){
if(v[u]) return;
v[u] = 1;
for(auto v : e[u]){
d[v] = min(d[v], d[u] + 1);
dfs(v);
}
}
int main() {
fast;
cin >> m;
int y = 0; // Вспомогательная переменная для заполнения массива s[i]
for(int i = 0; i < m; i++){
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
int x1 = -1, x2 = -1;
// Находим индексы в уже имеющемся массиве где s[i] - хранит название элемента, а i это его номер
for(int j = 0; j < 1000; j++){
if(s1 == s[j]) x1 = j;
if(s3 == s[j]) x2 = j;
}
// Если не нашли название в массиве s[i], то добаляем
if(x1 == -1){
x1 = y;
s[y] = s1;
y++;
}
if(x2 == -1){
x2 = y;
s[y] = s3;
y++;
}
e[x1].pb(x2);
}
string st, f;
cin >> st >> f;
int u = -1, v = -1;
//Считываем название элементов и ищем индексы в массиве s[i]
for(int i = 0; i < 1000; i++)
d[i] = inf;
for(int i = 0; i < 1000; i++){
if(s[i] == st) u = i;
if(s[i] == f) v = i;
}
// Если какое - то название не нашли в массиве s[i] значит его нет и в графе -> ответ -1
if(u == -1 or v == -1){
cout << -1;
return 0;
}
d[u] = 0;
dfs(u);
if(d[v] == inf) cout << -1;
else cout << d[v];
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiAgICAgCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAgICAgCiNkZWZpbmUgZmFzdCBpb3M6OnN5bmNfd2l0aF9zdGRpbygwKTtjaW4udGllKDApO2NvdXQudGllKDApCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiAgICAKY29uc3QgZG91YmxlIFBJID0gMy4xNDE1OTI2NTM1ODk3OTM7CiAKc3RyaW5nIHNbMTAwMDBdOwppbnQgbTsKaW50IGRbMTAwMDBdOwppbnQgdlsxMDAwMF07CnZlY3RvciA8aW50PiBlWzEwMDAwXTsKaW50IGluZiA9IDFlOTsKIAp2b2lkIGRmcyhpbnQgdSl7CiAgICBpZih2W3VdKSByZXR1cm47CiAgICB2W3VdID0gMTsKICAgIGZvcihhdXRvIHYgOiBlW3VdKXsKICAgICAgICBkW3ZdID0gbWluKGRbdl0sIGRbdV0gKyAxKTsKICAgICAgICBkZnModik7CiAgICB9Cn0KIAppbnQgbWFpbigpIHsKICAgIGZhc3Q7CiAgICBjaW4gPj4gbTsKICAgIGludCB5ID0gMDsgLy8g0JLRgdC/0L7QvNC+0LPQsNGC0LXQu9GM0L3QsNGPINC/0LXRgNC10LzQtdC90L3QsNGPINC00LvRjyDQt9Cw0L/QvtC70L3QtdC90LjRjyDQvNCw0YHRgdC40LLQsCBzW2ldCiAgICBmb3IoaW50IGkgPSAwOyBpIDwgbTsgaSsrKXsKICAgICAgICBzdHJpbmcgczEsIHMyLCBzMzsKICAgICAgICBjaW4gPj4gczEgPj4gczIgPj4gczM7CiAgICAgICAgaW50IHgxID0gLTEsIHgyID0gLTE7CiAgICAgICAgLy8g0J3QsNGF0L7QtNC40Lwg0LjQvdC00LXQutGB0Ysg0LIg0YPQttC1INC40LzQtdGO0YnQtdC80YHRjyDQvNCw0YHRgdC40LLQtSDQs9C00LUgc1tpXSAtINGF0YDQsNC90LjRgiDQvdCw0LfQstCw0L3QuNC1INGN0LvQtdC80LXQvdGC0LAsINCwIGkg0Y3RgtC+INC10LPQviDQvdC+0LzQtdGACiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IDEwMDA7IGorKyl7CiAgICAgICAgICAgIGlmKHMxID09IHNbal0pIHgxID0gajsKICAgICAgICAgICAgaWYoczMgPT0gc1tqXSkgeDIgPSBqOwogICAgICAgIH0KICAgICAgICAvLyDQldGB0LvQuCDQvdC1INC90LDRiNC70Lgg0L3QsNC30LLQsNC90LjQtSDQsiDQvNCw0YHRgdC40LLQtSBzW2ldLCDRgtC+INC00L7QsdCw0LvRj9C10LwKICAgICAgICBpZih4MSA9PSAtMSl7CiAgICAgICAgICAgIHgxID0geTsKICAgICAgICAgICAgc1t5XSA9IHMxOwogICAgICAgICAgICB5Kys7CiAgICAgICAgfQogICAgICAgIGlmKHgyID09IC0xKXsKICAgICAgICAgICAgeDIgPSB5OwogICAgICAgICAgICBzW3ldID0gczM7CiAgICAgICAgICAgIHkrKzsKICAgICAgICB9ICAgCiAgICAgICAgZVt4MV0ucGIoeDIpOwogICAgICAgICAKICAgIH0KICAgIHN0cmluZyBzdCwgZjsKICAgIGNpbiA+PiBzdCA+PiBmOwogICAgCiAgICBpbnQgdSA9IC0xLCB2ID0gLTE7CiAgICAvL9Ch0YfQuNGC0YvQstCw0LXQvCDQvdCw0LfQstCw0L3QuNC1INGN0LvQtdC80LXQvdGC0L7QsiDQuCDQuNGJ0LXQvCDQuNC90LTQtdC60YHRiyDQsiDQvNCw0YHRgdC40LLQtSBzW2ldCiAgICBmb3IoaW50IGkgPSAwOyBpIDwgMTAwMDsgaSsrKQogICAgICAgIGRbaV0gPSBpbmY7CiAgICBmb3IoaW50IGkgPSAwOyBpIDwgMTAwMDsgaSsrKXsKICAgICAgICBpZihzW2ldID09IHN0KSB1ID0gaTsKICAgICAgICBpZihzW2ldID09IGYpIHYgPSBpOwogICAgfQogICAgLy8g0JXRgdC70Lgg0LrQsNC60L7QtSAtINGC0L4g0L3QsNC30LLQsNC90LjQtSDQvdC1INC90LDRiNC70Lgg0LIg0LzQsNGB0YHQuNCy0LUgc1tpXSDQt9C90LDRh9C40YIg0LXQs9C+INC90LXRgiDQuCDQsiDQs9GA0LDRhNC1IC0+INC+0YLQstC10YIgLTEKICAgIGlmKHUgPT0gLTEgb3IgdiA9PSAtMSl7CiAgICAgICAgY291dCA8PCAtMTsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIGRbdV0gPSAwOwogICAgZGZzKHUpOwogICAgaWYoZFt2XSA9PSBpbmYpIGNvdXQgPDwgLTE7CiAgICBlbHNlIGNvdXQgPDwgZFt2XTsKICAgIHJldHVybiAwOwp9
NQpBcXVhIC0mZ3Q7IEFxdWFWaXRhCkFxdWFWaXRhIC0mZ3Q7IFBoaWxvc29waGVyU3RvbmUKQXF1YVZpdGEgLSZndDsgQXJnZW50dW0KQXJnZW50dW0gLSZndDsgQXVydW0KQXF1YVZpdGEgLSZndDsgQXVydW0KQXF1YQpBdXJ1bQ==
5
Aqua -> AquaVita
AquaVita -> PhilosopherStone
AquaVita -> Argentum
Argentum -> Aurum
AquaVita -> Aurum
Aqua
Aurum