#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "apm";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif // HOME
typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;
const int INF = (1LL << 30) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = (int)(1e9) + 7;
const int NMAX = 200000 + 5;
const int MMAX = 400000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;
int N, M, sum;
vector<PII> V[NMAX];
int dist[NMAX];
PII dad[NMAX];
PII APM[NMAX];
priority_queue<PII, vector<PII>, greater<PII> > PQ;
int main() {
int i, j = 0, x, y, z;
#ifndef ONLINE_JUDGE
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
#endif
scanf("%d%d", &N, &M);
for(i = 1; i <= M; i++) {
scanf("%d%d%d", &x, &y, &z);
V[x].push_back(make_pair(y, z));
V[y].push_back(make_pair(x, z));
}
for(i = 1; i <= N; i++)
dist[i] = INF;
dist[1] = INF;
PQ.push(make_pair(INF, 1));
while(!PQ.empty()) {
x = PQ.top().second;
PQ.pop();
if(!dist[x])
continue;
APM[j++] = make_pair(x, dad[x].first);
sum += dad[x].second;
dist[x] = 0;
for(auto y : V[x])
if(dist[y.first] && y.second < dist[y.first]) {
dist[y.first] = y.second;
dad[y.first] = make_pair(x, y.second);
PQ.push(make_pair(dist[y.first], y.first));
}
}
printf("%d\n%d\n", sum, N - 1);
for(i = 1; i <= N - 1; i++)
printf("%d %d\n", APM[i].first, APM[i].second);
return 0;
}