#include <bits/stdc++.h>
using namespace std;
int main() {
  int v, e, r;
  cin >> v >> e >> r;
  int graph[101][101];
  for (int i = 0; i < v; i++) {
    for (int j = 0; j < v; j++) {
      graph[i][j] = -1;
    }
  }
  for (int i = 0; i < e; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    graph[s][t] = w;
  }
  priority_queue<pair<int, int> > qu;
  vector<bool> ischecked(v + 1, false);
  int ans = 0;
  qu.push(make_pair(0, r));
  while (!qu.empty()) {
    int noww = -(qu.top()).first;
    int nownode = (qu.top()).second;
    qu.pop();
    if (ischecked[nownode]) continue;
    ischecked[nownode] = true;
    ans += noww;
    for (int i = 0; i < v; i++) {
      if (!ischecked[i] && graph[nownode][i] >= 0) {
        qu.push(make_pair(-graph[nownode][i], i));
      }
    }
  }
  cout << ans << endl;
}
