#include<bits/stdc++.h>
using namespace std;
#define il inline
#define rg register
il int read()
{
	int re=0,k=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')k=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){re=re*10+ch-48;ch=getchar();}
	return re*k;
}
il void write(int x)
{
	if(x<0) return putchar('-'),write(-x),void();
	if(x<10)return putchar(x+48),void();
	return write(x/10),putchar(x%10+48),void();
}
int col[200005],l,n,m;
int fa[200005],head[200005],tot;
int find(int x)
{
	if(x==fa[x])return x;
	return fa[x]=find(fa[x]);
}
struct ss{
	int node,nxt,w;
}e[400005];
void add(int u,int v,int w)
{
	e[++tot].nxt=head[u];
	head[u]=tot;
	e[tot].node=v;
	e[tot].w=w;
}

void dfs(int x,int fa,int c)
{
//	cerr<<x
	col[x]=c;
	for(rg int i=head[x];i;i=e[i].nxt)
	{
		int y=e[i].node;
		if(y==fa)continue;
		if(c==e[i].w)
		{
			dfs(y,x,c%n+1);
		}
		else
		{
			dfs(y,x,e[i].w);
		}
	}
}
signed main()
{
	n=read();m=read();
	for(rg int i=1;i<=n;i++)
		fa[i]=i;
	for(rg int i=1,a,b,c;i<=m;i++)
	{
		a=read();b=read();c=read();
		int x=find(a);
		int y=find(b);
		if(x==y)continue;
		fa[x]=y;
		add(a,b,c);
		add(b,a,c);
	}
	
	dfs(1,0,1);
	for(rg int i=1;i<=n;i++)
		write(col[i]),puts("");
}
#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;

#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
  return S.find(key) != S.end();
}

struct BIT {
    // definition
    using size_type = long long; // must be signed
    using value_type = int;
    static value_type plus(value_type a, value_type b) {
        return a + b;
    }
    const value_type identity = 0;
    
    BIT(size_type N) : tree(N+1, identity) {        
    }
    
    void add(size_type i, value_type x) {
        ++i; // convert to 1-based
        while(i < tree.size()) {
            tree[i] += x;
            i += i & (-i);
        }
    }
    
    value_type sum0(size_type i) {
        // open boundary in 0-based index = close boundary in 1-based index
        value_type ans = identity;
        while(i > 0) {
            ans += tree[i];
            i -= i & (-i);
        }
        return ans;
    }
    
    value_type sum(size_type l, size_type r) {
        return sum0(r) - sum0(l);
    }
    
    vector<value_type> tree;
};

int main(int argc, char *argv[]) {
  i64 i, j, k;  
  i64 N; RI(N);
  vi a(N); RVI(a);

  BIT tree(N);
  i64 T = 0;
  REP(0, N, i) {
    T += tree.sum(a[i], N);
    tree.add(a[i], 1);
  }

  i64 T0 = T;
  REP(0, N, i) {
    cout << T << endl;
    T -= a[i];
    T += (N - 1 - a[i]);
  }
  assert(T == T0);

  return 0;
}

