#include <bits/stdc++.h>
using namespace std;

#define fo(i, x, y) for (int i = (x); i <= (y); ++i)
#define fd(i, x, y) for (int i = (x); i >= (y); --i)

const int maxn = 2e5 + 5;

struct edge{int to, w; edge *nxt;}*arc1[maxn], *arc2[maxn], pool[maxn << 2], *pt = pool;
int n, m;
int ans[maxn];
bool vis[maxn];

int getint()
{
	char ch;
	int res = 0, p;
	while (!isdigit(ch = getchar()) && (ch ^ '-'));
	p = ch == '-'? ch = getchar(), -1 : 1;
	while (isdigit(ch))
		res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
	return res * p;
}

void addedge(edge *arc[], int u, int v, int w)
{
	*pt = (edge){v, w, arc[u]};
	arc[u] = pt++;
}

void gettr(int x)
{
	vis[x] = true;
	for (edge *e = arc1[x]; e; e = e->nxt)
		if (!vis[e->to])
		{
			addedge(arc2, x, e->to, e->w);
			gettr(e->to);
		}
}

void dfs(int x)
{
	for (edge *e = arc2[x]; e; e = e->nxt)
	{
		if (e->w != ans[x])
			ans[e->to] = e->w;
		else
			ans[e->to] = e->w - 1? e->w - 1 : e->w + 1;
		dfs(e->to);
	}
}

int main()
{
	n = getint(); m = getint();
	fo(i, 1, m)
	{
		int u, v, w;
		u = getint(); v = getint(); w = getint();
		addedge(arc1, u, v, w);
		addedge(arc1, v, u, w);
	}
	gettr(1);
	ans[1] = 1;
	dfs(1);
	fo(i, 1, n) printf("%d\n", ans[i]);
	return 0;
}
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
long long n, m, c, u, v, l;
vector<long long>g[100000];
int b[100000];
int x[100000];
int y[100000];
void dfs(int a, int p, int q) {
	b[a] = 1;
	int wa, wp, wq;
	for (int i = 0; i < g[a].size(); i++) {
		wa = g[a][i] % n;
		wp = g[a][i] / n;
		if (b[wa])continue;
		if (p == wp)wq = q + 1;
		else wq = 0;
		if (wq % 2 == 0) {
			x[wa] = wp;
		}
		dfs(wa, wp, wq);
	}
}
int main() {
	scanf("%lld%lld", &n, &m);
	for (long long i = 0; i < m; i++) {
		scanf("%lld%lld%lld", &u, &v, &c);
		c--;
		u--;
		v--;
		g[u].push_back(c * n + v);
		g[v].push_back(c * n + u);
	}
	for (long long i = 0; i < n; i++)x[i] = -1;
	dfs(0, -1, 0);
	for (int i = 0; i < n; i++) {
		if (x[i] >= 0)y[x[i]]++;
	}
	while (y[l])l++;
	for (int i = 0; i < n; i++) {
		if (x[i] == -1)x[i] = l;
	}
	for (int i = 0; i < n; i++) {
		printf("%d\n", x[i] + 1);
	}
}