#include <iostream>
#include <cstdio>
#include <vector>
#include <limits>
using namespace std;
struct node
{
vector<int> edges;
vector<int> weights;
bool used;
int cost_to_disable;
};
int mainRoot;
int compute(vector<node>& nodes, int root)
{
//cout << root << " " << nodes[root].edges.size() << endl;
if(nodes[root].edges.size() == 1 && root != mainRoot)
return 1001;
int min = 0;
for(int i = 0; i < nodes[root].edges.size(); ++i)
{
if(!nodes[nodes[root].edges[i]].used)
{
int costToDisable;
nodes[nodes[root].edges[i]].used = true;
costToDisable = compute(nodes, nodes[root].edges[i]);
if(costToDisable > nodes[root].weights[i])
costToDisable = nodes[root].weights[i];
min += costToDisable;
}
}
return min;
}
int main()
{
int nodeCount;
while(scanf("%d %d", &nodeCount, &mainRoot) > 0)
{
vector<node> nodes(nodeCount + 1);
for(int i = 0; i < nodeCount - 1; ++i)
{
int from, to, weight;
scanf("%d %d %d", &from, &to, &weight);
nodes[from].edges.push_back(to);
nodes[from].weights.push_back(weight);
nodes[to].edges.push_back(from);
nodes[to].weights.push_back(weight);
nodes[from].used = false;
nodes[to].used = false;
}
cout << compute(nodes, mainRoot) << endl;
}
}