#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 105;
struct edge {
  int to, cost;
};
vector<edge> G[MAX_N];
bool flag[MAX_N];
int main() {
  int n, m, r;
  cin >> n >> m >> r;
  for (int i = 0; i < (int)m; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    G[x].push_back((edge){y, z});
  }
  for (int i = 0; i < (int)n; ++i) {
    flag[i] = false;
  }
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      que;
  flag[r] = true;
  for (int i = 0; i < (int)G[r].size(); ++i) {
    que.push(pair<int, int>(G[r][i].cost, G[r][i].to));
  }
  int cnt = 0;
  int ans = 0;
  while (!que.empty()) {
    pair<int, int> p = que.top();
    que.pop();
    if (flag[p.second]) {
      continue;
    } else {
      flag[p.second] = true;
      cnt++;
      ans += p.first;
    }
    if (cnt == n - 1) {
      break;
    }
    for (int i = 0; i < (int)G[p.second].size(); ++i) {
      if (!flag[G[p.second][i].to]) {
        que.push(pair<int, int>(G[p.second][i].cost, G[p.second][i].to));
      }
    }
  }
  cout << ans << "\n";
  return 0;
}
