#include <bits/stdc++.h>
using namespace std;
struct edge {
  int v, w;
};
vector<int> minimum_spanning_arborescence(const vector<vector<edge> > &G,
                                          int root) {
  const int INF = 1 << 29;
  const int N = G.size();
  vector<int> prev(N, -1), weight(N, INF);
  for (int i = 0; i < N; i++) {
    for (auto e : G[i]) {
      if (e.v != root && e.w < weight[e.v]) {
        weight[e.v] = e.w;
        prev[e.v] = i;
      }
    }
  }
  vector<int> check(N, N);
  vector<int> cycle;
  vector<char> in_cycle(N);
  for (int i = 0; i < N; i++) {
    cycle.clear();
    fill(begin(in_cycle), end(in_cycle), false);
    for (int j = i; j >= 0;) {
      if (check[j] < i) break;
      if (check[j] == i) goto found;
      check[j] = i;
      cycle.push_back(j);
      in_cycle[j] = true;
      j = prev[j];
    }
  }
  return prev;
found:
  int nN = N - cycle.size() + 1;
  vector<int> newv(N), oldv(N);
  for (int i = 0, idx = 0; i < N; i++) {
    if (!in_cycle[i]) {
      oldv[idx] = i;
      newv[i] = idx++;
    } else {
      newv[i] = nN - 1;
    }
  }
  vector<vector<edge> > nG(nN);
  vector<int> in(N, -1), out(N, -1);
  vector<int> in_val(N, INF), out_val(N, INF);
  for (int i = 0; i < N; i++) {
    for (auto e : G[i]) {
      int nc = e.w - weight[e.v];
      if (!in_cycle[i]) {
        if (!in_cycle[e.v]) {
          nG[newv[i]].push_back({newv[e.v], nc});
        } else {
          if (nc < in_val[i]) {
            in_val[i] = nc;
            in[i] = e.v;
          }
        }
      } else {
        if (!in_cycle[e.v]) {
          if (nc < out_val[e.v]) {
            out_val[e.v] = nc;
            out[e.v] = i;
          }
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (in_val[i] < INF) {
      nG[newv[i]].push_back({nN - 1, in_val[i]});
    }
    if (out_val[i] < INF) {
      nG[nN - 1].push_back({newv[i], out_val[i]});
    }
  }
  auto r = minimum_spanning_arborescence(nG, newv[root]);
  vector<int> rv(N);
  for (int i = 0; i < nN - 1; i++) {
    if (r[i] < 0) {
      rv[oldv[i]] = r[i];
    } else if (r[i] < nN - 1) {
      rv[oldv[i]] = oldv[r[i]];
    } else {
      rv[oldv[i]] = out[oldv[i]];
    }
  }
  int v_into_cycle = oldv[r[nN - 1]];
  int v_in_cycle = in[v_into_cycle];
  rv[v_in_cycle] = v_into_cycle;
  int x = cycle[(find(begin(cycle), end(cycle), v_in_cycle) - begin(cycle) +
                 cycle.size() - 1) %
                cycle.size()];
  for (int i = 0; i < cycle.size() - 1; i++) {
    rv[x] = prev[x];
    x = prev[x];
  }
  return rv;
}
int main() {
  int V, E, r;
  cin >> V >> E >> r;
  vector<vector<edge> > G(V);
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back({t, w});
  }
  auto msa = minimum_spanning_arborescence(G, r);
  for (auto e : msa) {
  }
  int ans = 0;
  for (int i = 0; i < G.size(); i++) {
    for (auto e : G[i]) {
      if (msa[e.v] == i) {
        ans += e.w;
      }
    }
  }
  cout << ans << endl;
}
