#include <bits/stdc++.h>
using namespace std;
typedef priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                       greater<pair<long long, int> > >
    pq;
struct UF {
  int rep[1000000];
  UF() {
    for (int i = 0; i < 1000000; i++) rep[i] = i;
  }
  int findrep(int a) {
    if (rep[a] == a) return a;
    return rep[a] = findrep(rep[a]);
  }
  void merge(int a, int b) { rep[findrep(a)] = findrep(b); }
  bool connected(int a, int b) { return findrep(a) == findrep(b); }
};
UF stronglyconnected, weaklyconnected;
int v, e;
pq* adj[1000000];
int incoming[1000000];
long long incomingDis[1000000];
pq* merge(pq* a, pq* b) {
  if (a->size() > b->size()) swap(a, b);
  while (!a->empty()) {
    b->push(a->top());
    a->pop();
  }
  return b;
}
long long ans;
int main() {
  scanf("%d%d", &v, &e);
  int r;
  scanf("%d", &r);
  for (int i = 0; i < v; i++) {
    adj[i] = new pq();
  }
  for (int i = 0; i < e; i++) {
    int a, b;
    long long c;
    scanf("%d%d%lld", &a, &b, &c);
    adj[b]->emplace(c, a);
  }
  queue<int> q;
  for (int i = 0; i < v; i++) {
    if (i != r) q.push(i);
  }
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    while (!adj[a]->empty() &&
           stronglyconnected.connected(a, adj[a]->top().second))
      adj[a]->pop();
    incoming[a] = adj[a]->top().second;
    incomingDis[a] = adj[a]->top().first;
    incoming[a] = stronglyconnected.findrep(incoming[a]);
    adj[a]->pop();
    ans += incomingDis[a];
    int b = incoming[a];
    if (weaklyconnected.connected(a, b)) {
      long long mxincycle = incomingDis[a];
      for (int i = b; i != a; i = stronglyconnected.findrep(incoming[i])) {
        mxincycle = max(mxincycle, incomingDis[i]);
        stronglyconnected.merge(i, a);
        adj[a] = merge(adj[a], adj[i]);
      }
      ans -= mxincycle;
      q.push(a);
    } else {
      weaklyconnected.merge(a, b);
    }
  }
  printf("%lld\n", ans);
}
