#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define cnts(x,c) count(all(x),c)
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) {a = b; return 1;} return 0;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) {a = b; return 1;} return 0;}
template<class T> void print(const T &t) {cout<<t<<"\n";}
template<class T> void PRINT(const T &t) {rep(i,sz(t)) cout<<t[i]<<" \n"[i==sz(t)-1];}
const ll INF = 1LL << 62;
const int iINF = 1 << 30;

using Graph = vector<vector<int>>;
int n,m;

int BFS(const Graph &G, int s) {
    int n = sz(G);
    vector<int> dist(n,-1);
    queue<int> que;

    dist[s]=0;
    que.push(s);

    while(!que.empty()) {
        int v=que.front();
        que.pop();
        fore(nv,G[v]) {
            if (dist[nv]!=-1) continue;
            dist[nv]=dist[v]+1;
            que.push(nv);
        }
    }
    int cnt=0;
    fore(x,dist) if(x!=-1) cnt++;
    return cnt;
}

int main() {
    fio(); cin>>n>>m;
    Graph G(n);
    rep(i,m) {
        int a,b; cin>>a>>b;a--;b--;
        G[a].push_back(b);
    }
    int ans=0;
    rep(i,n) ans+=BFS(G,i);
    print(ans);
}

#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define fi first
#define se second
const int maxn=100000;
typedef pair<int,int> P;
vector<P>g[maxn+5];
int vis[maxn+5];
int pr[maxn+5];
int col[maxn+5];
void dfs(int x,int pre,int c)
{
    vis[x]=1;
    pr[c]=1;
    if(col[pre]!=c)
        col[x]=c;
    for(int i=0; i<g[x].size(); i++)
    {
        P tmp=g[x][i];
        if(vis[tmp.fi])
            continue;
        dfs(tmp.fi,x,tmp.se);
    }
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<m; i++)
    {
        int u,v,c;
        scanf("%d%d%d",&u,&v,&c);
        g[u].push_back(mp(v,c));
        g[v].push_back(mp(u,c));
    }
    dfs(1,0,0);
    int tmp;
    for(int i=1;i<=n;i++)
    {
        if(!pr[i])
        {
            tmp=i;
            break;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!col[i])
            printf("%d\n",tmp);
        else
            printf("%d\n",col[i]);
    }
}
