#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int nMax = 2e5 + 1;
const int Inf = 1 << 30;
vector<pair<int, int>> adj[nMax];
vector<int> dist(nMax, Inf), parent(nMax);
bitset<nMax> visited;
int n, m, mstCost;
pair<int, int> getMin() {
pair<int, int> Min = {Inf, 0};
for (int i = 1; i <= n; i++) {
if (visited[i] == 0) {
if (Min.first > dist[i]) {
Min = {dist[i], i};
}
}
}
return Min;
}
void prim(int node) {
dist[node] = 0;
for (int i = 1; i <= n; i++) {
auto [cost, node] = getMin();
visited[node] = 1;
mstCost += cost;
for (const auto& [neighbour, ncost] : adj[node]) {
if (visited[neighbour] == 0 && ncost < dist[neighbour]) {
dist[neighbour] = ncost;
parent[neighbour] = node;
}
}
}
}
int main() {
fin >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, z;
fin >> x >> y >> z;
adj[x].push_back({y, z});
adj[y].push_back({x, z});
}
prim(1);
fout << mstCost << "\n" << n - 1 << "\n";
for (int i = 1; i <= n; i++) {
if (parent[i]) {
fout << i << " " << parent[i] << "\n";
}
}
return 0;
}