#include <bits/stdc++.h>
using namespace std;
using edge = pair<int, pair<int, int>>;
int INF = 1 << 30;
vector<edge> Edmonds(const vector<edge>& G, int V, int r) {
  vector<pair<int, int>> MIN(V, pair<int, int>(INF, -1));
  for (auto& e : G) {
    MIN[e.second.second] =
        min(MIN[e.second.second], pair<int, int>(e.first, e.second.second));
  }
  MIN[r] = {-1, -1};
  vector<int> group(V, 0);
  vector<bool> isCycle(V, false);
  int count = 0;
  vector<bool> used(V, false);
  for (int i = 0; i < V; i++) {
    if (used[i]) continue;
    vector<int> chain;
    int now = i;
    while (now != -1 && !used[now]) {
      used[now] = 1;
      chain.push_back(now);
      now = MIN[now].second;
    }
    if (now != -1) {
      bool inCycle = false;
      for (int j : chain) {
        group[j] = count;
        if (j == now) {
          isCycle[count] = true;
          inCycle = true;
        }
        if (!inCycle) count++;
      }
      if (inCycle) count++;
    } else {
      for (int j : chain) {
        group[j] = count;
        count++;
      }
    }
  }
  vector<edge> ans;
  if (count == V) {
    for (int i = 0; i < V; i++) {
      if (i == r) continue;
      ans.push_back({MIN[i].first, {i, MIN[i].second}});
    }
    return ans;
  }
  for (int i = 0; i < V; i++) {
    if (i = r) continue;
    if (isCycle[group[i]]) ans.push_back({MIN[i].first, {i, MIN[i].second}});
  }
  vector<edge> nG;
  for (auto& e : G) {
    int to = e.second.second;
    int gfrom = group[e.second.first];
    int gto = group[e.second.second];
    if (gfrom == gto)
      continue;
    else if (isCycle[gto])
      nG.push_back({e.first - MIN[to].first, {gfrom, gto}});
    else {
      nG.push_back({e.first, {gfrom, gto}});
    }
  }
  auto res = Edmonds(nG, count, group[r]);
  for (auto& e : res) ans.push_back(e);
  return ans;
}
int main() {
  int V, E, r;
  cin >> V >> E >> r;
  vector<edge> G;
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G.push_back({w, {s, t}});
  }
  auto res = Edmonds(G, V, r);
  int sum = 0;
  for (auto& e : res) {
    sum += e.first;
  }
  cout << sum << endl;
}
