#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<vector>
using namespace std;
struct Node
{
string address;
int key;
string next;
bool operator< (const Node& t) const
{
return key<t.key;
}
};
int main()
{
int n;
char head[10];
scanf("%d%s", &n, head);
unordered_map<string, Node> map;
char address[10], next[10];
while (n--)
{
int key;
scanf("%s%d%s", address, &key, next);
map[address] = { address,key,next };
}
vector<Node> nodes;
for (string i = head; i != "-1"; i = map[i].next) nodes.push_back(map[i]);
printf("%d ", nodes.size());
if (nodes.empty()) printf("-1\n");
else
{
sort(nodes.begin(), nodes.end());
printf("%s\n", nodes[0].address.c_str());
for (int i = 0; i<nodes.size(); i++)
{
if (i + 1 == nodes.size())
printf("%s %d -1\n", nodes[i].address.c_str(), nodes[i].key);
else
printf("%s %d %s\n", nodes[i].address.c_str(), nodes[i].key, nodes[i + 1].address.c_str());
}
}
return 0;
}
#include<iostream>
#include<vector>
#include<unordered_map>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100010;
vector<int> v;
int n,h;
int e[N],ne[N];
int main()
{
unordered_map<int,int> mp;
cin>>n>>h;
for(int i=0;i<n;i++)
{
int address,key,next;
cin>>address>>key>>next;
e[address]=key,ne[address]=next;
mp[key]=address;
}
for(int i=h;i!=-1;i=ne[i])
v.push_back(e[i]);
sort(v.begin(),v.end());
cout<<v.size();
if(v.size()==0) cout<<" -1"<<endl;
for(int i=0;i<v.size();i++)
{
if(!i) printf(" %05d\n",mp[v[i]]);
if(i!=v.size()-1) printf("%05d %d %05d\n",mp[v[i]],v[i],mp[v[i+1]]);
else printf("%05d %d -1",mp[v[i]],v[i]);
}
return 0;
}