#include<cstdio>
#include<algorithm>
const int nmax=105;
struct muchie
{
int u,v,cost,sel;
}v[nmax];
int t[nmax],h[nmax];
bool cmp(muchie a,muchie b)
{
return a.cost<b.cost;
}
inline int FindSet(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
inline void UnionSet(int x,int y)
{
if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else if(h[x]>h[y])
t[y]=x;
else
t[x]=y;
}
int main()
{
freopen("apm.in","r",stdin);
freopen("apm.out","w",stdout);
int n,i,m,u,f,w,nr=0,tu,tv,tcost=0;
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&f,&w);
v[i].u=u;
v[i].v=f;
v[i].cost=w;
}
std::sort(v+1,v+m+1,cmp);
for(i=1;i<=n;i++)
t[i]=i;
i=1;
while(i<=m&&nr<n-1)
{
tu=FindSet(v[i].u);
tv=FindSet(v[i].v);
if(tu!=tv)
{
UnionSet(tu,tv);
nr++;
tcost=tcost+v[i].cost;
v[i].sel=1;
}
i++;
}
printf("%d\n%d\n",tcost,nr);
for(i=1;i<=m;i++)
{
if(v[i].sel)
printf("%d %d\n",v[i].u,v[i].v);
}
}