/*
本题要求求出最大树,刚好克鲁斯卡尔算法按照权值从大到小排序就行了
POJ 2377
*/
#include<cstdio>
#include<algorithm>
using namespace std;
int father[1005];
int n,m;
struct Edge{
int s,e;
int cost;
}edge[20005];
int cmp(Edge x,Edge y){
return x.cost>y.cost;
}
void make_set(){
for(int i=1;i<=n;i++)
father[i]=i;
for(int i=1;i<m;i++)
edge[i].cost=0;
}
int find_set(int x){
if(x!=father[x])
father[x]=find_set(father[x]);
return father[x];
}
int union_set(int x,int y){
int fx=find_set(x);
int fy=find_set(y);
if(fx==fy)
return 0;
father[fx]=fy;
return 1;
}
int main(){
while(~scanf("%d%d",&n,&m)){
int sum=0;
make_set();
for(int i=0;i<m;i++){
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].cost);
}
sort(edge,edge+m,cmp);
for(int i=0;i<m;i++){
if(union_set(edge[i].s,edge[i].e)){
sum+=edge[i].cost;
}
}
int a=find_set(1),flag=1;
for(int i=2;i<=n;i++){
if(find_set(i)!=a){
flag=0;
break;
}
}
if(flag)
printf("%d\n",sum);
else
printf("-1\n");
}
}
/*
求最小生成树中的最长边
POJ 2395
*/
#include<cstdio>
#include<algorithm>
using namespace std;
int father[2005];
int n,m;
struct Edge{
int s,e;
int cost;
}edge[10005];
int cmp(Edge x,Edge y){
return x.cost<y.cost;
}
void make_set(){
for(int i=1;i<=n;i++)
father[i]=i;
for(int i=1;i<m;i++)
edge[i].cost=0;
}
int find_set(int x){
if(x!=father[x])
father[x]=find_set(father[x]);
return father[x];
}
int union_set(int x,int y){
int fx=find_set(x);
int fy=find_set(y);
if(fx==fy)
return 0;
father[fx]=fy;
return 1;
}
int main(){
while(~scanf("%d%d",&n,&m)){
make_set();
for(int i=0;i<m;i++){
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].cost);
}
sort(edge,edge+m,cmp);
int max_x=-1;
for(int i=0;i<m;i++){
if(union_set(edge[i].s,edge[i].e)){
if(max_x<edge[i].cost)
max_x=edge[i].cost;
}
}
printf("%d\n",max_x);
}
}