#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] == 2) continue;
    vector<int> vs;
    vector<int> from_cost(v, 0);
    while (state[cur] != 2) {
      vs.emplace_back(cur);
      if (vpq[cur].empty()) return (1e9 + 1);
      pair<int, int> e = vpq[cur].top();
      vpq[cur].pop();
      if (cur == uf.find(e.second)) continue;
      ret += e.first + lazy[e.second];
      if (state[cur] == 1) {
        int q;
        int ind = vs.size() - 1;
        do {
          q = vs[ind--];
          lazy[q] -= from_cost[q];
          if (q != cur) {
            uf.unite(cur, q);
            while (vpq[q].size()) {
              pair<int, int> t = vpq[q].top();
              vpq[q].pop();
              t.first += lazy[q];
              vpq[cur].push(t);
            }
          }
        } while (vs[ind] != cur);
        cur = q;
      } else {
        state[cur] = 1;
        cur = uf.find(e.second);
      }
    }
    for (auto elm : vs) 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;
}
