#include<bits/stdc++.h>usingnamespacestd;
constint maxn = 1e5 + 5;
structedge {int to, dis, pre;
}a[maxn];
int head[maxn];
int cnt;
voidadd_edges(int u, int v, int w){
a[++cnt].to= v;
a[cnt].dis = w;
a[cnt].pre = head[u];
head[u] = cnt;
}
voidprint_edges(int root){
for (int i = head[root]; i; i = a[i].pre) {
cout <<a[i].to << ' ';
}
}
intmain(){
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v, w;
cin >> u >> v >> w;
add_edges(u, v, w);
add_edges(v, u, w);
}
for (int i = 1; i <= n; i++) {
print_edges(i);
}
}