#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>
#define NMAX 200200
#define MMAX 400200
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int n,m;
pair <int, pair <int, int> >muchii[MMAX];
pair <int, int> NM[NMAX];
int grad[NMAX], tata[NMAX];
int gasesteTata(int nod)
{
if(tata[nod] == nod) return nod;
tata[nod] = gasesteTata(tata[nod]);
return tata[nod];
}
void combina(int x,int y)
{
int tx,ty;
tx = gasesteTata(x);
ty = gasesteTata(y);
if(tx != ty)
{
if(grad[tx] > grad[ty])
{
tata[ty] = tx;
grad[tx] = grad[tx] + grad[ty];
}
else
{
tata[tx] = ty;
grad[ty] = grad[ty] + grad[tx];
}
}
}
int main()
{
int i,x,y,c;
f>>n>>m;
for(i=1; i<=m; i++)
{
f>>x>>y>>c;
muchii[i] = make_pair(c,make_pair(x,y));
}
sort(muchii+1, muchii + m+1);
for(i=1; i<=n; i++)
{
tata[i] = i;
grad[i] = 1;
}
int t=0;
c=0;
for(i=1; i<=m; i++)
{
x = muchii[i].second.first;
y = muchii[i].second.second;
if(gasesteTata(x) != gasesteTata(y))
{
t++;
NM[t] = make_pair(x,y);
combina(x,y);
c = c + muchii[i].first;
}
}
g<<c<<"\n"<<t<<"\n";
for(i=1; i<=t; i++)
g<<NM[i].first<<" "<<NM[i].second<<"\n";
return 0;
}