#include <bits/stdc++.h>
using namespace std;
int e, v;
struct edge {
  int from, to, cost;
};
vector<edge> edges;
class UF {
 private:
  vector<int> par;

 public:
  int groups;
  UF(int __size) : par(__size) {
    for (int i = 0; i < __size; i++) par[i] = i;
  }
  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    par[y] = x;
  }
};
int MSA(int r) {
  int ret = 0;
  UF uf(v);
  vector<priority_queue<pair<int, int>, vector<pair<int, int>>,
                        greater<pair<int, int>>>>
      vpq(v);
  vector<int> lazy(v, 0);
  vector<int> state(v, 0);
  for (auto e : edges) {
    vpq[e.to].push(pair<int, int>(e.cost, e.from));
  }
  state[r] = 2;
  for (long long start = 0; start < (long long)(v); start++) {
    int cur = start;
    if (state[cur] != 0) continue;
    vector<int> processing;
    vector<int> from(v, -1);
    vector<int> from_cost(v, 0);
    while (state[cur] != 2) {
      processing.emplace_back(cur);
      state[cur] = 1;
      if (vpq[cur].empty()) return (1e9 + 1);
      pair<int, int> e = vpq[cur].top();
      vpq[cur].pop();
      from[cur] = e.second;
      from_cost[cur] = e.first;
      if (cur == from[cur]) continue;
      ret += from_cost[cur] + lazy[cur];
      if (state[from[cur]] == 1) {
        int p = cur;
        do {
          lazy[p] -= from_cost[p];
          if (p != cur) {
            uf.unite(cur, p);
            while (vpq[p].size()) {
              pair<int, int> t = vpq[p].top();
              vpq[p].pop();
              t.first += lazy[p] - lazy[cur];
              vpq[cur].push(t);
            }
          }
          p = uf.find(from[p]);
        } while (p != cur);
      } else {
        cur = from[cur];
      }
    }
    for (auto elm : processing) state[elm] = 2;
  }
  return ret;
}
int main() {
  int r;
  cin >> v >> e >> r;
  for (long long i = 0; i < (long long)(e); i++) {
    int s, t, w;
    cin >> s >> t >> w;
    edges.emplace_back(edge{s, t, w});
  }
  cout << MSA(r) << endl;
}
