#include <bits/stdc++.h>
using namespace std;
typedef struct edge {
  int from;
  int to;
  int cost;
} edge;
struct cost_cmp {
  bool operator()(edge x, edge y) const { return (x.cost) > (y.cost); }
};
vector<edge> G[101];
bool used[101];
int Prim(int start) {
  int res = 0;
  edge e = {0, 0, 0};
  priority_queue<edge, vector<edge>, cost_cmp> que;
  que.push(e);
  while (!que.empty()) {
    edge p = que.top();
    que.pop();
    int v = p.to;
    if (used[v]) continue;
    used[v] = true;
    res += p.cost;
    for (int i = 0; i < G[v].size(); i++) {
      int to = G[v][i].to;
      if (!used[to]) que.push(G[v][i]);
    }
  }
  return res;
}
int main() {
  int i, V, E, u, v, c, ans, r;
  cin >> V >> E >> r;
  for (i = 0; i < E; i++) {
    cin >> u >> v >> c;
    edge e;
    e.from = u;
    e.to = v;
    e.cost = c;
    G[u].push_back(e);
  }
  ans = Prim(r);
  cout << ans << endl;
  return 0;
}
