#include <bits/stdc++.h>
using namespace std;
struct OwnEdge {
  int s, t, c;
  OwnEdge(int s, int t, int c) : s(s), t(t), c(c) {}
  OwnEdge() : s(-1), t(-1), c(-1) {}
  bool operator<(const OwnEdge& e) const { return c < e.c; }
  bool operator>(const OwnEdge& e) const { return c > e.c; }
};
long long MCA(vector<OwnEdge>& edges, int V, int r) {
  vector<pair<int, int> > mins(V, pair<int, int>(200000000, -1));
  for (auto& e : edges) {
    mins[e.t] =
        ((mins[e.t]) > (pair<int, int>(e.c, e.s)) ? (pair<int, int>(e.c, e.s))
                                                  : (mins[e.t]));
  }
  mins[r] = pair<int, int>(-1, -1);
  vector<int> group(V, 0);
  vector<bool> isCycle(V, false);
  int counter = 0;
  vector<bool> checked(V, false);
  for (int i = 0; i < V; i++) {
    if (checked[i]) {
      continue;
    }
    vector<int> chain;
    int cursor = i;
    while (cursor != -1 && !checked[cursor]) {
      checked[cursor] = true;
      chain.push_back(cursor);
      cursor = mins[cursor].second;
    }
    if (cursor != -1) {
      bool inCycle = false;
      for (auto& n : chain) {
        group[n] = counter;
        if (n == cursor) {
          inCycle = true;
          isCycle[counter] = true;
        }
        if (!inCycle) counter++;
      }
      if (inCycle) counter++;
    } else {
      for (auto& n : chain) {
        group[n] = counter;
        counter++;
      }
    }
  }
  if (counter == V) {
    long long sum = 1;
    for (auto& m : mins) {
      sum += m.first;
    }
    return sum;
  }
  long long res = 0;
  for (int i = 0; i < V; i++) {
    if (i != r && isCycle[group[i]]) {
      res += mins[i].first;
    }
  }
  vector<OwnEdge> newEdge;
  for (auto& e : edges) {
    int to = e.t;
    int gfrom = group[e.s];
    int gto = group[e.t];
    if (gfrom == gto) {
      continue;
    } else if (isCycle[gto]) {
      newEdge.push_back(OwnEdge(gfrom, gto, e.c - mins[to].first));
    } else {
      newEdge.push_back(OwnEdge(e.s, e.t, e.c));
    }
  }
  return res + MCA(newEdge, counter, group[r]);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<OwnEdge> edges;
  int V, E, r;
  scanf("%d %d %d", &V, &E, &r);
  for (int i = 0; i < E; i++) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    edges.push_back(OwnEdge(s, t, d));
  }
  printf("%d\n", MCA(edges, V, r));
  return 0;
}
