#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
struct vertex {
  vector<P> IN;
};
vector<vertex> V;
int nv, ne, r;
bool isUsed[100 * 2];
void erase(int s, int t) {
  for (auto ite = V[t].IN.begin(); ite != V[t].IN.end(); ite++) {
    if (ite->second == s) {
      V[t].IN.erase(ite);
      return;
    }
  }
}
void add(int s, int t, int w) { V[t].IN.emplace_back(w, s); }
bool isCircle(int i, bool* isUsed, vector<int>& C) {
  if (isUsed[i]) {
    C.push_back(i);
    return true;
  }
  isUsed[i] = true;
  for (auto& e : V[i].IN) {
    if (isCircle(e.second, isUsed, C)) {
      C.push_back(i);
      return true;
    }
  }
  return false;
}
void edmonds() {
  for (int i = 0; i < V.size(); i++) {
    sort(V[i].IN.begin(), V[i].IN.end());
    for (auto ite = V[i].IN.begin(); ite != V[i].IN.end(); ite++) {
      if (ite->first != V[i].IN[0].first) {
        V[i].IN.erase(ite, V[i].IN.end());
        break;
      }
    }
  }
  vector<int> C;
  for (int i = 0; i < V.size(); i++) {
    C.clear();
    fill(isUsed, isUsed + 100 * 2, false);
    if (isCircle(r, isUsed, C)) break;
  }
  if (C.empty()) {
    return;
  } else {
    for (auto ite = C.begin() + 1; ite != C.end(); ite++) {
      if (*ite == C[0]) {
        C.erase(ite, C.end());
        reverse(C.begin(), C.end());
        break;
      }
    }
  }
  int INv[V.size()];
  fill(INv, INv + V.size(), -1);
  int INw[V.size()];
  fill(INw, INw + V.size(), 10000 + 1);
  for (auto& v : C) {
    for (auto& e : V[v].IN) {
      int w = e.first, s = e.second;
      if (INw[s] > w) {
        INw[s] = w;
        INv[s] = v;
      }
      erase(s, v);
    }
  }
  int OUTv[V.size()];
  fill(OUTv, OUTv + V.size(), -1);
  int OUTw[V.size()];
  fill(OUTw, OUTw + V.size(), 10000 + 1);
  for (int i = 0; i < V.size(); i++) {
    for (auto& e : V[i].IN) {
      int w = e.first, s = e.second;
      for (auto& i : C) {
        if (i == s && OUTw[i] > w) {
          OUTw[i] = w;
          OUTv[i] = s;
        }
        erase(s, i);
      }
    }
  }
  int vc = V.size();
  V.emplace_back();
  for (int i = 0; i < vc; i++) {
    if (~INv[i]) add(i, vc, INw[i]);
  }
  for (int i = 0; i < vc; i++) {
    if (~OUTv[i]) add(vc, i, OUTw[i]);
  }
  edmonds();
  int s = V[vc].IN[0].second, w = V[vc].IN[0].first;
  add(s, INv[s], w);
  for (int i = 0; i < C.size(); i++) {
    if (V[C[i]].IN.empty()) add(i + 1 == C.size() ? 0 : i + 1, i, INw[i]);
  }
  for (int i = 0; i < vc; i++) {
    if (~OUTv[i]) add(OUTv[i], i, OUTw[i]);
  }
  V.erase(V.begin() + vc);
  return;
}
int main() {
  cin >> nv >> ne >> r;
  for (int i = 0; i < nv; i++) V.emplace_back();
  for (int i = 0; i < ne; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    if (t == r) continue;
    add(s, t, w);
  }
  edmonds();
  int ans = 0;
  for (auto& v : V) {
    for (auto& e : v.IN) {
      ans += e.first;
    }
  }
  cout << ans << endl;
  return 0;
}
