#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int V, E, r;
  int s, t, w;
  int ed[1000][1000];
  int min, mx, my;
  int cost;
  bool use[1000];
  cin >> V >> E >> r;
  for (int i = 0; i < V; i++) use[i] = false;
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      ed[i][j] = -1000;
    }
  }
  for (int i = 0; i < E; i++) {
    cin >> s >> t >> w;
    ed[s][t] = w;
  }
  use[r] = true;
  cost = 0;
  while (1) {
    min = -1000;
    for (int i = 0; i < V; i++) {
      if (use[i] == true) {
        for (int j = 0; j < V; j++) {
          if (use[j] == false) {
            if (ed[i][j] != -1000) {
              if (min == -1000 || min > ed[i][j])
                min = ed[i][j], mx = j, my = i;
            }
          }
        }
      }
    }
    if (min == -1000) break;
    use[my] = true, use[mx] = true;
    cost += ed[my][mx];
  }
  cout << cost << endl;
}
