import java.text.DecimalFormat;
import java.util.Scanner;
class Node{
int address;
int data;
int nextAddress;
Node next;
public Node(){
}
public Node(Node next){
this.next = next;
}
public Node(int address,int data,int nextAddress){
this.address = address;
this.data = data;
this.nextAddress = nextAddress;
}
}
public class Main{
private static Node[] nodes = new Node[100005];
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int headAddress = scan.nextInt();
int N = scan.nextInt();
int K = scan.nextInt();//反转的节点数
for(int i=0;i<N;i++){
int address = scan.nextInt();
int data = scan.nextInt();
int nextAddress = scan.nextInt();
nodes[address] = new Node(address,data,nextAddress);
}
Node head = createLink(headAddress);
Node newHead = resver(head,K);
System.out.println("-----------------------------------");
DecimalFormat df = new DecimalFormat("00000");
while(newHead!=null){
System.out.println(df.format(newHead.address)+" "+newHead.data+" "+(newHead.nextAddress==-1?-1:df.format(newHead.nextAddress)));
newHead = newHead.next;
}
}
public static Node resver(Node head,int K){
Node newNode,oldNode,temp;
newNode = head.next;
oldNode = newNode.next;
int cnt = 1;
int tmp;
while(cnt<K){
temp = oldNode.next;
oldNode.next = newNode;
oldNode.nextAddress = newNode.address;
newNode = oldNode;
oldNode = temp;
cnt++;
}
head.next.next = oldNode;
head.next.nextAddress = -1;
return newNode;
}
public static Node createLink(int headAddress){
Node head = new Node();
Node finalHead = head;
int address = headAddress;
int data,nextAddress;
while(address!=-1){
head.next = nodes[address];
address = nodes[address].nextAddress;
head = head.next;
}
return finalHead;
}
}
00100 6 600000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
-----------------------------------
68237 6 99999
99999 5 00000
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 -1