#include <stdio.h>
#include <ctype.h>
#define MAXN 200000
#define MAXM 800000
int dsu[MAXN];
int ea[MAXM];
int eb[MAXM];
int ec[MAXM];
int reza[MAXM];
int rezb[MAXM];
int find( int i ){
if( dsu[i] == i )
return i;
return dsu[i] = find(dsu[i]);
}
void myqsort( int begin, int end ){
int b = begin - 1, e = end + 1, p = ec[(begin + end) / 2], aux;
while( ec[++b] < p );
while( ec[--e] > p );
while( b < e ){
aux = ec[b];
ec[b] = ec[e];
ec[e] = aux;
aux = eb[b];
eb[b] = eb[e];
eb[e] = aux;
aux = ea[b];
ea[b] = ea[e];
ea[e] = aux;
while( ec[++b] < p );
while( ec[--e] > p );
}
if( begin < e )
myqsort(begin, e);
if( e + 1 < end )
myqsort(e + 1, end);
}
FILE *fin, *fout;
int main(){
fin = fopen("apm.in", "r");
fout = fopen("apm.out", "w");
int n, m, i, u, v, rez, cost;
for( i = 0 ; i < n ; i++ )
dsu[i] = i;
for( i = 0 ; i < n - 1 ; i++ )
fscanf(fin, "%d%d%d", ea + i, eb + i, ec + i);
myqsort(0, n - 1);
rez = 0;
cost = 0;
for( i = 0 ; i < n - 1 ; i++ ){
if( (u = find(ea[i])) != (v = find(eb[i])) ){
reza[rez = ea[i];
rezb[rez++] = eb[i];
dsu[u] = v;
cost += ec[i];
}
}
fprintf(fout, "%d\n%d\n", cost, rez);
for( i = 0 ; i < rez ; i++ )
fprintf(fout, "%d %d\n", reza[i], rezb[i]);
fclose(fin);
fclose(fout);
return 0;
}