#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
  int nv, ne, r;
  cin >> nv >> ne >> r;
  vector<P> V[10000];
  for (int i = 0; i < ne; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    V[s].emplace_back(w, t);
  }
  bool isUsed[nv];
  fill(isUsed, isUsed + nv, false);
  priority_queue<P, vector<P>, greater<P>> que;
  que.emplace(0, r);
  int ans = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int from = p.second;
    if (isUsed[from]) continue;
    isUsed[from] = true;
    ans += p.first;
    for (auto& e : V[from]) {
      int to = e.second, weight = e.first;
      if (!isUsed[to]) que.emplace(weight, to);
    }
  }
  cout << ans << endl;
  return 0;
}
