#include <bits/stdc++.h>
using namespace std;
const int MAX_V = 105;
const int INF = 1e9;
int V, E, r;
int prim(int v[MAX_V][MAX_V]) {
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      open;
  open.push(pair<int, int>(0, r));
  vector<int> closed(V, INF);
  int min_cost = 0;
  while (!open.empty()) {
    pair<int, int> now = open.top();
    open.pop();
    if (closed[now.second] != INF) continue;
    closed[now.second] = now.first;
    min_cost += now.first;
    for (int i = 0; i < (int)(V); ++i) {
      if (closed[i] == INF && v[now.second][i] != INF)
        open.push(pair<int, int>(v[now.second][i], i));
    }
  }
  return min_cost;
}
int main() {
  cin >> V >> E >> r;
  int v[MAX_V][MAX_V];
  for (int i = 0; i < (int)(V); ++i)
    for (int j = 0; j < (int)(V); ++j) v[i][j] = INF;
  for (int i = 0; i < (int)(E); ++i) {
    int f, t, c;
    cin >> f >> t >> c;
    v[f][t] = c;
  }
  cout << prim(v) << endl;
  return 0;
}
