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