#include <bits/stdc++.h>
struct side {
  int from;
  int to;
  int weight;
};
side sides[1000];
void mergesort(int l, int r) {
  if (l == r) return;
  side temp;
  if (r - 1 == l) {
    if (sides[l].weight > sides[r].weight) {
      temp = sides[l];
      sides[l] = sides[r];
      sides[r] = temp;
    }
    return;
  }
  int mid = (l + r) / 2, now = 0;
  side b[r - l + 1];
  mergesort(l, mid - 1);
  mergesort(mid, r);
  for (int i = 0; i <= r - l; i++) {
    if (sides[i + l - now].weight < sides[mid + now].weight) {
      b[i] = sides[i + l - now];
      if (i + l - now == mid - 1) {
        for (i++; i <= r - l; i++) {
          b[i] = sides[i + l];
        }
      }
    } else {
      b[i] = sides[mid + now];
      now++;
      if (mid + now > r) {
        for (i++; i <= r - l; i++) {
          b[i] = sides[i + l - now];
        }
      }
    }
  }
  for (int i = 0; i <= r - l; i++) sides[i + l] = b[i];
}
int main() {
  int n, q, r, c = 1, ans = 0;
  bool tree[100], f;
  scanf("%d%d%d", &n, &q, &r);
  for (int i = 0; i < n; i++) tree[i] = false;
  tree[r] = true;
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &sides[i].from, &sides[i].to, &sides[i].weight);
  }
  mergesort(0, q - 1);
  while (c < n) {
    f = true;
    for (int i = 0; i < q && f; i++) {
      if (tree[sides[i].from] && !tree[sides[i].to]) {
        tree[sides[i].to] = true;
        ans += sides[i].weight;
        f = false;
      }
    }
    c++;
  }
  printf("%d\n", ans);
}
