#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, cost;
};
typedef pair<int, int> P;
int V;
vector<edge> G[100000];
int d[100000];
int r;
int prim() {
  priority_queue<P, vector<P>, greater<P> > que;
  fill(d, d + V, 111111111);
  int res = 0;
  que.push(P(0, r));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] != 111111111) continue;
    res += p.first;
    d[v] = p.first;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] == 111111111) que.push(P(e.cost, e.to));
    }
  }
  return res;
}
int main() {
  int E;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    int s, t, c;
    edge e;
    cin >> s >> t >> c;
    e.to = t, e.cost = c;
    G[s].push_back(e);
  }
  cout << prim() << endl;
  return 0;
}
