#include <algorithm>
#include <fstream>
#define NMAX 200020
#define MMAX 400020
int TT[NMAX], RG[NMAX], SOL[NMAX];
int N, M;
struct muchie {
int x, y, c;
} E[MMAX];
bool cmp(const muchie &a, const muchie &b) {
return a.c < b.c;
}
int find(int x) {
int R, y;
R = x;
while (TT[R] != R)
R = TT[R];
while (TT[x] != x) {
y = TT[x];
TT[x] = R;
x = y;
}
return R;
}
void unite(int x, int y) {
if (RG[x] > RG[y])
TT[y] = x;
else
TT[x] = y;
if (RG[x] == RG[y])
RG[y]++;
}
using namespace std;
int main() {
ifstream fin("apm.in");
ofstream fout("apm.out");
fin >> N >> M;
int i, x, y, nr = 0, cost = 0;
for (i = 1; i <= N; i++) {
TT[i] = i;
RG[i] = 1;
}
for (i = 1; i <= M; i++)
fin >> E[i].x >> E[i].y >> E[i].c;
sort(E + 1, E + M + 1, cmp);
for (i = 1; i <= M && nr < N - 1; i++) {
x = find(E[i].x);
y = find(E[i].y);
if (x != y) {
unite(x, y);
cost += E[i].c;
SOL[++nr] = i;
}
}
fout << cost << '\n' << nr << '\n';
for (i = 1; i <= nr; i++)
fout << E[SOL[i]].x << ' ' << E[SOL[i]].y << '\n';
return 0;
}