#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> to[100];
bool checked[100];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E, r;
  scanf("%d %d %d", &V, &E, &r);
  for (int i = 0; i < E; i++) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    to[s].push_back(pair<int, int>(t, d));
  }
  int sum = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  pq.push(pair<int, int>(0, r));
  while (!pq.empty()) {
    pair<int, int> t = pq.top();
    pq.pop();
    if (checked[t.second]) {
      continue;
    }
    checked[t.second] = true;
    sum += t.first;
    for (int i = 0; i != to[t.second].size(); i++) {
      pair<int, int> e = to[t.second][i];
      pq.push(pair<int, int>(e.second, e.first));
    }
  }
  printf("%d\n", sum);
  return 0;
}
