#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, cost;
};
vector<edge> G[10000];
bool used[10000];
int V, E;
int d[10000];
int prim(int f) {
  for (int i = 0; i < V; i++) {
    d[i] = 1e9;
    used[i] = false;
  }
  d[f] = 0;
  used[f] = true;
  for (int i = 0; i < G[f].size(); i++) d[G[f][i].to] = G[f][i].cost;
  int num = 0;
  for (int j = 0; j < V; j++) {
    int m = -1;
    for (int i = 0; i < V; i++) {
      if (used[i] == false && (m == -1 || d[m] > d[i])) m = i;
    }
    if (m == -1) break;
    used[m] = true;
    num += d[m];
    for (int i = 0; i < G[m].size(); i++)
      d[G[m][i].to] = min(d[G[m][i].to], G[m][i].cost);
  }
  return num;
}
int main() {
  int first;
  cin >> V >> E >> first;
  for (int i = 0; i < E; i++) {
    int f, t;
    edge e;
    cin >> f >> e.to >> e.cost;
    G[f].push_back(e);
  }
  int fl = prim(first);
  cout << fl << endl;
}
