#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 all(a) a.begin(),a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-10)
#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<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef pair<int, pair<int, int> > Edge;
typedef vector<Edge> vE;
typedef priority_queue<Edge, vector<Edge>, greater<Edge> > pqlE;

// 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;
		}
	}

	// ??????????????¨???????????§?????°????????°??????????§????
	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)));
		}
	}

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

int main() {
	int V, E, r;
	cin >> V >> E >> r;
	vector<Edge> G(E);
	rep(i, E) {
		int s, t, w;
		cin >> s >> t >> w;
		G[i] = mp(w, mp(s, t));
	}
	cout << Chi_Liu_Edmonds(G, V, r) << endl;

	return 0;
}