#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 << 31) - 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;
pair<int, PII> E[MMAX];
int root[NMAX];
PII APM[NMAX];
int find(int x) {
if(x != root[x]) root[x] = find(root[x]);
return root[x];
}
void unite(int x, int y) {
root[x] = y;
}
int main() {
int i, j, 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);
E[i] = make_pair(z, make_pair(x, y));
}
sort(E + 1, E + M + 1);
for(i = 1; i <= N; i++)
root[i] = i;
for(i = j = 1; i <= N - 1; j++) {
z = E[j].first;
x = E[j].second.first;
y = E[j].second.second;
if(find(x) == find(y))
continue;
unite(find(x), find(y));
APM[i++] = make_pair(x, y);
sum += z;
}
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;
}