#include <bits/stdc++.h>
using namespace std;
class Edge {
 public:
  int to, cost;
  Edge(int t, int c) {
    to = t;
    cost = c;
  }
};
vector<Edge> G[10000 + 1];
int V, E;
int mincost[10000 + 1];
bool used[10000 + 1];
int solve(int r) {
  fill(mincost, mincost + 10000 + 1, INT_MAX);
  fill(used, used + 10000 + 1, false);
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      qu;
  int res = 0;
  qu.push(pair<int, int>(0, r));
  while (!qu.empty()) {
    pair<int, int> p = qu.top();
    qu.pop();
    int v = p.second;
    if (used[v]) continue;
    used[v] = true;
    res += p.first;
    for (int i = 0; i < G[v].size(); i++) {
      Edge e = G[v][i];
      if (!used[e.to]) qu.push(pair<int, int>(e.cost, e.to));
    }
  }
  return res;
}
int main() {
  int r;
  cin >> V >> E >> r;
  int s, t, c;
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> c;
    Edge e1(t, c);
    G[s].push_back(e1);
  }
  cout << solve(r) << endl;
  return 0;
}
