#include <iostream>
#include <vector>
#include <deque>
#include <numeric>
#define pii pair<int, int>
using namespace std;
int n;
int m;
vector<int> a;
vector<int> b;
deque<bool> c;
vector<int> p;
// vector<int> sz;
vector<int> res;
vector<vector<pii>> graph;
vector<pii> fa;
vector<int> h;
int P(int a) {
if (p[a] == a) {
return a;
}
return p[a] = P(p[a]);
}
void MergeNoCheckSimple(int a, int b) {
a = P(a);
b = P(b);
p[b] = a;
}
// void Merge(int a, int b) {
// a = P(a);
// b = P(b);
// if (a != b) {
// if (sz[a] < sz[b]) {
// swap(a, b);
// }
// sz[a] += sz[b];
// p[b] = a;
// }
// }
void Dfs(int x) {
// cout << x << endl;
for (const auto& [y, v] : graph[x]) {
if (y != fa[x].first) {
fa[y] = { x, v };
h[y] = h[x] + 1;
Dfs(y);
}
}
}
void Solve() {
cin >> n >> m;
a.resize(m);
b.resize(m);
c.resize(m);
p.resize(n + 1);
// sz.assign(n + 1, 1);
res.resize(m);
graph.resize(n + 1);
fa.resize(n + 1);
h.resize(n + 1);
iota(p.begin() + 1, p.end(), 1);
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i];
if (c[i]) {
graph[a[i]].push_back({ b[i], i });
graph[b[i]].push_back({ a[i], i });
}
}
// cout << "DFS begin" << endl;
Dfs(1);
// cout << "DFS end" << endl;
int cc = 1;
for (int i = 0; i < m; i++) {
// cout << i << endl;
if (!res[i]) {
if (c[i]) {
if (fa[a[i]].first == b[i]) {
MergeNoCheckSimple(b[i], a[i]);
}
else {
MergeNoCheckSimple(a[i], b[i]);
}
res[i] = cc;
cc++;
}
else {
int x = P(a[i]);
int y = P(b[i]);
vector<int> cur;
while (x != y) {
// cout << i << ',' << x << ',' << y << endl;
// cout << "Add " << x << endl;
if (h[x] < h[y]) {
swap(x, y);
}
// cout << x << ',' << fa[x].second << endl;
cur.push_back(fa[x].second);
// res[fa[x].second] = cc;
// cc++;
MergeNoCheckSimple(fa[x].first, x);
x = P(x);
}
sort(cur.begin(), cur.end());
for (const auto& j : cur) {
res[j] = cc;
cc++;
}
res[i] = cc;
cc++;
}
}
cout << res[i] << ' ';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
Solve();
return 0;
}