#include <bits/stdc++.h>
using namespace std;
class edge {
 public:
  int from, to, cost;
  edge(int from = 0, int to = 0, int cost = 0)
      : from(from), to(to), cost(cost) {}
  bool operator<(const edge &e) const { return e.cost < cost; }
};
int V;
vector<edge> graph[10000];
int prim(int r) {
  bool used[10000];
  memset(used, false, sizeof(used));
  int ret = 0;
  int cnt = 0;
  priority_queue<edge> pque;
  for (int i = 0; i < graph[r].size(); i++) {
    pque.push(graph[r][i]);
  }
  cnt++;
  used[r] = true;
  while (true) {
    while (!pque.empty() && used[pque.top().to]) pque.pop();
    if (pque.empty()) break;
    int p = pque.top().to;
    cnt++;
    used[p] = true;
    ret += pque.top().cost;
    pque.pop();
    if (cnt >= V) break;
    for (int i = 0; i < graph[p].size(); i++) {
      edge e = graph[p][i];
      if (used[e.to]) continue;
      pque.push(e);
    }
  }
  return ret;
}
int main() {
  int E, r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    graph[s].push_back(edge(s, t, w));
  }
  cout << prim(r) << endl;
}
