#include <fstream>
#include <vector>
#include <algorithm>
std::ifstream cin("apm.in");
std::ofstream cout("apm.out");
struct muchie {
int sursa, dest, cost;
bool operator < (const muchie & other) const {
return cost < other.cost;
}
};
int n, m;
std::vector<muchie> muchii;
int root[200001];
int find_root(const int node) {
if(node == root[node]) return node;
root[node] = find_root(root[node]);
return root[node];
}
void unite(int x, int y) {
int r_x = find_root(x), r_y = find_root(y);
root[r_x] = r_y;
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; ++i) root[i] = i;
while(m--) {
int x, y, z;
cin >> x >> y >> z;
muchii.push_back({x, y, z});
}
std::sort(muchii.begin(), muchii.end());
std::vector<muchie> ans;
int sum = 0;
for(auto m : muchii) {
int r_x = find_root(m.sursa), r_y = find_root(m.dest);
if(r_x != r_y) {
unite(r_x, r_y);
ans.push_back(m);
sum += m.cost;
}
}
cout << sum << '\n';
cout << ans.size() << '\n';
for(auto m : ans) {
cout << m.sursa << ' ' << m.dest << '\n';
}
}