#include <queue>
#include <algorithm>
using namespace std;
#define MAXN 1000000
typedef long long ll;
typedef priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > pq;
struct UF // Union find data structure using path compression
{
	int rep[MAXN];
	UF()
	{
		for (int i = 0; i < MAXN; i++) rep[i] = i;
	}
	int findrep(int a) // Returns the representative in the UF 
	{
		if (rep[a] == a) return a;
		return rep[a] = findrep(rep[a]);
	}
	void merge(int a, int b) // Merges a into b
	{
		rep[findrep(a)] = findrep(b);
	}
	bool connected(int a, int b) // Is a connected to b
	{
		return findrep(a) == findrep(b); 
	}
};
UF stronglyconnected, weaklyconnected;
int v, e; 
pq* adj[MAXN]; // Incoming edges in a priority queue
int incoming[MAXN]; // The incoming edge to this node in the current graph
ll incomingDis[MAXN]; // The weight of the incoming edge to this node
pq* merge(pq* a, pq* b) // Merges a into b
{
	if (a->size() > b->size()) swap(a, b); // Ensure a is smaller than b
	while (!a->empty()) // Push alll elements of a into b
	{
		b->push(a->top());
		a->pop();
	}
	return b;
}
ll ans; // Sum of edge weights
int main()
{
	scanf("%d%d", &v, &e);
	int r;
	scanf("%d", &r); // Root
	for (int i = 0; i < v; i++) // Allocate priority queues
	{
		adj[i] = new pq();
	}
	for (int i = 0; i < e; i++)
	{
		int a, b;
		ll c;
		scanf("%d%d%lld", &a, &b, &c);
		adj[b]->emplace(c, a);
	}
	queue<int> q; // Nodes to be processed
	for (int i = 0; i < v; i++) 
	{
		if (i != r) q.push(i); // Process all nodes except the root
	}
	while (!q.empty())
	{
		int a = q.front();
		q.pop();
		incoming[a] = adj[a]->top().second;
		incomingDis[a] = adj[a]->top().first;
		incoming[a] = stronglyconnected.findrep(incoming[a]);
		adj[a]->pop();
		ans += incomingDis[a]; // Add the distance to the answer
		int b = incoming[a];
		if (weaklyconnected.connected(a, b)) // If connected, makes a cycle
		{
			ll mxincycle = incomingDis[a]; // Maximum edge weight : will not be in the arborescence 
			for (int i = b; i != a; i = stronglyconnected.findrep(incoming[i]))
			{
				mxincycle = max(mxincycle, incomingDis[i]);
				// Merge nodes i and a
				stronglyconnected.merge(i, a);
				adj[a] = merge(adj[a], adj[i]); 
			}
			ans -= mxincycle;
			q.push(a); // Process a again as a supernode
		}
		else
		{
			// Weakly merge them
			weaklyconnected.merge(a, b);
		}
	}
	printf("%lld\n", ans);
}
