#include <bits/stdc++.h>
using namespace std;
template <typename T>
class MinimumArborescence {
  static constexpr T INF = numeric_limits<T>::max();

 public:
  MinimumArborescence() {}
  ~MinimumArborescence() {}
  int dfsCount(int v, vector<int> &visit,
               const vector<vector<pair<int, T> > > &G) {
    int res = 1;
    visit[v] = 1;
    for (const pair<int, T> &e : G[v]) {
      if (visit[e.first] == -1) res += dfsCount(e.first, visit, G);
    }
    return res;
  }
  T solve(int r, const vector<vector<pair<int, T> > > &G) {
    int n = G.size();
    vector<int> visit(n, -1);
    if (dfsCount(r, visit, G) != n) return INF;
    vector<vector<pair<int, T> > > H(n);
    for (int v = 0; v < n; v++) {
      for (const pair<int, T> &e : G[v]) {
        H[e.first].push_back(make_pair(v, e.second));
      }
    }
    T cost = 0;
    for (vector<int> active(n, true);;) {
      vector<T> min_cost(n, INF);
      vector<int> min_prev(n);
      fill(visit.begin(), visit.end(), -1);
      for (int v = 0; v < n; v++) {
        for (const pair<int, T> &e : H[v]) {
          if (v != r && active[v] && e.second < min_cost[v]) {
            min_cost[v] = e.second;
            min_prev[v] = e.first;
          }
        }
      }
      bool has_cycle = false;
      for (int u = 0; u < n; u++) {
        if (visit[u] != -1 || !active[u] || u == r) continue;
        int v = visit[u] = u;
        for (v = min_prev[v]; v != r && visit[v] == -1; v = min_prev[v]) {
          visit[v] = u;
        }
        if (visit[v] == u) {
          vector<int> cycle_nodes;
          for (int w = v; cycle_nodes.empty() || w != v; w = min_prev[w]) {
            cycle_nodes.push_back(w);
          }
          active.push_back(true);
          H.push_back(vector<pair<int, T> >());
          for (int v : cycle_nodes) {
            active[v] = false;
            cost += min_cost[v];
            for (const auto &e : H[v]) {
              if (active[e.first]) {
                H[n].push_back(pair<int, T>(e.first, e.second - min_cost[v]));
              }
            }
            for (const auto &e : G[v]) {
              if (active[e.first]) {
                H[e.first].push_back(pair<int, T>(n, e.second));
              }
            }
          }
          has_cycle = true;
          n++;
        }
      }
      if (!has_cycle) {
        for (int v = 0; v < n; v++) {
          if (v != r && active[v]) cost += min_cost[v];
        }
        return cost;
      }
    }
  }
};
int main(int argc, char *argv[]) {
  int n, m, r;
  cin >> n >> m >> r;
  vector<vector<pair<int, int> > > G(n);
  int s, t, w;
  for (int i = 0; i < m; i++) {
    cin >> s >> t >> w;
    G[s].push_back(make_pair(t, w));
  }
  MinimumArborescence<int> solver;
  cout << solver.solve(r, G) << endl;
  return 0;
}
