#include <fstream>
#include <algorithm>
#define cost first
#define x second.first
#define y second.second
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int n,p,i,op,rx,ry,T[200010],m,sum,k;
pair <int, pair <int,int> > A[400010];
pair<int, int> sol[400010];
int rad(int h){
while(T[h]>0)
h=T[h];
return h;
}
int main(){
fin>>n>>m;
for(i=1;i<=m;i++){
fin>>A[i].x>>A[i].y>>A[i].cost;
T[A[i].x]=-1;
T[A[i].y]=-1;
}
sort(A+1,A+m+1);
for(i=1;i<=m;i++){
rx=rad(A[i].x);
ry=rad(A[i].y);
if(rx!=ry){
sol[++k].cost=A[i].x;
sol[k].second=A[i].y;
if(T[rx]<T[ry]){
T[rx]+=T[ry];
T[ry]=rx;
}else{
T[ry]+=T[rx];
T[rx]=ry;
}
sum+=A[i].cost;
}
}
fout<<sum<<"\n"<<k<<"\n";
for(i=1;i<=k;i++)
fout<<sol[i].cost<<" "<<sol[i].second<<"\n";
return 0;
}