#include <bits/stdc++.h>
const int INF = 2000000000;
using namespace std;
class Edge {
 public:
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
int prim(const vector<vector<Edge>> &g, int s) {
  int n = g.size();
  int total = 0;
  vector<bool> visited(n);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push(make_pair(0, s));
  while (not q.empty()) {
    pair<int, int> u = q.top();
    q.pop();
    if (visited[u.second]) continue;
    total += u.first;
    visited[u.second] = true;
    for (auto it : g[u.second]) {
      if (not visited[it.to]) q.push(make_pair(it.cost, it.to));
    }
  }
  return total;
}
int main() {
  int v, e, r;
  cin >> v >> e >> r;
  vector<vector<Edge>> g(v);
  for (int i = 0; i < (e); i++) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].emplace_back(b, c);
  }
  cout << prim(g, r) << endl;
}
