#include <bits/stdc++.h>
using namespace std;
const int NMAX = 400000;
int p[NMAX+1];
int sz[NMAX+1];
int Find(int x){
while(x != p[x])
x = p[x];
return x;
}
void Union (int x, int y){
x= Find(x);
y= Find(y);
if(x != y)
{
if(sz[x] < sz[y])
{
p[x] = y;
sz[y] += sz[x];
}
else{
p[y] = x;
sz[x] += sz[y];
}
}
}
struct muchie {
int x, y, c;
int ales;
};
muchie v[NMAX+1];
int main()
{
ifstream fin("apm.in");
ofstream fout("apm.out");
int n, m;
fin >> n >> m;
for(int i=1;i<= n; i++){
p[i]=i;
sz[i]=1;
}
for(int i=1;i<=m;i++)
{
fin>>v[i].x>>v[i].y>>v[i].c;
}
sort(v+1, v+m+1, [](muchie a, muchie b){ return a.c < b.c; });
int apm = 0;
int nr=0;
for(int i=1;i<=m;i++)
{
if(Find(v[i].x) != Find(v[i].y))
{
Union(v[i].x, v[i].y);
apm += v[i].c;
nr++;
v[i].ales = 1;
}
}
fout<<apm<<"\n"<<nr<<"\n";
for(int i=1;i<=m;i++)
if(v[i].ales)
fout<<v[i].x<<" "<<v[i].y<<"\n";
return 0;
}