#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using std::cerr, std::vector;
struct Edge {
int a, b, dist;
};
struct EdgeTo {
int y, dist;
};
struct EdgeComparator {
bool operator()(Edge const a, Edge const b) {
return a.dist > b.dist;
}
};
vector<Edge> get_mst(vector<vector<EdgeTo>> const &adj) {
int const N = adj.size();
vector<char> visited(N);
visited[0] = true;
std::priority_queue<Edge, vector<Edge>, EdgeComparator> pq;
for (auto [next, dist]: adj[0]) {
pq.push({0, next, dist});
}
vector<Edge> selected_edges;
while (!pq.empty()) {
Edge edge = pq.top();
pq.pop();
if (!visited[edge.a] || !visited[edge.b]) {
selected_edges.push_back(edge);
int const new_node = visited[edge.a]? edge.b : edge.a;
visited[new_node] = true;
for (auto [next, dist]: adj[new_node]) {
if (!visited[next]) {
pq.push({new_node, next, dist});
}
}
}
}
return selected_edges;
}
int main() {
std::ifstream fin("apm.in");
std::ofstream fout("apm.out");
int N, M;
fin >> N >> M;
vector<vector<EdgeTo>> adj(N);
for (int i = 0; i < M; ++i) {
int a, b, dist;
fin >> a >> b >> dist;
--a, --b;
adj[a].push_back({b, dist});
adj[b].push_back({a, dist});
}
vector<Edge> mst = get_mst(adj);
int sum = 0;
for (auto [a, b, dist]: mst) {
sum += dist;
}
fout << sum << '\n' << N-1 << '\n';
for (auto [a, b, dist]: mst) {
fout << a+1 << ' ' << b+1 << '\n';
}
return 0;
}