/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
/**
*
* @author cteam028
*/
public class Main {
/**
* @param args the command line arguments
*/
static Node[] nodea;
while((line = br.readLine()) != null) {
int nodes, root;
root
= Integer.
parseInt(a
[a.
length-1]);
nodea = new Node[nodes+1];
for(int i = 0 ; i <= nodes; i++) {
nodea[i] = new Node();
}
for(int i = 0; i < nodes-1;i++) {
int b[] = new int[3];
parseIntArr(br.readLine().toCharArray(), b);
nodea[b[0]].childs.add(new Edge(b[1], b[2]));
nodea[b[1]].childs.add(new Edge(b[0], b[2]));
}
System.
out.
println(compute
(root
)); }
}
static int compute(int node) {
nodea[node].used = true;
int sum = 0;
boolean found = false;
for(Edge e : nodea[node].childs) {
if(nodea[e.endNode].used) continue;
else {
sum
+= Math.
min(compute
(e.
endNode), e.
weight); found = true;
}
}
if(found) return sum;
}
public static void parseIntArr(char[] arr, int[] target)
{
int i = 0;
int acc = -1;
for(char ch :arr)
{
if(ch == ' ')
{
if(acc!= -1)
{
target[i++] = acc;
acc = -1;
}
}
else if(acc == -1)
acc = ch - '0';
else
acc = acc * 10 + (ch - '0');
}
if(acc != -1)
target[i] = acc;
}
}
class Node {
LinkedList<Edge> childs = new LinkedList<Edge>();
boolean used = false;
}
class Edge {
int endNode, weight;
public Edge(int a, int b) {
endNode = a;
weight = b;
}
}