#include <bits/stdc++.h>
using namespace std;
const int MAX_V = 10000;
vector<pair<int, int> > G[MAX_V];
bool f[MAX_V];
int v;
int prim(int s) {
  int r = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push(pair<int, int>(0, s));
  fill(f, f + v, 0);
  for (; !q.empty();) {
    pair<int, int> p = q.top();
    q.pop();
    int b = p.second, d = p.first;
    if (f[b]) continue;
    f[b] = 1;
    r += d;
    for (int i = 0; i < (int)G[b].size(); i++) q.push(G[b][i]);
  }
  return r;
}
int main() {
  int e, r;
  cin >> v >> e >> r;
  for (int i = 0, a, b, c; i < e && cin >> a >> b >> c; i++)
    G[a].push_back(pair<int, int>(c, b));
  cout << prim(r) << endl;
  return 0;
}
