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 <iostream>
#include <iomanip>
#include <climits>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <unordered_map>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <random>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e15
#define _INFLL (long long)-1e15
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<int, int> P;

/*******************************************************/

struct graph_t {
	int n;           // |V|, index begins with 0
	int m;           // |E|
	vector<P> edges; // E
	vector<ll> cost; // cost or distance
	vector<ll> cap;  // capacity
};

// solve MCA
class Chuliu_Edmonds {
private:
	struct fromedge {
		int eid;
		int id;
		ll cost;
		stack<int> included_stk;
		bool operator<(const fromedge & another) const {
			return !(cost != another.cost ? cost < another.cost : eid < another.eid);
		}
	};
	struct node {
		int overnode; bool done; bool fin; priority_queue<fromedge> from_edges; fromedge from;
	};
	vector<node> nodes;
	int n, root;
	bool no_mca;
	int topnode(int a) {
		while (nodes[a].overnode != -1) a = nodes[a].overnode;
		return a;
	}
	void contract(int s) {
		int a = s;
		priority_queue<fromedge> new_from_edges;
		do {
			a = topnode(a);
			priority_queue<fromedge> from_ebuf = nodes[a].from_edges;
			while (from_ebuf.size()) {
				fromedge from_e = from_ebuf.top();
				from_ebuf.pop();
				if (from_e.id == nodes[a].from.id) continue;
				from_e.cost -= nodes[a].from.cost;
				from_e.included_stk.push(a);
				new_from_edges.push(from_e);
			}
			nodes[a].overnode = nodes.size();
			a = nodes[a].from.id;
		} while (a != s);
		nodes.push_back({ -1, false, false, new_from_edges,{} });
	}
	void unfold(stack<int> *stk) {
		while (stk->size()) {
			int a = stk->top(); stk->pop();
			if (a >= n) {
				int b = nodes[a].from.included_stk.top();
				ll d = nodes[b].from.cost;
				nodes[b].from = nodes[a].from;
				nodes[b].from.cost += d;
				nodes[b].from.included_stk.pop();
			}
			else nodes[a].fin = true;
		}
	}
public:
	Chuliu_Edmonds(graph_t G, int start) {
		n = G.n;
		nodes.resize(n);
		Loop(i, n) nodes[i] = { -1, false, false, priority_queue<fromedge>(),{} };
		Loop(i, G.edges.size()) {
			nodes[G.edges[i].second].from_edges.push({ i, G.edges[i].first, G.cost[i], stack<int>() });
		}
		root = start;
		no_mca = false;
		nodes[root].fin = nodes[root].done = true;
		Loop(i, n) {
			if (!nodes[i].fin) {
				int a = i;
				stack<int> stk;
				nodes[a].done = true;
				stk.push(a);
				do {
					int b;
					do {
						if (nodes[a].from_edges.empty()) { no_mca = true; return; }
						nodes[a].from = nodes[a].from_edges.top();
						b = nodes[a].from.id;
					} while (a == b);
					if (nodes[b].fin) unfold(&stk);
					else if (nodes[b].done) {
						contract(b);
						stk.push(nodes.size() - 1);
						a = nodes.size() - 1;
					}
					else {
						nodes[b].done = true;
						stk.push(b);
						a = b;
					}
				} while (stk.size());
			}
		}
	}
	vector<P> get_tree_idpair() {
		if (no_mca) return{};
		vector<P> ret;
		Loop(i, n) {
			if (i != root) ret.push_back({ nodes[i].from.id, i });
		}
		return ret;
	}
	vi get_tree_eid() {
		if (no_mca) return{};
		vi ret;
		Loop(i, n) {
			if (i != root) ret.push_back(nodes[i].from.eid);
		}
		return ret;
	}
	ll get_weight() {
		if (no_mca) return -1;
		ll ret = 0;
		Loop(i, n) {
			if (i != root) ret += nodes[i].from.cost;
		}
		return ret;
	}
};

// Chuliu_Edmonds sample
int main() {
	graph_t G;
	cin >> G.n >> G.m;
	int r; cin >> r;
	Loop(i, G.m) {
		int s, t, c; cin >> s >> t >> c;
		G.edges.push_back({ s, t });
		G.cost.push_back(c);
	}
	Chuliu_Edmonds mca(G, r);
	cout << mca.get_weight() << endl;
	return 0;
}