#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
int n, m, root;
struct edge {
  int u, v, w;
};
vector<edge> E;
int dmst(int N, vector<edge> &E, int root) {
  vector<int> cost(N + 1), back(N + 1), label(N + 1), bio(N + 1);
  int ret = 0;
  for (;;) {
    for (int i = 1; i <= N; i++) cost[i] = 1000000000;
    for (edge e : E) {
      if (e.u == e.v) continue;
      if (e.w < cost[e.v]) cost[e.v] = e.w, back[e.v] = e.u;
    }
    cost[root] = 0;
    for (int i = 1; i <= N; i++)
      if (cost[i] == 1000000000) return -1;
    for (int i = 1; i <= N; i++) ret += cost[i];
    int K = 0;
    for (int i = 1; i <= N; i++) label[i] = 1;
    for (int i = 1; i <= N; i++) bio[i] = -1;
    for (int i = 1; i <= N; i++) {
      int x = i;
      for (; x != root && bio[x] == -1; x = back[x]) bio[x] = i;
      if (x != root && bio[x] == i) {
        for (; label[x] == -1; x = back[x]) label[x] = K;
        ++K;
      }
    }
    if (K == 0) break;
    for (int i = 1; i <= N; i++)
      if (label[i] == -1) label[i] = K++;
    for (edge &e : E) {
      int uu = label[e.u];
      int vv = label[e.v];
      if (uu != vv) e.w -= cost[e.v];
      e.u = uu;
      e.v = vv;
    }
    root = label[root];
    N = K;
  }
  return ret;
}
int main() {
  scanf("%d%d%d", &n, &m, &root);
  root++;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    u++;
    v++;
    E.push_back((edge){u, v, w});
  }
  printf("%d\n", dmst(n, E, root));
  return 0;
}
