#include <iostream>
#include <vector>
#include <algorithm> 

using namespace std;

#define N 5
#define d_max 5


int arr[N];

struct TNode {
    int father;
    int val;
} node[N];


struct TEdge {
	int a, b, w;
	int inMST;
};

vector<struct TEdge> e;
bool connect[N][N];

void init() {
    for (int i = 0; i < N; i++) {
        node[i].val = arr[i];
        node[i].father = i;
    }
}


int find(int a) {
    if (node[a].father != a)
        node[a].father = find( node[a].father);
    return node[a].father;
}

void unify(int a, int b) {
    if (find(b) != find(a))
        node[find(a)].father = find(b);
}

bool cmp(TEdge a, TEdge b) {
	return a.w < b.w;
}

int kruskal() {
	sort(e.begin(), e.end(), cmp);
	
	int cost = 0;
	for (int i = 0; i < e.size(); i++) {
		if (find(e[i].a) == find(e[i].b))
			continue;
		unify(e[i].a, e[i].b);
		cost += e[i].w;
	}
	return cost;
}

int main()
{
    for (int i = 0; i < N; i++)
        arr[i] = i;
    init();
	
    srand(100);
	
    for (int i = 0; i < 3*N; i++) {
		int a = rand()%N, b = rand()%N;
		
        if (!connect[a][b]) {
			connect[a][b] = 1;
			connect[b][a] = 1;
			
			struct TEdge x;
			
			x.inMST = 0;
			
			x.a = a;
			x.b = b;
			x.w = rand()%d_max+1;
			e.push_back(x);
			
			x.a = b;
			x.b = a;
			e.push_back(x);
			
			cout << a << " " << b << " " << x.w << endl;
		}
	}
	
	cout << kruskal()<<endl;
    
}