#include <bits/stdc++.h>
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using namespace std;
int INF = 10000000;
int V, E;
int r;
vector<vector<int> > cost;
long long prim() {
  vector<int> mincost(V, INF);
  vector<bool> used(V, false);
  mincost[r] = 0;
  long long res = 0;
  while (true) {
    int v = -1;
    for (int u = 0; u < V; u++) {
      if (!used[u] && (v == -1 || mincost[u] < mincost[v])) {
        v = u;
      }
    }
    if (v == -1) break;
    used[v] = true;
    res += mincost[v];
    for (int u = 0; u < V; u++) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }
  return res;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> V >> E >> r;
  cost.resize(V, vector<int>(V, INF));
  for (int i = 0; i < (E); i++) {
    int s, t;
    cin >> s >> t;
    int c;
    cin >> c;
    cost[s][t] = c;
  }
  cout << prim() << endl;
  return 0;
}
