#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E, r;
  cin >> V >> E >> r;
  vector<bool> visited(E, false);
  vector<vector<pair<int, int>>> Es(V);
  while (E--) {
    int s, t, w;
    cin >> s >> t >> w;
    Es[s].emplace_back(t, w);
  }
  priority_queue<tuple<int, int, int>> pq;
  pq.push(make_tuple(0, r, r));
  int cost = 0;
  while (not pq.empty()) {
    int w, s, t;
    tie(w, s, t) = pq.top();
    pq.pop();
    if (visited[t]) continue;
    cost -= w;
    visited[t] = true;
    for (auto &p : Es[t]) {
      int tt, ww;
      tie(tt, ww) = p;
      if (visited[tt]) continue;
      pq.push(make_tuple(-ww, t, tt));
    }
  }
  cout << cost << endl;
  return 0;
}
