#include <bits/stdc++.h> 

using namespace std; 
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>; 

#define INF 100000000
#define MOD 10007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()

struct Graph{
  int from,to,cost;
  bool operator <(Graph a){
    return (from<a.cost);
  }
};


int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);

  int N,M;
  cin>>N>>M;
  vector<Graph>e(M);
  for(int i=0;i<M;i++){
    int a,b,c;
    cin>>a>>b>>c;
    e[i].from=a;
    e[i].to=b;
    e[i].from=c;
  }
  vector<bool>used(N+1,false);
  sort(rng(e));
  ll ans=M;
  for(auto it:e)cout<<it.from<<endl;
  for(int i=0;i<M;i++){
    int to=e[i].to;
    int from=e[i].from;
    if(used[to]&&used[from])continue;
    else{
      used[to]=true;
      used[from]=true;
      ans--;
    }
  }
  cout<<ans<<endl;
  return 0;
}
