#include <bits/stdc++.h>
using namespace std;
int prim(int n, vector<pair<int, int> >* vs, int s = 0) {
  vector<int> gone(n, 0);
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      que;
  gone[s] = 1;
  for (int i = 0; i < (vs[s].size()); i++) {
    int to = vs[s][i].first, co = vs[s][i].second;
    que.push(pair<int, int>(co, to));
  }
  int res = 0;
  while (!que.empty()) {
    pair<int, int> pa = que.top();
    que.pop();
    int no = pa.second, co = pa.first;
    if (gone[no] == 0) {
      gone[no] = 1;
      res += co;
      for (int i = 0; i < (vs[no].size()); i++) {
        int to = vs[no][i].first, tc = vs[no][i].second;
        if (gone[to] == 1) continue;
        que.push(pair<int, int>(tc, to));
      }
    }
  }
  return res;
}
int n, m;
vector<pair<int, int> > vs[10005];
int main(void) {
  int n, m, s;
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 0; i < (m); i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    vs[a].push_back(pair<int, int>(b, c));
  }
  printf("%d\n", prim(n, vs, s));
  return 0;
}
