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<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<ctime>
using namespace std;

typedef long long ll;

bool debug = false;
const int INF = 1000000000;
const int NUM = 110;
clock_t START, END;

int V, E, r;
int G[NUM][NUM];
bool visited[NUM], flag[NUM];
int pre[NUM];

int mincost(int root)
{
	int sum = 0;
	int i, j, k;
	for (int i = 0; i < V; i++) {
		flag[i] = false;
	}
	pre[root] = root;
	while (true) {
		for (i = 0; i < V; i++) {
			if (flag[i] || i == root)
				continue;
			pre[i] = i;
			for (j = 0; j < V; j++) {
				if (!flag[j] && G[j][i] < G[pre[i]][i])
					pre[i] = j;
			}
			if (pre[i] == i)
				return -1;
		}
		for (i = 0; i < V; i++) {
			if (flag[i] || i == root)
				continue;
			for (j = 0; j < V; j++)
				visited[j] = false;
			visited[root] = true;
			j = i;
			do {
				visited[j] = true;
				j = pre[j];
			} while (!visited[j]);
			if (j == root)
				continue;
			i = j;
			do {
				sum += G[pre[j]][j];
				j = pre[j];
			} while (j != i);
			j = i;
			do {
				for (k = 0; k < V; k++) {
					if (!flag[k] && G[k][j] < INF && k != pre[j])
						G[k][j] -= G[pre[j]][j];
				}
				j = pre[j];
			} while (j != i);
			for (j = 0; j < V; j++) {
				if (j == i)
					continue;
				for (k = pre[i]; k != i; k = pre[k]) {
					if (G[k][j] < G[i][j])
						G[i][j] = G[k][j];
					if (G[j][k] < G[j][i])
						G[j][i] = G[j][k];
				}
			}
			for (j = pre[i]; j != i; j = pre[j])
				flag[j] = true;
			break;
		}
		if (i >= V) {
			for (i = 0; i < V; i++)
				if (!flag[i] && i != root)
					sum += G[pre[i]][i];
			break;
		}
	}
	return sum;
}

int main(void)
{
	if (debug) {
		START = clock();
		freopen("in.txt", "r", stdin);
		freopen("out.txt", "w", stdout);
	}
	int s, t, d, ans;
	cin >> V >> E >> r;
	for (int i = 0; i < V; i++) {
		for (int j = 0; j < V; j++) {
			G[i][j] = INF;
		}
	}
	for (int i = 0; i < E; i++) {
		scanf("%d%d%d", &s, &t, &d);
		G[s][t] = d;
	}
	ans = mincost(r);
	cout << ans << endl;
	if (debug) {
		END = clock();
		double endtime = (double)(END - START) / 1000;
		printf("total time = %lf s", endtime);
	}
	return 0;
}
