#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <algorithm>
using namespace std;
// complexitate O(m log n)
ifstream fin("apm.in");
ofstream fout("apm.out");
int p[200001], sz[1000001];
int Find(int x) { // O(log n)
while(x != p[x]) {
x = p[x];
}
return x;
}
void Union(int x, int y) { // O(log n)
x = Find(x);
y = Find(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;
};
muchie v[400001], sol[200001];
bool cmp(muchie a, muchie b) {
return a.c < b.c;
}
int main() {
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, cmp); // O(m log m)
int apm = 0, nr = 0;
for(int i = 1; i <= m; i++) { // O(m log n)
if(Find(v[i].x) != Find(v[i].y)) {
apm+= v[i].c;
Union(v[i].x, v[i].y);
sol[++nr] = v[i];
}
}
//afisati apm, nr, elemente din sol
fout << apm << "\n" << nr << "\n";
for(int i = 1; i <= nr; i++) {
fout << sol[i].x << " " << sol[i].y << "\n";
}
fin.close();
fout.close();
return 0;
}