#include <bits/stdc++.h>
using namespace std;
int main() {
  int V, E, r;
  cin >> V >> E >> r;
  int cost[V][V];
  int mincost[V];
  bool used[V];
  for (int i = 0; i < V; i++)
    for (int j = 0; j < V; j++) cost[i][j] = 99999999;
  ;
  for (int i = 0; i < E; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    cost[s][t] = w;
  }
  for (int i = 0; i < V; i++) {
    mincost[i] = 99999999;
    ;
    used[i] = false;
  }
  mincost[r] = 0;
  int res = 0;
  while (true) {
    int v = -1;
    for (int i = 0; i < V; i++)
      if (!used[i] && (v == -1 || mincost[i] < mincost[v])) v = i;
    if (v == -1) break;
    used[v] = true;
    res += mincost[v];
    for (int i = 0; i < V; i++) {
      mincost[i] = min(mincost[i], cost[v][i]);
    }
  }
  cout << res << endl;
  return 0;
}
