#include <bits/stdc++.h>
class Edge;
class Node {
 public:
  bool reach;
  std::vector<Edge> edge;
  Node() : reach(false), edge(){};
  void add_edge(const Edge &&arg) { edge.push_back(arg); }
  std::vector<Edge> &get_edge() { return edge; }
};
class Edge {
 public:
  Node *to;
  int weight;
  Edge(Node *t = nullptr, int w = 0) : to(t), weight(w){};
  bool operator>(const Edge &other) const { return weight < other.weight; }
  bool operator<(const Edge &other) const { return weight > other.weight; }
};
int main() {
  int v, e, r;
  std::scanf("%d %d %d", &v, &e, &r);
  std::vector<Node> node(v);
  std::priority_queue<Edge> queue;
  for (auto i = 0; i < e; ++i) {
    int s, t, w;
    std::scanf("%d %d %d", &s, &t, &w);
    node.at(s).add_edge(Edge(&node.at(t), w));
  }
  int sum{0};
  node.at(r).reach = true;
  for (auto i : node.at(r).get_edge()) {
    queue.push(i);
  }
  for (auto i = 1; i < v; ++i) {
    while (queue.top().to->reach) {
      queue.pop();
    }
    sum += queue.top().weight;
    Edge temp = queue.top();
    queue.pop();
    for (auto j : temp.to->get_edge()) {
      if (!(j.to->reach)) {
        queue.push(j);
      }
    }
  }
  std::printf("%d\n", sum);
}
