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