#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)

struct edge{int from,to,cost;};

class UF{
private:
	vector<int> par;
public:
	UF(int __size):par(__size){ rep(i,__size)par[i]=i; }
	
	int find(int x){
		if(par[x]==x) return x;
		else return par[x]=find(par[x]);
	}
	
	void unite(int x,int y){ par[find(y)]=find(x); }
};

int MSA(int r, int v, vector<edge> edges){
	int ret = 0;
	UF uf(v);
	vector<priority_queue<pii,vector<pii>,greater<pii>>> vpq(v);
	vector<int> lazy(v,0), state(v,0);
	
	for(auto e:edges){
		vpq[e.to].push(pii(e.cost,e.from));
	}
	state[r] = 2;
	
	rep(start,v){
		int cur = uf.find(start);
		if(state[cur]!=0)continue;
		
		vector<int> processing, from(v,-1), from_cost(v,0);
		
		while(state[cur]!=2){
			processing.pb(cur);
			state[cur]=1;
			if( vpq[cur].empty() )return INF;
			
			pii e = vpq[cur].top(); vpq[cur].pop();
			from[cur] = e.second;
			from_cost[cur] = e.first+lazy[cur];
			if(cur==from[cur])continue;
			
			ret+=from_cost[cur];
			
			if(state[ from[cur] ]==1){
				int p=cur;
				do{
					lazy[p] -= from_cost[p];
					
					if(p!=cur){
						uf.unite(cur,p);
						while(vpq[p].size()){
							pii t = vpq[p].top();vpq[p].pop();
							t.first+=lazy[p]-lazy[cur];
							t.second = uf.find(t.second);
							vpq[cur].push(t);
						}
					}
					p = uf.find(from[p]);
				}while(p!=cur);
			}else{
				cur = from[cur];
			}
		}
		for(auto elm:processing)state[elm]=2;
	}
	return ret;
}

int main(){
	int e,v;
	int r;
	cin>>v>>e>>r;

	vector<edge> edges;
	rep(i,e){
		int s,t,w;
		cin>>s>>t>>w;
		edges.pb(edge{s,t,w});
	}
	cout<<MSA(r,v,edges)<<endl;
}