#include <bits/stdc++.h>
template <class T, class S>
void cmin(T &a, const S &b) {
  if (a > b) a = b;
}
template <class T, class S>
void cmax(T &a, const S &b) {
  if (a < b) a = b;
}
long long int INF = 1e9 * 1e9;
using namespace std;
signed main() {
  long long int v, e, r;
  cin >> v >> e >> r;
  using piii = pair<pair<long long int, long long int>, long long int>;
  vector<bool> used(v, true);
  vector<vector<pair<long long int, long long int>>> edge(
      v, vector<pair<long long int, long long int>>());
  for (long long int i = 0; i < e; i++) {
    long long int a, b, c;
    cin >> a >> b >> c;
    edge[a].push_back(pair<long long int, long long int>(b, c));
  }
  priority_queue<pair<long long int, long long int>,
                 vector<pair<long long int, long long int>>,
                 greater<pair<long long int, long long int>>>
      que;
  que.push(pair<long long int, long long int>(0, r));
  used[0] = true;
  long long int res = 0;
  while (!que.empty()) {
    pair<long long int, long long int> now = que.top();
    que.pop();
    if (used[now.second] == false) continue;
    res += now.first;
    for (long long int i = 0; i < edge[now.second].size(); i++) {
      que.push(pair<long long int, long long int>(edge[now.second][i].second,
                                                  edge[now.second][i].first));
    }
    used[now.second] = false;
  }
  cout << res << endl;
}
