//
// File: f1.cc
// Author: cteam029
//
// Created on October 19, 2013, 10:26 AM
//
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <climits>
using namespace std;
struct Node{
vector<int> off;
int force;
void Print(map <int, Node*> all){
for(vector<int>::iterator it = off.begin(); it != off.end(); ++it){
cout << *it << endl;
all[*it]->Print(all);
}
}
int Count(map <int, Node*> all){
int res = 0;
for(vector<int>::iterator it = off.begin(); it != off.end(); ++it){
res += all[*it]->Count(all);
}
if(off.empty() || res > force){
return force;
}else{
return res;
}
}
};
//
//
//
int main(int argc, char** argv) {
int count, central, start, end, force;
Node *root;
map <int, Node*> all;
while(cin >> count >> central){
root = new Node();
root->force = INT_MAX;
all.insert(pair<int, Node*>(central, root));
count--;
for(int i = 0; i < count; i++){
cin >> start >> end >> force;
map<int, Node*>::iterator it = all.find(start);
if(it == all.end()){
Node* s = new Node();
s->force = force;
all.insert(pair<int, Node*>(start, s));
all[end]->off.push_back(start);
}
map<int, Node*>::iterator is = all.find(end);
if(is == all.end()){
Node* s= new Node();
s->force = force;
all.insert(pair<int, Node*>(end, s));
all[start]->off.push_back(end);
}
}
int res;
res = root->Count(all);
cout << res << endl;
for(map<int, Node*>::iterator it = all.begin(); it != all.end(); ++it){
delete (*it).second;
}
all.clear();
}
return 0;
}