#include <bits/stdc++.h>
using namespace std;
struct E {
  E(int t, int c) : t(t), c(c) {}
  int t, c;
};
int n, m, r;
vector<E> g[100];
bool used[100];
int main() {
  scanf("%d%d%d", &n, &m, &r);
  for (int i = 0; i < (m); ++i) {
    int f, t, c;
    scanf("%d%d%d", &f, &t, &c);
    g[f].push_back(E(t, c));
  }
  int ans = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      q;
  q.push(pair<int, int>(0, r));
  while (!q.empty()) {
    pair<int, int> v = q.top();
    q.pop();
    if (used[v.second]) {
      continue;
    }
    used[v.second] = true;
    ans += v.first;
    for (int i = 0; i < (g[v.second].size()); ++i) {
      E e = g[v.second][i];
      q.push(pair<int, int>(e.c, e.t));
    }
  }
  printf("%d\n", ans);
  return 0;
}
