#include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto& x:c){cout<<x<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

class ConnectNodeInfo{
	vector<vector<pair<ll,ll>>> graph;
public:
	ConnectNodeInfo(int node_num){
		graph.resize(node_num);
	}
	void AddNonDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
		graph[v].emplace_back(u,w);
	}
	void AddDirectionalConnection(ll u,ll v,ll w){
		graph[u].emplace_back(v,w);
	}
	vector<pair<ll,ll>>& operator[](ll index){
		return graph[index];
	}
	size_t size(){return graph.size();}
};

ll solve(ConnectNodeInfo& connect,int R){
	int N=connect.size();
	vector<pair<ll,ll>> mins(N,make_pair(1LL<<60,-1));
	REP(i,N){
		for(auto& x:connect[i]){
			mins[x.first] = min(mins[x.first],make_pair(x.second,(ll)i));
		}
	}
	mins[R] = make_pair(-1,-1);

	vector<ll> group(N,0),comp(N,0),used(N,0);
	ll cost = 0;

	REP(i,N){
		if(used[i]) continue;
		vector<int> chain;
		int cur = i;
		while(cur!=-1 && !used[cur]){
			chain.emplace_back(cur);
			used[cur] = 1;
			cur = mins[cur].second;
		}
		if(cur!=-1){
			int cycle = 0;
			for(auto& e:chain){
				group[e] = cost;
				if(e==cur){
					cycle = 1;
					comp[cost] = 1;
				}
				if(!cycle) cost++;
			}
			if(cycle) cost++;
		}
		else{
			for(auto& e:chain){
				group[e] = cost;
				cost++;
			}
		}
	}
	if(cost==N){
		ll sum = 0;
		for(auto& x:mins) sum += x.first;
		return sum+1;
	}

	ll res = 0;
	REP(i,N){
		if(i!=R && comp[group[i]]){
			res += mins[i].first;
		}
	}

	ConnectNodeInfo new_connect(cost);
	REP(i,N){
		for(auto& x:connect[i]){
			ll gs = group[i],gt = group[x.first];
			if(gs==gt) continue;
			if(comp[gt]){
				new_connect.AddDirectionalConnection(gs,gt,x.second-mins[x.first].first);
			}
			else{
				new_connect.AddDirectionalConnection(gs,gt,x.second);
			}
		}
	}
	return res + solve(new_connect,group[R]);
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N=in(),E=in(),R=in();
	ConnectNodeInfo connect(N);
	REP(i,E){
		int u=in(),v=in(),w=in();
		connect.AddDirectionalConnection(u,v,w);
	}

	out(solve(connect,R));
	return 0;
}
