#include<bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> adj;
vector<int> viz;
vector<pair<int,int>> tata;
int cost;
vector<pair<int,int>> muchiiApm;
void prim(int start){
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
pq.push({0, start});
while(!pq.empty()){
pair<int,int> minWeightNode = pq.top();
pq.pop();
int minWeight = minWeightNode.first;
int minNode = minWeightNode.second;
if (viz[minNode])
continue;
viz[minNode] = 1;
cost += minWeight;
if (tata[minNode].second != 0)
muchiiApm.push_back({tata[minNode].second, minNode});
for(int i = 0; i < adj[minNode].size(); i++){
pair<int,int> nextWeightNode = adj[minNode][i];
int nextWeight = nextWeightNode.first;
int nextNode = nextWeightNode.second;
if (nextWeight < tata[nextNode].first || tata[nextNode].second == 0)
{
tata[nextNode] = {nextWeight, minNode};
}
if (!viz[nextNode]){
pq.push(nextWeightNode);
}
}
}
}
int main(){
ifstream fin("apm.in");
ofstream fout("apm.out");
int n, m;
fin >> n >> m;
viz.resize(n+5);
adj.resize(n+5);
tata.resize(n+5);
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
adj[x].push_back({c, y});
adj[y].push_back({c, x});
}
prim(1);
fout << cost << "\n" << muchiiApm.size() << "\n";
for(int i = 0; i < muchiiApm.size(); i++){
fout << muchiiApm[i].first << " " << muchiiApm[i].second << "\n";
}
return 0;
}