#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=10010;
const int Max=100010;
bool vis[maxn]={false};
struct node{
int address,data,next,order;
}list[Max];
int N,head;
bool cmp(node a, node b){
return a.order<b.order;
}
int main(void){
scanf("%d%d",&head,&N);
for(int i=0;i<Max;i++) list[i].order=Max;
for(int i=0;i<N;i++){
int ad;
scanf("%d",&ad);
scanf("%d%d",&list[ad].data,&list[ad].next);
list[ad].address=ad;
}
int cnt=0,p=head,cnt1=0;
while(p!=-1){
int n=abs(list[p].data);
if(!vis[n]){
list[p].order=cnt++;
vis[n]=true;
}else{
list[p].order=maxn+cnt1++;
}
p=list[p].next;
}
sort(list,list+Max,cmp);
for(int i=0;i<cnt;i++){
printf("%05d %d",list[i].address,list[i].data);
if(i!=cnt-1) printf(" %05d\n",list[i+1].address);
else printf(" -1\n");
}
for(int i=cnt;i<cnt+cnt1;i++){
printf("%05d %d",list[i].address,list[i].data);
if(i!=cnt+cnt1-1) printf(" %05d\n",list[i+1].address);
else printf(" -1");
}
return 0;
}
06-01
134
01-28
159
07-20
221