#include <bits/stdc++.h>
struct vertex {
  std::vector<int> edge_to;
  std::vector<int> edge_cost;
  bool done;
  long long int mincost;
};
std::vector<vertex> G(100000);
void shortest_path(int v);
int prim(int x);
int V, E;
int r;
int main(void) {
  std::cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    int from, to, cost;
    std::cin >> from >> to >> cost;
    G[from].edge_to.push_back(to);
    G[from].edge_cost.push_back(cost);
  }
  std::cout << prim(r) << std::endl;
  return 0;
}
void shortest_path(int v) {
  for (int i = 0; i < V; i++) {
    G[i].mincost = 50000000000;
  }
  G[v].mincost = 0;
  std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int> >,
                      std::greater<std::pair<int, int> > >
      que;
  que.push(std::pair<int, int>(0, v));
  while (!que.empty()) {
    std::pair<int, int> p = que.top();
    que.pop();
    int from = p.second;
    if (G[from].mincost < p.first) {
      continue;
    }
    for (int i = 0; i < G[from].edge_to.size(); i++) {
      int to = G[from].edge_to[i];
      int cost_to = G[from].edge_cost[i];
      if (G[to].mincost > G[from].mincost + cost_to) {
        G[to].mincost = G[from].mincost + cost_to;
        que.push(std::pair<int, int>(G[to].mincost, to));
      }
    }
  }
}
int prim(int x) {
  for (int i = 0; i < V; i++) {
    G[i].mincost = 50000000000;
    G[i].done = false;
  }
  G[x].mincost = 0;
  int res = 0;
  while (1) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!G[u].done && (v == -1 || G[u].mincost < G[v].mincost)) {
        v = u;
      }
    }
    if (v == -1) {
      break;
    }
    G[v].done = true;
    res += G[v].mincost;
    for (int i = 0; i < G[v].edge_to.size(); i++) {
      int to = G[v].edge_to[i];
      int to_cost = G[v].edge_cost[i];
      if (G[to].mincost > to_cost) {
        G[to].mincost = to_cost;
      }
    }
  }
  return res;
}
