#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)

int e,v;

struct edge{int from,to,cost;};
vector<edge> edges;

class UF{
private:
	vector<int> par;
public:
	int groups;
	
	UF(int __size):par(__size){
		for(int i=0;i<__size;i++)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){
		x=find(x);
		y=find(y);
		par[y] = x;
	}
};

int MSA(int r){
	int ret = 0;
	UF uf(v);
	vector<priority_queue<pii,vector<pii>,greater<pii>>> vpq(v);
	vector<int> lazy(v,0);
	vector<int> 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;
		vector<int> from(v,-1);
		vector<int> 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 r;
	cin>>v>>e>>r;
	rep(i,e){
		int s,t,w;
		cin>>s>>t>>w;
		edges.pb(edge{s,t,w});
	}
	cout<<MSA(r)<<endl;
}