#include <bits/stdc++.h>
using namespace std;

struct edge
{
	int u, v, w;
	
	edge(void) {};
	edge(int a, int b, int c) :
		u(a), v(b), w(c) {};
		
	friend bool operator < (const edge &a, const edge &b)
	{
		return a.w < b.w;
	}
	
	friend bool operator > (const edge &a, const edge &b)
	{
		return a.w > b.w;
	}
};

class MCA
{
	private:
		
		const static int inf = 2e9;
		const static int maxn = 505;
		const static int maxm = 50005;
		
		edge inEdge[maxn];
		
		int vis[maxn], id[maxn];
	
	public:
		
		int MinCostArb(int n, int r, vector<edge> E)
		{
			int minCost = 0;
			
			while (true)
			{
				for (int i = 0; i < n; ++i)
					inEdge[i].w = inf;
					
				for (auto e : E)
					if (e.u != e.v
					&&  inEdge[e.v] > e)
						inEdge[e.v] = e;
						
				inEdge[r].w = 0;
				
				for (int i = 0; i < n; ++i)
					if (inEdge[i].w == inf)
						return inf;
						
				int cnt = 0;
				
				memset(id, -1, sizeof(id));
				memset(vis, -1, sizeof(vis));
				
				for (int i = 0; i < n; ++i)
				{
					minCost += inEdge[i].w;
					
					register int v = i;
					
					while (vis[v] != i && id[v] == -1 && v != r)
						vis[v] = i, v = inEdge[v].u;
						
					if (v != r && id[v] == -1)
					{
						register int u = inEdge[v].u;
						
						while (u != v)
							id[u] = cnt, u = inEdge[u].u;
							
						id[v] = cnt++;
					}
				}
				
				if (cnt == 0)break;
				
				for (int i = 0; i < n; ++i)
					if (id[i] == -1)id[i] = cnt++;
					
				for (auto &e : E)if (e.u != e.v)
				{
					e.w -= inEdge[e.v].w;
					e.u = id[e.u];
					e.v = id[e.v];
				}
				
				n = cnt, r = id[r];
			}
			
			return minCost;
		}
}mca;

signed main(void)
{
	int n, m, r;
		cin >> n >> m >> r;
		
	vector<edge> e;
		for (int i = 0; i < m; ++i)
		{
			int u, v, w;
				cin >> u >> v >> w;
			if (u != v)
				e.push_back(edge(u, v, w));
		}
		
	cout << mca.MinCostArb(n, r, e) << endl;
}