#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned V, E, r;
  cin >> V >> E >> r;
  set<unsigned> seen;
  seen.insert(r);
  map<unsigned, vector<pair<unsigned, unsigned> > > graph;
  priority_queue<pair<unsigned, unsigned>, vector<pair<unsigned, unsigned> >,
                 greater<pair<unsigned, unsigned> > >
      q;
  unsigned distance = 0;
  for (unsigned _ = 0; _ < E; _++) {
    unsigned s, t, d;
    cin >> s >> t >> d;
    graph[s].push_back(make_pair(d, t));
    if (s == 0) q.push(make_pair(d, t));
  }
  while (!q.empty() && seen.size() != V) {
    pair<unsigned, unsigned> p = q.top();
    unsigned v = p.second;
    unsigned w = p.first;
    q.pop();
    if (seen.find(v) == seen.end()) {
      seen.insert(v);
      vector<pair<unsigned, unsigned> > neighbours = graph[v];
      for (unsigned i = 0; i < neighbours.size(); i++) q.push(neighbours[i]);
      distance += w;
    }
  }
  cout << distance << endl;
}
