//Bismillahir Rahmanir Rahim
//Allahumma Rabbi Jhidni Elma
/*--------Please carefully check--------
    1.Overflow and underflow
    2.Corner case
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long  ull;
typedef long double ld;
const ld PI = 2*acosl(0.0);
const int inf=1e5+7;
const int mxN=3000000;
#define speed ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define pb push_back
#define reset(a) memset(a,0,sizeof a)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (a/gcd(a,b)*b)
#define abs(a) (a<0?-(a):a)
#define debug1(x)  cout << #x << "=" << x << endl
#define debug2(x, y)  cout << #x << "=" << x << "," << #y << "=" << y << endl
#define digit2(x) floor((log2(x)))
#define digit2(x) floor((log2(x)))
#define sc(a) scanf("%d",&a)
#define pf(a) printf("%d\n",a)
#define  PII  pair<int,int>
#define mk  make_pair

int mark[inf];
struct node
{
    int a,b;
};
vector<PII>tree[inf];
bool cmp(node x,node y)
{
    return x.b<y.b;
}
int prims(int src)
{
    priority_queue<PII ,vector<PII>,greater<PII>>pq;
    pq.push(make_pair(0,src));
    PII x;
    while(!pq.empty())
    {
        x=pq.top();
       int sc=x.second;
       // cout<<x.first<<"  "<<x.second<<endl;
        pq.pop();
        if(mark[sc])
        {
            continue;
        }
         mark[sc]=1;
        // cout<<x.second<<" "<<x.first<<endl;
       for(int i=0;i<tree[sc].size();i++)
       {
           int value=tree[sc][i].second;
          //
           if(mark[value]==0)
           {
              // mark[value]=1;
              // cout<<sc<<" "<<value<<" "<<tree[sc][i].first<<endl;
                pq.push(tree[sc][i]);
           }
       }
    }
    return 0;
}
int write[inf],par[inf],n;
void dfs(int src,int level)
{
   // cout<<level<<" ";
   // cout<<par[src]<<' '<<src<<" "<<write[par[src]]<<" "<<level<<endl;
    if(write[par[src]]==level)
    {
        write[src]=(level%n)+1;
    }
    else
    {
        write[src]=level;
    }
    //ar[src]=level;
    mark[src]=1;

    for(auto x:tree[src])
    {
        //cout<<src<<"-->"<<x.second<<"---"<<x.first<<endl;
        if(mark[x.second]==0)
        {
            par[x.second]=src;
            dfs(x.second,x.first);
        }
    }
   // cout<<src<<" ";
}
int main()
{
    int m,u,v,c,i,temp,j;
    sc(n),sc(m);
    node nd;
    for(i=0;i<m;i++)
    {
        sc(u),sc(v),sc(c);
        //nd.a=v;
       // nd.b=c;
        tree[u].pb(mk(c,v));
        //nd.a=u;
        tree[v].pb(mk(c,u));
    }/*
    for(i=1;i<=n;i++)
    {
        for(j=0;j<tree[i].size();j++)
        {
            cout<<i<<"-->"<<tree[i][j].a<<"-->"<<tree[i][j].b<<endl;
        }
        cout<<endl;
    }*/
    dfs(1,1);
    for(i=1;i<=n;i++)
    {
        cout<<write[i]<<endl;
    }
    cout<<endl;
    //sort(tree.begin(),tree.end(),cmp());
    //prims(1);

}
/*
6 7
1 2 6
2 5 3
5 6 4
5 3 5
3 4 1
1 3 2
1 5 2
*/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define INF 0x3f3f3f3f
#define mem(a, b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)

// const ll mod = 998244353;
// const ll mod = 1e9 + 7;
// const double eps = 1e-6;
// const double PI = acos(-1);
// const double R = 0.57721566490153286060651209;

#define MAXN 205000//点数
#define MAXM 404000//道路数

#define search(i,y) for(int i=head[y];i;i=edge[i].next) //遍历一个点的出边
#define add(u,v,c)  {edge[++cnt]={u,v,c,head[u]};head[u]=cnt;}//加入一条边
int head[MAXN],cnt=1;//0说明没有下一个值
struct EDGE{
    int u,v,c;
    int next;//下一个值的位置;
}edge[MAXM];
int inf = MAXN +1;
int color[MAXN];
bool used[MAXN];
void dfs(int x){
    if(used[x])
        return;
    used[x]=true;
    search(i,x){
        int y=edge[i].v;
        if(used[y])continue;
        if(color[x]==edge[i].c){
            color[y]=edge[i].c-1;
            if(color[y]==0)
                color[y]=edge[i].c+1;
            dfs(y);
        }else{
            color[y]=edge[i].c;
            dfs(y);
        }
    }
    return ;
}

void solve() {
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        add(a,b,c);
        add(b,a,c);
    }
    color[1]=1;
    dfs(1);
    for(int i=1;i<=n;i++){
        printf("%d\n",color[i]);
    }


}

signed main() {
    ios_base::sync_with_stdio(false);
    //cin.tie(nullptr);
    //cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

