#include <bits/stdc++.h>
using namespace std;
vector<bool> pre;
long weight = -1;
struct HeapNode {
  int t;
  long wt;
  HeapNode(int ti, long wti) : t(ti), wt(wti) { ; }
  bool operator<(const HeapNode &rhs) const { return wt > rhs.wt; }
};
priority_queue<HeapNode> nq;
struct Graph {
  int V;
  vector<forward_list<HeapNode> > adj;
  Graph(int v) : V(v) {
    adj.resize(V);
    pre.resize(V);
  }
  void add_edge(int v, int t, long w) { adj[v].push_front(HeapNode(t, w)); }
  void bfs(int v);
};
void Graph::bfs(int v) {
  weight = 0;
  for (int i = (0); i < (V); ++i) {
    pre[i] = false;
  }
  nq.push(HeapNode(v, 0));
  while (!nq.empty()) {
    HeapNode x = nq.top();
    nq.pop();
    int t;
    if (pre[(t = x.t)]) continue;
    pre[t] = true;
    long wt1 = x.wt;
    weight += wt1;
    for (auto a = adj[t].begin(); a != adj[t].end(); ++a) {
      int w = a->t;
      if (pre[w]) continue;
      long wt2 = a->wt;
      nq.push(HeapNode(w, wt2));
    }
  }
}
int main() {
  int V, E, r, v, w, wt;
  scanf("%d %d %d", &V, &E, &r);
  Graph G(V);
  for (int i = (0); i < (E); ++i) {
    scanf("%d %d %d", &v, &w, &wt);
    G.add_edge(v, w, wt);
  }
  G.bfs(r);
  printf("%ld\n", weight);
  return 0;
}
