Find the sum of the weights of edges of the Minimum-Cost Arborescence with the root r for a given weighted directed graph G = (V, E).

Constraints

* 1 ≤ |V| ≤ 100
* 0 ≤ |E| ≤ 1,000
* 0 ≤ wi ≤ 10,000
* G has arborescence(s) with the root r

Input


|V| |E| r
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1


, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the root of the Minimum-Cost Arborescence.

si and ti represent source and target verticess of i-th directed edge. wi represents the weight of the i-th directed edge.

Output

Print the sum of the weights the Minimum-Cost Arborescence.

Examples

Input

4 6 0
0 1 3
0 2 2
2 0 1
2 3 1
3 0 1
3 1 5


Output

6


Input

6 10 0
0 2 7
0 1 1
0 3 5
1 4 9
2 1 6
1 3 2
3 4 3
4 2 2
2 5 8
3 5 3


Output

11
#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;
}