#include <bits/stdc++.h>
struct Edge {
  int to;
  int weight;
};
struct Node {
  std::vector<Edge> edges{};
};
struct lessWeight {
  bool operator()(Edge a, Edge b) { return a.weight > b.weight; }
};
int main() {
  int v, e, r;
  std::cin >> v >> e >> r;
  std::vector<Node> nodes(v);
  for (auto i = 0; i < e; ++i) {
    int s, t, w;
    std::cin >> s >> t >> w;
    nodes[s].edges.push_back(Edge{t, w});
  }
  std::vector<bool> is_joined(v, false);
  is_joined[r] = true;
  std::priority_queue<Edge, std::vector<Edge>, lessWeight> queue;
  for (auto edge : nodes[r].edges) queue.push(edge);
  int sum{0};
  for (auto i = 1; i < v; ++i) {
    while (is_joined[queue.top().to]) {
      queue.pop();
    }
    auto top = queue.top();
    queue.pop();
    is_joined[top.to] = true;
    sum += top.weight;
    for (auto edge : nodes[top.to].edges) {
      if (!is_joined[edge.to]) queue.push(edge);
    }
  }
  std::cout << sum << std::endl;
}
