#include <bits/stdc++.h>
using namespace std;
const int MX = 1e9 + 7;
int a[11000];
int search(int c) {
  if (a[c] == c) {
    return c;
  }
  a[c] = search(a[c]);
  return a[c];
}
int main() {
  int v, e, r;
  int W = 0;
  bool used[1100] = {};
  for (int i = 0; i < 11000; i++) {
    a[i] = i;
  }
  vector<pair<int, pair<int, int>>> E;
  cin >> v >> e >> r;
  for (int i = 0; i < e; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    E.push_back({w, {s, t}});
  }
  sort(E.begin(), E.end());
  for (int i = 0; i < v; i++) {
    for (int t = 0; t < e; t++) {
      if (a[E[t].second.first] == r && a[E[t].second.second] != r) {
        a[E[t].second.second] = r;
        W += E[t].first;
        used[t] = true;
        break;
      }
    }
  }
  for (int i = e - 1; i >= 0; i--) {
    if (!used[i]) {
      continue;
    }
    for (int t = 0; t < i; t++) {
      if (E[t].second.second == E[i].second.second && !used[t]) {
        used[i] = false;
        used[t] = true;
        W -= E[i].first;
        W += E[t].first;
      }
    }
  }
  cout << W << endl;
  return 0;
}
