import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Fr {
public static void main
(String[] args
) { Scanner sc
= new Scanner
(System.
in);
int n;
int main;
ArrayList<Node> nodes;
int a;
int b;
int v;
Node tempNode;
Node father;
while(sc.hasNextInt()){
a = 0;
b = 0;
n = sc.nextInt();
nodes = new ArrayList<Node>(n+1);
for (int i = 0; i < n+1; i++) {
nodes.add(null);
}
// System.out.println(nodes.size());
main = sc.nextInt();
nodes.
add(main,
new Node
(main,
Integer.
MAX_VALUE));
for (int i = 0; i < n-1; i++) {
a = sc.nextInt();
b = sc.nextInt();
v = sc.nextInt();
father = nodes.get(a);
if (father == null){
tempNode = new Node(a, v);
nodes.set(a, tempNode);
father = nodes.get(b);
father.addChild(tempNode);
}else{
tempNode = new Node(b, v);
nodes.set(b, tempNode);
father.addChild(tempNode);
}
}
System.
out.
println(getNodeValue
(nodes.
get(main
))); }
sc.close();
}
static int getNodeValue(Node node){
if (node.child.size() == 0){
return node.value;
}
int sumV = 0;
for (Node n : node.child) {
sumV += getNodeValue(n);
}
if (node.value > sumV){
node.value = sumV;
return node.value;
}
return node.value;
}
}
class Node{
int id;
int value;
LinkedList<Node> child;
Node ancestor;
public Node(int id, int value) {
super();
this.id = id;
this.value = value;
child = new LinkedList<Node>();
// this.ancestor = ancestor;
}
void addChild(Node node){
this.child.add(node);
}
}