#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, cost;
};
int V, E;
vector<edge> G[10000];
int mincost[10000];
bool used[10000];
int prim(int r) {
  int res = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      que;
  for (int i = 0; i < V; i++) {
    mincost[i] = 1000000000;
    used[i] = false;
  }
  mincost[r] = 0;
  used[r] = true;
  for (int i = 0; i < G[r].size(); i++) {
    que.push(pair<int, int>(G[r][i].cost, G[r][i].to));
  }
  while (!que.empty()) {
    pair<int, int> p = que.top();
    que.pop();
    int v = p.second;
    if (used[v] == true) continue;
    used[v] = true;
    res += p.first;
    for (int i = 0; i < G[v].size(); i++) {
      que.push(pair<int, int>(G[v][i].cost, G[v][i].to));
    }
  }
  return res;
}
int main() {
  int r;
  cin >> V >> E >> r;
  for (int i = 0; i < E; i++) {
    int s, t, c;
    cin >> s >> t >> c;
    edge e;
    e.to = t;
    e.cost = c;
    G[s].push_back(e);
  }
  cout << prim(r) << endl;
  return 0;
}
