#include <bits/stdc++.h>
namespace orislib {
struct Edge {
  int from, to;
  int w;
  Edge(int from, int to, int w) : from(from), to(to), w(w) {}
  bool operator<(const Edge& e) const {
    return w != e.w ? w > e.w : from != e.from ? from < e.from : to < e.to;
  }
  static const int INF;
};
const int Edge::INF = INT_MAX;
}  // namespace orislib
using namespace orislib;
using namespace std;
template <typename T>
inline T sq(T x) {
  return x * x;
}
template <typename T>
inline T In() {
  T x;
  cin >> x;
  return x;
}
template <typename T>
inline void Out(T& x) {
  cout << x;
}
template <typename T>
inline void sort(T& v) {
  sort(begin(v), end(v));
}
template <typename T>
inline void revs(T& v) {
  reverse(begin(v), end(v));
}
template <typename T>
inline void uniq(T& v) {
  sort(v);
  v.erase(unique(begin(v), end(v)), end(v));
}
template <typename T>
inline int ubnd(T& v, typename T::value_type& x) {
  return upper_bound(begin(v), end(v), x) - begin(v);
}
template <typename T>
inline int lbnd(T& v, typename T::value_type& x) {
  return lower_bound(begin(v), end(v), x) - begin(v);
}
template <typename T, typename F>
inline void inpt(T& v, int n, F f) {
  for (v.reserve(n); n--; v.emplace_back(f()))
    ;
}
template <typename T, typename F>
inline void show(T& v, F f, string d = " ", string e = "\n") {
  int i = 0;
  for (auto& x : v) i++ && (cout << d), f(x);
  cout << e;
}
template <size_t I = 0, typename... Ts>
inline
    typename enable_if<I == tuple_size<tuple<Ts...>>::value - 1, string>::type
    join(string s, tuple<Ts...> t) {
  return to_string(get<I>(t));
}
template <size_t I = 0, typename... Ts>
    inline typename enable_if <
    I<tuple_size<tuple<Ts...>>::value - 1, string>::type join(string s,
                                                              tuple<Ts...> t) {
  return to_string(get<I>(t)) + s + join<I + 1>(s, t);
}
template <typename T>
inline string join(string s, T t) {
  return to_string(t);
}
template <typename T, typename... Ts>
inline string join(string s, T t, Ts... ts) {
  return join(s, t) + s + join(s, ts...);
}
inline void fast_io() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
inline long long pow_mod(long long a, long long k, long long m) {
  long long r = 1;
  for (; k > 0; a = a * a % m, k >>= 1)
    if (k & 1) r = r * a % m;
  return r;
}
inline long long mod_inv(long long a, long long p) {
  return pow_mod(a, p - 2, p);
}
inline int puts(const string& s) { return puts(s.c_str()); }
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000 + 0;
std::vector<Edge> prim(const std::vector<std::vector<Edge>>& g, int s = 0) {
  const int V = g.size();
  vector<bool> visited(V, false);
  priority_queue<Edge> pq;
  std::vector<Edge> result;
  for (pq.push(Edge(-2, s, 0)); !pq.empty();) {
    Edge c = pq.top();
    pq.pop();
    if (visited[c.to]) continue;
    visited[c.to] = true;
    result.emplace_back(c);
    for (const auto& e : g[c.to]) {
      pq.push(e);
    }
  }
  return result;
}
int main() {
  int V, E, r;
  V = in(), E = in(), r = in();
  std::vector<std::vector<Edge>> g(V);
  for (int i = 0; i < E; i++) {
    int s, t, d;
    s = in(), t = in(), d = in();
    g[s].emplace_back(s, t, d);
  }
  auto es = prim(g, r);
  int tot = 0;
  for (const auto& e : es) tot += e.w;
  cout << tot << endl;
  return 0;
}
