#include <bits/stdc++.h>
using namespace std;
ifstream f("apm.in");
ofstream g("apm.out");
set < pair < int , int > > s;
vector < pair < int , int > > v[200005];
int n,m;
int x,y,c;
int dist[200005];
bool viz[200005];
int t[200005];
int sum=0;
int main()
{
f >> n >> m;
for (int i=1;i<=m;i++) {
f >> x >> y >> c;
v[x].push_back({y, c});
v[y].push_back({x, c});
}
dist[1] = 0; viz[1] = 1;
fill(dist+2, dist+n+1, INT_MAX);
s.insert({0, 1});
while (!s.empty()) {
int nod = s.begin()->second;
int dist_nod = s.begin()->first;
viz[nod] = 1;
sum += dist_nod;
s.erase(s.begin());
for (auto edge:v[nod]) {
if (dist[edge.first] > edge.second && !viz[edge.first]) {
s.erase({dist[edge.first], edge.first});
dist[edge.first] = edge.second;
s.insert({dist[edge.first], edge.first});
t[edge.first] = nod;
}
}
}
g << sum << '\n';
g << n-1 << '\n';
for (int i=1;i<=n;i++) {
if (t[i]!=0) {
g << i << " " << t[i] << '\n';
}
}
return 0;
}