#include <bits/stdc++.h>
using namespace std;
int n;
const int dydis = 1e5 + 100;
vector<pair<int, int> > mas[dydis], gr[dydis];
bool vis[dydis] = {0};
int ans[dydis] = {0};
int neut[dydis] = {0};
void dfs(int v){
    vis[v] = 1;
    for(auto x : mas[v]){
        if(vis[x.first]) continue;
        gr[x.first].push_back({v, x.second});
        gr[v].push_back({x.first, x.second});
        dfs(x.first);
    }
}
void mkN(int v){
    vector<int> st;
    for(auto x : gr[v]){
        st.push_back(x.second);
    }
    sort(st.begin(), st.end());
    int db = 1;
    for(auto x : st){
        if(x > db){
            neut[v] = db;
            return;
        }
        if(x == db) db++;
    }
    neut[v] = db;
}
void fAns(int v, int came){
    if(v == 0){
        ans[v] = neut[v];
    }
    for(auto x : gr[v]){
        if(x.first == came) continue;
        if(ans[v] != x.second) ans[x.first] = x.second;
        else ans[x.first] = neut[x.first];
        fAns(x.first, v);
    }
}
int main(){
    cin.tie(NULL);
    ios_base::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int a, b, c; cin >> a >> b >> c; a--; b--;
        mas[a].push_back({b, c});
        mas[b].push_back({a, c});
    }
    dfs(0);
    for(int i = 0; i < n; i++) mkN(i);
    fAns(0, -1);
    for(int i = 0; i < n; i++){
        cout << ans[i] << "\n";
    }
    return 0;
}

    #include <bits/stdc++.h>
    using namespace std;
    #include <math.h>
    #include <iomanip>
    #include <cstdint>
    #include <string>
    #include <sstream>
    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
    #define rep(i,n) for (int i = 0; i < (n); ++i)
    typedef long long ll;
    using P=pair<ll,ll>;
    const int INF=1001001001;
    const int mod=1e9+7;
    
    int used[20],sz=0,p[20];
    int ng[3][20];
    vector<int>G[20];
    void rec(int n){
      if(used[n]){return;}
      used[n]=1;
      p[sz++]=n;
      for(int u:G[n]){
        rec(u);
      }
    }
    
    ll tot=0;
    void dfs(int depth){
      int now=p[depth];
      if(depth==sz){tot++;return;}
      rep(col,3){
        if(!ng[col][now]){
          for(int u:G[now]){
            ng[col][u]++;
          }
          dfs(depth+1);
          for(int u:G[now]){
            ng[col][u]--;
          }
        }
      }
    }
    void solve(){
      int n,m;
      cin>>n>>m;
      rep(i,m){
        int a,b;
        cin>>a>>b;
        a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
      }
      ll ans=1;
      rep(i,n){
        if(used[i]){continue;}
        sz=0,tot=0;
        rec(i);
        dfs(0);
        ans*=tot;
      }
      cout<<ans<<endl;
    }
    int main(){
      ios_base::sync_with_stdio(false);
      cin.tie(NULL);
      solve();
      return 0;
    }

