#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int src, dst;
  int weight;
  Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
         : e.src != f.src     ? e.src < f.src
                              : e.dst < f.dst;
}
pair<int, vector<Edge> > minimumSpanningTree(const vector<vector<Edge> > &g,
                                             int r = 0) {
  int n = g.size();
  vector<Edge> T;
  int total = 0;
  vector<bool> visited(n);
  priority_queue<Edge> Q;
  Q.push(Edge(-1, r, 0));
  while (!Q.empty()) {
    Edge e = Q.top();
    Q.pop();
    if (visited[e.dst]) continue;
    T.push_back(e);
    total += e.weight;
    visited[e.dst] = true;
    for (__typeof((g[e.dst]).begin()) f = (g[e.dst]).begin();
         f != (g[e.dst]).end(); ++f)
      if (!visited[f->dst]) Q.push(*f);
  }
  return pair<int, vector<Edge> >(total, T);
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E, r;
  cin >> V >> E >> r;
  vector<vector<Edge> > G(V);
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    Edge a(s, t, w);
    G[s].push_back(a);
  }
  cout << minimumSpanningTree(G, r).first << endl;
  return 0;
}
