#include <bits/stdc++.h>
using namespace std;
long long n, m, r;
vector<map<long long, long long>> rg;
vector<pair<long long, long long>> t;
long long x, y, c;
vector<bool> used;
vector<bool> vis;
long long res = 0;
const long long INF = 10000000000;
long long closed(long long x) {
  vis = vector<bool>(n);
  while (!used[x]) {
    used[x] = true;
    vis[x] = true;
    x = t[x].first;
  }
  if (vis[x])
    return x;
  else
    return -1;
}
template <typename T>
class union_find : public vector<pair<long long, long long>> {
 public:
  union_find() : vector<pair<long long, long long>>() {}
  union_find(long long n) : vector<pair<long long, long long>>(n) {
    for (long long i = 0; i < n; i++) {
      (*this)[i].first = i;
      (*this)[i].second = 0;
    }
  }
  long long find(T x) {
    if ((*this)[x].first == x)
      return x;
    else
      return (*this)[x].first = find((*this)[x].first);
  }
  void unite(T x, T y) {
    x = find(x);
    y = find(y);
    if (x == y) return;
    if ((*this)[x].second < (*this)[y].second)
      (*this)[x].first = y;
    else {
      (*this)[y].first = x;
      if ((*this)[x].second == (*this)[y].second) (*this)[x].second++;
    }
  }
  bool same(T x, T y) { return find(x) == find(y); }
};
union_find<long long> uf;
int main() {
  cin >> n >> m >> r;
  rg = vector<map<long long, long long>>(n);
  for (long long i = 0; i < m; i++) {
    cin >> x >> y >> c;
    rg[y][x] = c;
  }
  uf = union_find<long long>(n);
  for (;;) {
    t = vector<pair<long long, long long>>(n,
                                           pair<long long, long long>(-1, INF));
    for (long long i = 0; i < n; i++) {
      if (i == r) continue;
      auto p = min_element(
          rg[i].begin(), rg[i].end(),
          [](pair<long long, long long> a, pair<long long, long long> b) {
            return a.second < b.second;
          });
      if (p != rg[i].end() && t[uf.find(i)].second > p->second)
        t[uf.find(i)] = *p;
      t[uf.find(i)].first = uf.find(t[uf.find(i)].first);
    }
    used = vector<bool>(n);
    used[r] = true;
    for (long long i = 0; i < n; i++) {
      x = closed(uf.find(i));
      if (x >= 0) break;
    }
    if (x < 0) break;
    y = x;
    do {
      uf.unite(x, y);
      rg[y].erase(t[y].first);
      for (auto itr = rg[y].begin(); itr != rg[y].end(); itr++) {
        itr->second -= t[y].second;
      }
      res += t[y].second;
      y = t[y].first;
    } while (y != x);
  }
  for (long long i = 0; i < n; i++) {
    if (t[i].first >= 0) res += t[i].second;
  }
  cout << res << endl;
  return 0;
}
