#include <bits/stdc++.h>
class Edge {
 public:
  int start, end, weight, bias;
  Edge(const int s = 0, const int t = 0, const int w = 0)
      : start(s), end(t), weight(w), bias(0){};
  int get_w() const { return (weight - bias); }
  bool operator>(const Edge &other) const { return get_w() < other.get_w(); }
  bool operator<(const Edge &other) const { return get_w() > other.get_w(); }
};
int main() {
  int v, e, r;
  std::scanf("%d %d %d", &v, &e, &r);
  std::vector<bool> node(v, false);
  std::vector<std::vector<std::shared_ptr<Edge> > > edge_to(v);
  std::vector<std::vector<std::shared_ptr<Edge> > > edge_from(v);
  for (auto i = 0; i < e; ++i) {
    int s, t, w;
    std::scanf("%d %d %d", &s, &t, &w);
    edge_to.at(s).push_back(std::make_shared<Edge>(Edge(s, t, w)));
    edge_from.at(t).push_back(edge_to.at(s).back());
  }
  for (auto i : edge_from) {
    int minw = INT_MAX;
    for (auto j : i) {
      if (minw > j->weight) {
        minw = j->weight;
      }
    }
    for (auto j : i) {
      j->bias = minw;
    }
  }
  std::priority_queue<Edge> queue;
  node.at(r) = true;
  for (auto i : edge_to.at(r)) {
    queue.push(*i);
  }
  int sum = 0;
  for (auto i = 1; i < v; ++i) {
    while (node.at(queue.top().end)) {
      queue.pop();
    }
    auto temp = queue.top();
    sum += temp.weight;
    queue.pop();
    node.at(temp.end) = true;
    for (auto j : edge_to.at(temp.end)) {
      if (!(node.at(j->end))) {
        queue.push(*j);
      }
    }
  }
  std::printf("%d\n", sum);
}
