#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
int N, M, sol[200002], cost, K, GR[200002];
struct muchie{ int x, y, c;}m[400002];
struct cmp
{
bool operator()(const muchie &A, const muchie &B)const
{
if (A.c<B.c) return 1;
return 0;
}
};
int grupa(int nod)
{
if (GR[nod]==nod) return nod;
GR[grupa(nod)]=grupa(nod);
return GR[nod];
}
void uneste(int x, int y){ GR[grupa(x)]=grupa(y); }
int main()
{
f>>N>>M;
for (int i=1; i<=M; ++i)
f>>m[i].x>>m[i].y>>m[i].c;
sort(m+1, m+M+1, cmp());
for (int i=1; i<=N; ++i)
GR[i]=i;
for (int i=1; i<=M && K!=N-1; ++i)
if (grupa(m[i].x)!=grupa(m[i].y))
{
cost+=m[i].c; sol[++K]=i;
uneste(m[i].x, m[i].y);
}
g<<cost<<'\n'<<N-1<<'\n';
for (int i=1; i<N; ++i)
g<<m[sol[i]].x<<' '<<m[sol[i]].y<<'\n';
return 0;
}