#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, cost;
};
vector<edge> G[101];
int main() {
  int V, E, r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(edge{t, w});
  }
  bool used[101];
  fill(used, used + 101, false);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  for (int i = 0; i < G[r].size(); ++i) {
    edge e = G[r][i];
    que.push(pair<int, int>(e.cost, e.to));
  }
  used[r] = true;
  int ans = 0;
  while (!que.empty()) {
    pair<int, int> p = que.top();
    que.pop();
    int v = p.second;
    if (used[v]) continue;
    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (!used[e.to]) {
        que.push(pair<int, int>(e.cost, e.to));
      }
    }
    used[v] = true;
    ans += p.first;
  }
  cout << ans << endl;
  return 0;
}
