#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr = ds.begin(); itr != ds.end(); ++itr)
#define mp make_pair
#define INF 2000000000
#define INFL 2000000000000000000LL
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef pair<int, pair<int, int> > Edge;

ll Chi_Liu_Edmonds(vector<Edge> edges, int V, int start) {
	vector<P> mins(V, mp(INF, -1));	// ?????????????????\???????°???????(????????????, ?§????)
	rep(i, edges.size()) {
		Edge edge = edges[i];
		mins[edge.second.second] = min(mins[edge.second.second], mp(edge.first, edge.second.first));
	}
	mins[start] = mp(-1, -1);	// ?§??????????????????\?????????

	vector<int> group(V, 0);
	vector<bool> isCycle(V, false);	// ?????????????´???????????????????????????????????????????
	int count = 0;	// ??°?????????????????§?????????????????????????´???????????????°

	vector<bool> used(V, false);
	rep(i, V) {
		if (used[i])continue;
		vector<int> chain;
		int cursor = i;
		while (cursor != -1 && !used[cursor]) {
			// ??????i?????????????????£?????????
			used[cursor] = true;
			chain.push_back(cursor);
			cursor = mins[cursor].second;	// ??????cursor?????\??£???????????????????§????
		}
		if (cursor != -1) {
			// ???????????????????????§??????
			// chain??????cursor??\????????????
			bool inCycle = false;	// ??????????????\???????????????????????¨???????????°
			rep(j, chain.size()) {
				group[chain[j]] = count;
				if (chain[j] == cursor) {
					// ??????????????????
					isCycle[count] = true;
					inCycle = true;
				}
				if (!inCycle)count++;	// ???????????§???????????°??\??°???????????±???
			}
			if (inCycle)count++;
		}
		else {
			// ?????§????????????????????????????????????
			// chain?????????????????????????????¬?????°?????????????????????
			rep(j, chain.size()) {
				group[chain[j]] = count;
				count++;
			}
		}
	}
	// ??????????????§??°????????????????????????????????????
	
	if (count == V) {
		// ????´???§????????°???????????????????????????
		ll ans = 1;	// ???????????????-1??????????¶?????????????1
		rep(i, V) {
			ans += mins[i].first;
		}
		return ans;
	}

	// ????´?????????????????????????????¨?????¨????
	ll res = 0;
	rep(i, V) {
		if (i != start && isCycle[group[i]]) {
			res += mins[i].first;
		}
	}

	/*
	// ??°???????????¨???
	cout << "Group------------" << endl;
	rep(i, V) {
		cout << group[i] << endl;
	}
	cout << "-----------------" << endl;
	*/

	// ??????????????¨???????????§?????°????????°??????????§????
	vector<Edge> newEdges;
	rep(i, edges.size()) {
		Edge edge = edges[i];
		int to = edge.second.second;
		int gfrom = group[edge.second.first];
		int gto = group[edge.second.second];
		if (gfrom == gto) {
			// ?§??????¨?????????????????°????????????????????????????????????????????????
			continue;
		}
		else if(isCycle[gto]) {
			// ?????????????´???????????????????????????????????¨?????????´????????????
			newEdges.push_back(mp(edge.first - mins[to].first, mp(gfrom, gto)));
		}
		else {
			// ????????\?????????????´??????????
			newEdges.push_back(mp(edge.first, mp(gfrom, gto)));
		}
	}

	/*
	// ??°????????°???????????¨???
	cout << "Graph---------------------" << endl;
	rep(i, newEdges.size()) {
		cout << newEdges[i].second.first << ',' << newEdges[i].second.second << '.' << newEdges[i].first << endl;
	}
	cout << "--------------------------" << endl;
	*/

	return res + Chi_Liu_Edmonds(newEdges, count, group[start]);
}

int main() {
	// std::ifstream in("GRL_2_B-in12.txt");	// 93730
	// std::cin.rdbuf(in.rdbuf());
	int V, E, r;
	cin >> V >> E >> r;
	vector<Edge> es;
	rep(i, E) {
		int s, t, w;
		cin >> s >> t >> w;
		es.push_back(mp(w, mp(s, t)));
	}

	cout << Chi_Liu_Edmonds(es, V, r) << endl;

	return 0;
}

Measured memory_bytes: 3596