#include <fstream>
#include <queue>
class comp {
public:
bool operator() (std::pair<int,int> a, std::pair<int,int> b){
return a.second > b.second;
}
};
std::vector<std::vector<std::pair<int,int>>> adj; // adj[nod1] = <nod2,cost>
std::vector<bool> viz; // daca e vizitat sau nu
std::vector<int> d; // distanta minima pana la nod
std::vector<int> tata; // pentru reconstruirea drumului
std::priority_queue<std::pair<int,int>, std::vector<std::pair<int,int>>, comp> pq; // min heap
long long cost_total;
const int INF = 1e9; // pt vectorul d
std::ifstream f("apm.in");
std::ofstream g("apm.out");
void prim(int i) {
pq.emplace(i,0);
d[i] = 0;
while (!pq.empty()){
int nod_curent = pq.top().first;
int cost_curent = pq.top().second;
pq.pop(); // am extras varful cozii
if (viz[nod_curent])
continue; // cazul in care am adaugat acelasi nod de 2+ ori in coada
viz[nod_curent] = true;
cost_total += cost_curent; // odata ce nodul e vizitat,nu il mai reaccesam,deci cost_curent e final
for (auto vec : adj[nod_curent]) {
int nod_vecin = vec.first;
int cost_vecin = vec.second;
if (!viz[nod_vecin] && d[nod_vecin] > cost_vecin) {
d[nod_vecin] = cost_vecin; // actualizam muchia minima
tata[nod_vecin] = nod_curent; // actualizam noul tata
pq.emplace(nod_vecin,cost_vecin); // actualizam pq ul
}
}
}
}
int main() {
int n,m;
f >> n >> m;
viz.resize(n + 1);
adj.resize(n + 1);
tata.resize(n + 1);
d.resize(n + 1, INF);
for (int i = 0; i < m; i++) {
int x,y,c;
f >> x >> y >> c;
adj[x].emplace_back(y,c);
adj[y].emplace_back(x,c);
}
prim(1);
g << cost_total << '\n' << n - 1 << '\n'; // cost total,nr muchii
for (int i = 2; i <= n; i++) {
g << i << ' ' << tata[i] << '\n';
}
return 0;
}