#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
int p[200001];
int sz[200001];
int n, m;
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 (sz[x] > sz[y]) {
p[y] = x;
sz[x] += sz[y];
} else {
p[x] = y;
sz[y] += sz[x];
}
}
struct muchie {
int x, y, cost;
};
muchie v[400001];
muchie sol[200001];
bool cmp(muchie a, muchie b) {
return a.cost < b.cost;
}
int main() {
fin >> n >> m;
for (int i = 1; i <= m; i++) {
fin >> v[i].x >> v[i].y >> v[i].cost;
}
for (int i = 1; i <= n; i++) {
p[i] = i;
sz[i] = 1;
}
sort(v + 1, v + m + 1, cmp);
int cost_total = 0;
int nr_muchii = 0;
for (int i = 1; i <= m; i++) {
if (Find(v[i].x) != Find(v[i].y)) {
Union(v[i].x, v[i].y);
cost_total += v[i].cost;
nr_muchii++;
sol[nr_muchii] = v[i];
if (nr_muchii == n - 1)
break;
}
}
fout << cost_total << "\n";
fout << nr_muchii << "\n";
for (int i = 1; i <= nr_muchii; i++) {
fout << sol[i].x << " " << sol[i].y << "\n";
}
return 0;
}