#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int kInfinity = 0x3F3F3F3F;
const int kNil = -1;
const int kMaxV = 10010;

template <class T>
struct Edge {
  using w_type = T;
  Edge(int u, int v, w_type w): u(u), v(v), w(w) { }
  int u, v;
  w_type w;
  bool operator<(const Edge<T> & rhs) const {
    if (w != rhs.w) { return w < rhs.w; }
    if (u != rhs.u) { return u < rhs.u; }
    return v < rhs.v;
  }
};

template <class Edge>
decltype(auto) ChuLiu(vector<Edge> & edges,
                 typename vector<Edge>::size_type node_num,
                 typename vector<Edge>::size_type root) {
  using size_type = typename vector<Edge>::size_type;
  using const_it = typename vector<Edge>::const_iterator;
  using w_type = typename Edge::w_type;

  w_type weight_sum{0};
  vector<w_type> min_in(node_num, kInfinity);
  vector<int> parent(node_num, kNil);

  while (1) {
    min_in = vector<w_type>(node_num, kInfinity);
    parent = vector<int>(node_num, kNil);

    for (const auto & e: edges) {
      if (e.w < min_in[e.v] && e.u != e.v) {
        min_in[e.v] = e.w;
        parent[e.v] = e.u;
      }
    }
    min_in[root] = 0;

    // Impossible
    // for (unsigned i = 0; i < node_num; ++i) {
    //   if (min_in[i] == kInfinity) { cout << "IMPOSSIBLE\n"; }

    size_type group_count = 0;
    vector<size_type> new_no(node_num, kNil);
    vector<int> group(node_num, kNil);
    for (unsigned i = 0; i < node_num; ++i) {
      if (i == root) { continue; }
      weight_sum += min_in[i];
      auto j = i;
      while (group[j] != i && new_no[j] == kNil && j != root) {
        group[j] = i;
        j = parent[j];
      }
      if (new_no[j] == kNil && j != root) {
        // contract the circuit
        auto k = j;
        do {
          new_no[k] = group_count;
          k = parent[k];
        } while (k != j);
        ++group_count;
      }
    }

    if (group_count == 0) { break; }
    // new graph
    for (unsigned i = 0; i < node_num; ++i) {
      if (new_no[i] == kNil) {
        new_no[i] = ++group_count;
      }
    }
    for (auto & e: edges) {
      auto origin_v = e.v;
      e.u = new_no[e.u];
      e.v = new_no[e.v];
      if (e.u != e.v) {
        e.w -= min_in[origin_v];
      }
    }
    node_num = group_count;
    root = new_no[root];
  }

  return weight_sum;
}

int main(int argc, char const *argv[]) {
  vector<Edge<unsigned>> edges;
  unsigned V, E, r;
  cin >> V >> E >> r;
  for (unsigned i = 0; i < E; ++i) {
    unsigned u, v;
    unsigned w;
    cin >> u >> v >> w;
    edges.emplace_back(u, v, w);
  }
  cout << ChuLiu(edges, V, r) << endl;
  return 0;
}