#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n, m, fa[N], ans[N], vis[N];
vector<pair<int, int> > G[N];
int Find(int x) {
	return fa[x] == x ? x : fa[x] = Find(fa[x]);
}
void dfs(int u, int lst) {
	for (auto &e : G[u]) {
		int v = e.first;
		if (v == lst) continue;
		if (ans[u] == e.second) ans[v] = -1;
		else ans[v] = e.second;
		dfs(v, u);
	}
	if (ans[u] == -1) {
		for (auto &e : G[u]) {
			int v = e.first;
			if (v == lst) continue;
			vis[ans[v]] = 1;
		}
		if (ans[lst] >= 1) vis[ans[lst]] = 1;
		for (int i = 1; i <= n; i++) if (!vis[i]) {
			ans[u] = i;
			break;
		}
		for (auto &e : G[u]) {
			int v = e.first;
			if (v == lst) continue;
			vis[ans[v]] = 0;
		}
		if (ans[lst] >= 1) vis[ans[lst]] = 0;
	}
}
int main() {
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) fa[i] = i;
	for (int i = 1; i <= m; i++) {
		int u, v, c;
		scanf("%d%d%d", &u, &v, &c);
		if (Find(u) != Find(v)) {
			fa[Find(u)] = Find(v);
			G[u].emplace_back(v, c);
			G[v].emplace_back(u, c);
		}
	}
	ans[1] = -1;
	dfs(1, 0);
	for (int i = 1; i <= n; i++) printf("%d\n", ans[i]);
	return 0;
}
/* by Hyperhydrochloric Acid */
#include <bits/stdc++.h>
using namespace std;
#define SZ(x)               ((int)(x).size())
#define all(x)              (x).begin(), (x).end()
#define loop(i, n)          for(int i = 0; i < (n); ++i)
#define cont(i, n)          for(int i = 1; i <= (n); ++i)
#define circ(i, a, b)       for(int i = (a); i <= (b); ++i)
#define range(i, a, b, c)   for(int i = (a); ((c) > 0 ? i <= (b) : i >= (b)); i += (c))
#define foreach(it, v)      for(__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define y0          y0O0OO00OO0OO0OO0OOO00OO0OO0O0O000OO0
#define y1          y1II11II11III11I1III11II111IIII1II1I1
#define pub         push_back
#define pob         pop_back
#define mak         make_pair
typedef long long   ll;
typedef long double lf;
const int   Inf = 0x3f3f3f3f;
const ll    INF = 0x3f3f3f3f3f3f3f3fll;
/* Source code starts here */

int n, m;
vector<pair<int, int> > nei[1 << 17];
bool lgl[1 << 17];
int res[1 << 17];

void inline dfs(int now, int col, bool sme = 0) {
	if(!sme) res[now] = col;
	else res[now] = col % n + 1;
	lgl[now] = 1;
	loop(i, SZ(nei[now])) {
		int to = nei[now][i].first, co = nei[now][i].second;
		if(lgl[to]) continue;
		dfs(to, co, co == res[now]);
	}
}

int main() {
	scanf("%d%d", &n, &m);
	cont(i, m) {
		int a, b, c; scanf("%d%d%d", &a, &b, &c);
		nei[a].pub(mak(b, c)); nei[b].pub(mak(a, c));
	}
	dfs(1, 1);
	cont(i, n) printf("%d\n", res[i]);
	return 0;
}