#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin ("apm.in");
ofstream cout ("apm.out");
const int asizeof=2e5+5;
bool viz[asizeof];
long long sizeoftree[asizeof];
int dad[asizeof];
int find_dad(int node)
{
if(dad[node]==node)
return node;
return(dad[node]=find_dad(dad[node]));
}
struct Edge
{
Edge(int val,int from,int to)
{
this->val=val;
this->from=from;
this->to=to;
}
long long val;
int from,to;
bool operator < (const Edge& a) const
{
return val > a.val; // pentru min-heap
}
};
void unionfunc(int dad1,int dad2)
{
if(sizeoftree[dad1]>=sizeoftree[dad2])
{
dad[dad2]=dad1;
sizeoftree[dad1]+=sizeoftree[dad2];
}
else
{
dad[dad1]=dad2;
sizeoftree[dad2]+=sizeoftree[dad1];
}
}
vector <pair<int,int>> ans;
int main()
{
int n,m;
cin>>n>>m;
priority_queue<Edge> pq;
for(int i=1; i<=n; i++)
{
dad[i]=i;
sizeoftree[i]=1;
}
for(int i=1; i<=m; i++)
{
int a,b,x;
cin>>a>>b>>x;
pq.push(Edge(x,b,a));
}
long long totalcost=0;
while(ans.size()<n-1)
{
Edge current=pq.top();
pq.pop();
int from=current.from;
int to=current.to;
int val=current.val;
int dad1=find_dad(from),dad2=find_dad(to);
if(dad1==dad2)
continue;
unionfunc(dad1,dad2);
ans.push_back({make_pair(from,to)});
totalcost+=val;
}
cout<<totalcost<<'\n'<<ans.size()<<'\n';
for(int i=0; i<ans.size(); i++)
{
cout<<ans[i].first<<' '<<ans[i].second<<'\n';
}
return 0;
}