#include <bits/stdc++.h>
using namespace std;
struct edge {
  int from;
  int to;
  int cost;
  bool operator<(const edge& another) const { return cost < another.cost; }
};
const int MAX_V = 105;
const int MAX_E = 1005;
vector<int> G[MAX_V];
edge es[MAX_E];
int color[MAX_V];
deque<int> L;
bool flag;
bool visited[MAX_V];
void visit(int u) {
  if (color[u] == 0) {
    flag = true;
    return;
  }
  if (color[u] == 1) {
    return;
  }
  color[u] = 0;
  for (int i = 0; i < G[u].size(); i++) {
    visit(G[u][i]);
  }
  color[u] = 1;
  L.push_front(u);
}
int main() {
  int n, m, root;
  cin >> n >> m >> root;
  for (int i = 0; i < m; ++i) {
    int u, v, r;
    scanf("%d%d%d", &u, &v, &r);
    es[i].from = u;
    es[i].to = v;
    es[i].cost = r;
  }
  sort(es, es + m);
  visited[root] = true;
  int ans = 0;
  for (int k = 0; k < m; ++k) {
    if (visited[es[k].to]) {
      continue;
    }
    visited[es[k].to] = true;
    G[es[k].from].push_back(es[k].to);
    L.clear();
    flag = false;
    for (int i = 0; i < n; ++i) {
      color[i] = -1;
    }
    for (int i = 0; i < n; ++i) {
      if (color[i] == -1) {
        flag = false;
        visit(i);
      }
      if (flag) {
        break;
      }
    }
    if (flag) {
      G[es[k].from].erase(
          remove(G[es[k].from].begin(), G[es[k].from].end(), es[k].to));
    } else {
      ans += es[k].cost;
    }
  }
  printf("%d\n", ans);
}
