// https://www.infoarena.ro/problema/apm
#include <bits/stdc++.h>
using namespace std;
ifstream fin("apm.in");
ofstream fout("apm.out");
const int NMAX=2e5+8;
int n,m;
vector<pair<int, pair<int,int> > > edg;
struct dsu {
vector<int> t;
vector<int> sz;
dsu(int n) : t(n+8, 0), sz(n+8, 0) {
for(int i=1; i<=n; i++) {
t[i]=i;
sz[i]=1;
}
}
int root(int x) {
if(x==t[x]) {
return x;
}
return t[x]=root(t[x]);
}
void unite(int x, int y) {
x=root(x);
y=root(y);
if(x==y) {
return ;
}
if(sz[x]<sz[y]) {
swap(x,y);
}
sz[x]+=sz[y];
t[y]=x;
}
bool are_conn(int x, int y) {
return root(x)==root(y);
}
};
int main() {
ios_base::sync_with_stdio(false);
fin.tie(nullptr);
fout.tie(nullptr);
fin >> n >> m;
for(int i=1; i<=m; i++) {
int x,y,w;
fin >> x >> y >> w;
edg.push_back({w,{x,y}});
}
sort(edg.begin(), edg.end());
int sum=0;
vector<pair<int,int> > ans;
dsu v(n);
for(const auto &it:edg) {
if(!v.are_conn(it.second.first, it.second.second)) {
sum+=it.first;
ans.push_back({it.second.first, it.second.second});
v.unite(it.second.first, it.second.second);
}
}
fout << sum << "\n" << n-1 << "\n";
for(const auto &[x, y]:ans) {
fout << x << " " << y << "\n";
}
return 0;
}