#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

constexpr int MAX_N = 100000;

int n, m;
vector<pair<int, int>> G[MAX_N];
int ans[MAX_N];
bool reached[MAX_N];

void dfs(int cur) {
    reached[cur] = true;
    for (auto p : G[cur]) {
        if (reached[p.first]) continue;
        if (ans[cur] != p.second)
            ans[p.first] = p.second;
        else
            ans[p.first] = (p.second + 1) % n + 1;
        dfs(p.first);
    }
}

signed main() {
    cin >> n >> m;
    int u, v, c;
    rep(i, m) {
        cin >> u >> v >> c;
        u--;
        v--;
        G[u].emplace_back(v, c);
        G[v].emplace_back(u, c);
    }
    ans[0] = 1;
    dfs(0);
    rep(i, n) {
        cout << ans[i] << endl;
    }
    return 0;
}
#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1;

vector<pair<int, int>> g[N];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < m; ++i) {
        int u, v, c;
        cin >> u >> v >> c;
        g[u].emplace_back(v, c);
        g[v].emplace_back(u, c);
    }
    vector<int> color(n + 1);
    set<int> colors;
    for (int i = 1; i <= n; ++i) {
        colors.insert(i);
    }
    vector<int> used(n + 1);
    queue<int> q;
    used[1] = 2;
    for (auto &[u, c] : g[1]) {
        q.push(u);
        used[u] = 1;
    }
    while (!q.empty()) {
        int v = q.front(); q.pop();
        used[v] = 2;
        for (auto& [u, c] : g[v]) {
            if (used[u] == 2 && color[u] != c) {
                color[v] = c;
            } else if (!used[u]) {
                q.push(u);
                used[u] = 1;
            }
        }
        if (!color[v]) {
            color[v] = *colors.begin();
        }
        colors.erase(color[v]);
    }
    color[1] = *colors.begin();
    for (int i = 1; i <= n; ++i) {
        cout << color[i] << "\n";
    }
}

int main() {
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(20);

    solve();
}
