#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct node{
int adress, data, next;
} nodes[100001];
bool cmp(node a, node b){
return a.data < b.data;
}
int main(){
int n, s;
cin >> n >> s;
vector<node> v;
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
nodes[a] = {a, b, c};
}
int head = s;
while (head != -1) {
v.push_back(nodes[head]);
head = nodes[head].next;
}
sort(v.begin(), v.end(), cmp);
if (v.size() == 0) {
cout << "0 -1\n";
}else{
cout << v.size() << ' ' << v[0].adress << endl;
for (int i = 0; i < v.size(); i++) {
if(i == 0) printf("%05d %d ", v[i].adress, v[i].data);
else printf("%05d\n%05d %d ", v[i].adress, v[i].adress, v[i].data);
}
cout << "-1\n";
}
return 0;
}