1 #include<iostream>
2 #include<cstdio>
3 using namespace std;
4 #define N 100010
5 #include<algorithm>
6 struct node
7 {
8 int a;
9 int b;
10 int l;
11 }map[N];
12 int n,m;
13 int k;
14 long long sum;
15 int far[N];
16 bool cmp(node a,node b)
17 {
18 return a.l<b.l;
19 }
20 int find(int a)
21 {
22 if(far[a]!=a)far[a]=find(far[a]);
23 return far[a];
24 }
25 int main()
26 {
27 cin>>n>>m;
28 for(int i=1;i<=m;++i)
29 {
30 scanf("%d%d%d",&map[i].a,&map[i].b,&map[i].l);
31 }
32 for(int i=1;i<=n;++i)
33 far[i]=i;
34 sort(map+1,map+m+1,cmp);
35 for(int i=1;i<=m;++i)
36 {
37 int aa=find(map[i].a);
38 int bb=find(map[i].b);
39 if(aa!=bb)
40 {
41 far[aa]=bb;
42 sum+=map[i].l;
43 ++k;
44 }
45 if(k==n-1)
46 break;
47 }
48 cout<<sum;
49 return 0;
50 }