#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
struct Muchie
{
int x, y, c, viz;
bool operator<(const Muchie& e) const
{
return c < e.c;
}
};
int n, m, costTotal;
vector<Muchie> L;
vector<int> T, S;
int Find(int x)
{
int rad, aux;
rad = x;
while(T[rad] != rad)
rad = T[rad];
while(rad != x)
{
aux = T[x];
T[x] = rad;
x = aux;
}
return rad;
}
void Union(int x, int y)
{
if(S[x] < S[y]) swap(x, y);
T[y] = x;
S[x] += S[y];
}
int main()
{
int i;
fin >> n >> m;
T.resize(n + 1);
S.resize(n + 1);
L.resize(m + 1);
for(i = 1; i <= m; i++)
fin >> L[i].x >> L[i].y >> L[i].c;
sort(L.begin() + 1, L.begin() + m + 1);
int nrc = n;
for(i = 1; i <= n; i++)
{
T[i] = i;
S[i] = 0;
}
for(i = 1; nrc > 0 && i <= m; i++)
{
int x = Find(L[i].x);
int y = Find(L[i].y);
if(x != y)
{
Union(x, y);
costTotal += L[i].c;
L[i].viz = 1;
nrc--;
}
}
fout << costTotal << "\n";
fout << n -1 << "\n";
for(i = 1; i <= m; i++)
if(L[i].viz == 1) fout << L[i].x << " " << L[i].y << "\n";
fin.close();
fout.close();
return 0;
}