#include <iostream>
#include <vector>
#include <utility>
#include <cstdlib>
#include <queue>

using namespace std;

#define N 5
#define d_max 10
#define inf 10000000

#define pipii pair<int, pair<int, int> >
#define pii pair<int, int>

struct TNode {
	vector<int> nb;
	vector<int> e;
	
	int intree;
} no[N];

bool connect[N][N];

int dist[N];

int prim(int n) {
	int cost = 0;
	
	dist[n] = 0;
	no[n].intree = 1;
	
	priority_queue<pipii> q;
	q.push(pipii(0, pii(-1, n)));
	
	while (!q.empty()) {
		pipii s = q.top();
		q.pop();
		
		
		if (no[s.second.second].intree == 0) {
			no[s.second.second].intree = 1;
			cost += connect[s.second.second][s.second.first];
		}
		
		if (s.first >= dist[s.second.second])
			continue;
		
		dist[s.second.second] = s.first;
		
		for (int i = 0; i < no[s.second.second].nb.size(); i++) 
			q.push(pipii(dist[s.second.second]+no[s.second.second].e[i], 
					pii(s.second.second, no[s.second.second].nb[i])));
		
	}
	return cost;
}


int main()
{
	srand(100);
	
    for (int i = 0; i < 2*N; i++) {
		int a = rand()%N, b = rand()%N, w = rand()%d_max+1;
		
        if (!connect[a][b]) {
			connect[a][b] = w;
			connect[b][a] = w;
			
			no[a].nb.push_back(b);
			no[a].e.push_back(w);
			
			no[b].nb.push_back(a);
			no[b].e.push_back(w);
			
			cout << a << " " << b << " " << w << endl;
		}
	}
	
	
	for (int i = 0; i < N; i++) {
		dist[i] = inf;
		no[i].intree = 0;
	}
	
	cout << prim(0)<< endl;
	cout << dist[4] << endl;
}