#include <bits/stdc++.h>
using namespace std;
long long Chi_Liu_Edmonds(vector<pair<int, pair<int, int> > > edges, int V,
                          int start) {
  vector<pair<int, int> > mins(V, make_pair(1000000007, -1));
  for (int i = (int)(0); i < (int)(edges.size()); i++) {
    pair<int, pair<int, int> > edge = edges[i];
    mins[edge.second.second] =
        min(mins[edge.second.second], make_pair(edge.first, edge.second.first));
  }
  mins[start] = make_pair(-1, -1);
  vector<int> group(V, 0);
  vector<bool> isCycle(V, 0);
  int count = 0;
  vector<bool> used(V, false);
  for (int i = (int)(0); i < (int)(V); i++) {
    if (used[i]) continue;
    vector<int> chain;
    int cursor = i;
    while (cursor != -1 && !used[cursor]) {
      used[cursor] = true;
      chain.push_back(cursor);
      cursor = mins[cursor].second;
    }
    if (cursor != -1) {
      bool inCycle = false;
      for (int j = (int)(0); j < (int)(chain.size()); j++) {
        group[chain[j]] = count;
        if (chain[j] == cursor) {
          isCycle[count] = true;
          inCycle = true;
        }
        if (!inCycle) count++;
      }
      if (inCycle) count++;
    } else {
      for (int j = (int)(0); j < (int)(chain.size()); j++) {
        group[j] = count;
        count++;
      }
    }
  }
  if (count == V) {
    long long ans = 1;
    for (int i = (int)(0); i < (int)(V); i++) {
      ans += mins[i].first;
    }
    return ans;
  }
  long long res = 0;
  for (int i = (int)(0); i < (int)(V); i++) {
    if (i != start && isCycle[group[i]]) {
      res += mins[i].first;
    }
  }
  vector<pair<int, pair<int, int> > > newEdges;
  for (int i = (int)(0); i < (int)(edges.size()); i++) {
    pair<int, pair<int, int> > edge = edges[i];
    int to = edge.second.second;
    int gfrom = group[edge.second.first];
    int gto = group[edge.second.second];
    if (gfrom == gto) {
      continue;
    } else if (isCycle[gto]) {
      newEdges.push_back(
          make_pair(edge.first - mins[to].first, make_pair(gfrom, gto)));
    } else {
      newEdges.push_back(make_pair(edge.first, make_pair(gfrom, gto)));
    }
  }
  return res + Chi_Liu_Edmonds(newEdges, count, group[start]);
}
int main() {
  int V, E, r;
  cin >> V >> E >> r;
  vector<pair<int, pair<int, int> > > es;
  for (int i = (int)(0); i < (int)(E); i++) {
    int s, t, w;
    cin >> s >> t >> w;
    es.push_back(make_pair(w, make_pair(s, t)));
  }
  cout << Chi_Liu_Edmonds(es, V, r) << endl;
  return 0;
}
