#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
void chmin(T1& a, T2 b) {
  if (a > b) a = b;
}
template <class T1, class T2>
void chmax(T1& a, T2 b) {
  if (a < b) a = b;
}
const long long inf = 1LL << 55;
const long long mod = 1e9 + 7;
struct SCC {
  vector<vector<long long> > graph, rgraph, dag;
  vector<pair<long long, long long> > edges;
  vector<long long> cmp;
  SCC(long long V) : graph(V), rgraph(V), cmp(V, -1) {}
  void add_edge(long long from, long long to) {
    graph[from].push_back(to);
    rgraph[to].push_back(from);
    edges.emplace_back(from, to);
  }
  void dfs(long long u, vector<long long>& ord, vector<long long>& used) {
    if (used[u]) return;
    used[u] = 1;
    for (long long v : graph[u]) dfs(v, ord, used);
    ord.push_back(u);
  }
  void rdfs(long long u, long long& id) {
    if (~cmp[u]) return;
    cmp[u] = id;
    for (long long v : rgraph[u]) rdfs(v, id);
  }
  long long build() {
    vector<long long> ord, used(graph.size(), 0);
    for (long long u = 0; u < (long long)graph.size(); u++) dfs(u, ord, used);
    reverse(ord.begin(), ord.end());
    long long sz = 0;
    for (long long u : ord)
      if (cmp[u] == -1) rdfs(u, sz), sz++;
    return sz;
  }
};
struct ChuLiu_Edmonds {
  struct edge {
    long long to, cost;
    edge() {}
    edge(long long to, long long cost) : to(to), cost(cost) {}
  };
  vector<vector<edge> > graph;
  vector<long long> indeg;
  ChuLiu_Edmonds(long long V) : graph(V), indeg(V, 0) {}
  void add_edge(long long from, long long to, long long cost) {
    graph[from].emplace_back(to, cost);
    indeg[to]++;
  }
  long long get_root() {
    for (long long r = 0; r < (long long)graph.size(); r++) {
      if (indeg[r] == 0) return r;
    }
    return -1;
  }
  long long minimumCostArborescence(vector<vector<edge> >& g, long long root) {
    long long N = g.size();
    vector<long long> into(N), mincost(N, inf);
    for (long long u = 0; u < N; u++) {
      for (edge& e : graph[u]) {
        if (e.cost < mincost[e.to]) {
          into[e.to] = u;
          mincost[e.to] = e.cost;
        }
      }
    }
    SCC scc(N);
    for (long long u = 0; u < N; u++) {
      if (u == root) continue;
      scc.add_edge(into[u], u);
    }
    long long scc_sz = scc.build();
    if ((long long)scc_sz == N) {
      long long res = 0;
      for (long long u = 0; u < N; u++) {
        if (u == root) continue;
        res += mincost[u];
      }
      return res;
    }
    long long cycle_cost = 0;
    for (long long u = 0; u < N; u++) {
      if (scc.cmp[u] == scc.cmp[into[u]]) cycle_cost += mincost[u];
    }
    vector<vector<edge> > reduce_g(scc_sz);
    for (long long u = 0; u < N; u++) {
      for (edge& e : graph[u]) {
        if (scc.cmp[u] == scc.cmp[e.to]) continue;
        reduce_g[scc.cmp[u]].emplace_back(scc.cmp[e.to],
                                          e.cost - mincost[e.to]);
      }
    }
    return cycle_cost + minimumCostArborescence(reduce_g, root);
  }
  long long build(long long root = -1) {
    if (root == -1) root = get_root();
    return minimumCostArborescence(graph, root);
  }
};
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);
  long long N, M, r;
  cin >> N >> M >> r;
  ChuLiu_Edmonds graph(N);
  for (long long i = (long long)(0); i < (long long)(M); i++) {
    long long s, t, w;
    cin >> s >> t >> w;
    graph.add_edge(s, t, w);
  }
  cout << graph.build(r) << endl;
  return 0;
}
