#include <bits/stdc++.h>
using Int = int64_t;
using UInt = uint64_t;
using C = std::complex<double>;
template <typename T>
using RQ = std::priority_queue<T, std::vector<T>, std::greater<T>>;
struct Tree {
  Tree(Int n) : ps(n, -1) {}
  std::vector<Int> ps;
  Int root(Int x) {
    if (ps[x] == -1) return x;
    return ps[x] = root(ps[x]);
  }
  void connect(Int parent, Int child) { ps[root(child)] = root(parent); }
};
int main() {
  Int v, e, r;
  std::cin >> v >> e >> r;
  std::vector<Int> ss(e), ts(e), ws(e);
  for (Int i = 0; i < (Int)(e); ++i) std::cin >> ss[i] >> ts[i] >> ws[i];
  std::vector<RQ<std::pair<Int, Int>>> qs(v);
  for (Int i = 0; i < (Int)(e); ++i) {
    Int s = ss[i], t = ts[i], w = ws[i];
    qs[t].emplace(w, s);
  }
  std::vector<Int> xs(v);
  xs[r] = 2;
  Tree tree(v);
  bool fail = false;
  Int res = 0;
  for (Int i = 0; i < (Int)(v); ++i) {
    Int a = tree.root(i);
    assert(xs[a] == 0 or xs[a] == 2);
    if (not(xs[a] == 0)) continue;
    ;
    std::vector<Int> ps(v);
    std::vector<Int> ys;
    while (xs[a] != 2) {
      ys.emplace_back(a);
      xs[a] = 1;
      if (qs[a].empty()) {
        fail = true;
        break;
      }
      Int w, p;
      std::tie(w, p) = qs[a].top();
      qs[a].pop();
      ps[a] = p;
      res += w;
      if (xs[ps[a]] != 1) {
        a = ps[a];
      } else {
        Int t = a;
        Int b = a;
        do {
          if (qs[t].size() < qs[b].size()) t = b;
          b = ps[b];
        } while (b != a);
        b = a;
        do {
          if (not(t != b)) continue;
          ;
          tree.connect(t, b);
          while (not qs[b].empty()) {
            Int w2, p2;
            std::tie(w2, p2) = qs[b].top();
            qs[b].pop();
            qs[t].emplace(p2, w2 - w);
          }
          b = tree.root(ps[b]);
        } while (b != a);
      }
    }
    if (fail) break;
    for (Int y : ys) {
      xs[y] = 2;
    }
  }
  printf("%ld\n", res);
}
