#include <bits/stdc++.h>
using namespace std;
struct edge {
  int from, to, wt, next;
} E[2 * 1000];
struct dfsdata {
  int id, parent;
  bool after;
};
int LE[2 * 100];
bool active[2 * 100];
int next_m = 1000;
int next_n = 100;
int n, m, r;
int CMCA(int nactive) {
  if (nactive == 1) return 0;
  int m[2 * 100];
  fill(m, m + 2 * 100, 11000);
  for (int i = 0; i < 2 * 100; ++i) {
    if (!active[i]) continue;
    for (int e = LE[i]; e != -1; e = E[e].next)
      m[E[e].to] = min(m[E[e].to], E[e].wt);
  }
  bool flag[2 * 100]{0};
  set<int> fstar;
  for (int i = 0; i < 2 * 100; ++i) {
    if (!active[i]) continue;
    for (int e = LE[i]; e != -1; e = E[e].next) {
      E[e].wt -= m[E[e].to];
      if (E[e].wt == 0 && !flag[E[e].to] && E[e].to != r) {
        fstar.insert(e);
        flag[E[e].to] = true;
      }
    }
  }
  stack<dfsdata> dfs;
  bool visited[2 * 100]{0};
  int parent[2 * 100]{0}, pre[2 * 100]{0}, post[2 * 100]{0};
  int clk = 0;
  for (int i = 0; i < 2 * 100; ++i) {
    if (!visited[i] and active[i]) {
      dfs.push({i, -1, false});
      while (!dfs.empty()) {
        dfsdata d = dfs.top();
        dfs.pop();
        if (visited[d.id] && !d.after) continue;
        if (visited[d.id] && d.after) {
          post[d.id] = ++clk;
        }
        if (!visited[d.id]) {
          pre[d.id] = ++clk;
          visited[d.id] = true;
          parent[d.id] = d.parent;
          dfs.push({d.id, d.parent, true});
          for (int e = LE[d.id]; e != -1; e = E[e].next) {
            if (!fstar.count(e) or visited[E[e].to]) continue;
            dfs.push({E[e].to, d.id, false});
          }
        }
      }
    }
  }
  for (int e : fstar) {
    if (pre[E[e].to] < pre[E[e].from] && post[E[e].to] > post[E[e].from]) {
      bool in_cycle[2 * 100]{0};
      vector<int> C;
      int curr = E[e].from;
      while (curr != E[e].to) {
        C.push_back(curr);
        curr = parent[curr];
      }
      C.push_back(E[e].to);
      for (int u : C) in_cycle[u] = true;
      int cv = next_n++;
      for (int i = 0; i < 2 * 100; ++i) {
        for (int ei = LE[i]; ei != -1; ei = E[ei].next) {
          if (in_cycle[i] && !in_cycle[E[ei].to]) {
            E[next_m] = {cv, E[ei].to, E[ei].wt, LE[cv]};
            LE[cv] = next_m;
            ++next_m;
          }
          if (!in_cycle[i] && in_cycle[E[ei].to]) {
            E[next_m] = {i, cv, E[ei].wt, LE[i]};
            LE[i] = next_m;
            ++next_m;
          }
        }
      }
      int c0 = 0;
      for (int i = 0; i < 2 * 100; ++i) {
        if (active[i] and i != r) c0 += m[i];
      }
      for (int v : C) active[v] = false;
      active[cv] = true;
      return c0 + CMCA(nactive - C.size() + 1);
    }
  }
  int to_return = 0;
  for (int i = 0; i < 2 * 100; ++i) {
    if (active[i] and i != r) to_return += m[i];
  }
  return to_return;
}
int main() {
  cin >> n >> m >> r;
  fill(LE, LE + 2 * 100, -1);
  fill(active, active + n, true);
  fill(active + n, active + 2 * n, false);
  for (int src, dst, cost, i = 0; i < m; ++i) {
    cin >> src >> dst >> cost;
    E[i] = {src, dst, cost, LE[src]};
    LE[src] = i;
  }
  cout << CMCA(n) << endl;
}
