#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
const int INF = 100000000;
class UnionFind {
  vector<int> data;
  vector<int> rank;
  int root(int a) { return data[a] < 0 ? a : data[a] = root(data[a]); }

 public:
  UnionFind(int n) : data(n, -1), rank(n, 1) {}
  bool Find(int a, int b) { return root(a) == root(b); }
  void Union(int a, int b) {
    a = root(a);
    b = root(b);
    if (a == b) return;
    if (rank[a] < rank[b]) {
      data[a] += data[b];
      data[b] = a;
    } else {
      data[b] += data[a];
      data[a] = b;
      if (rank[a] == rank[b]) rank[b]++;
    }
  }
  int Size(int a) { return -data[root(a)]; }
  int Rank(int a) { return rank[root(a)]; }
};
int mini_arb(const vector<vector<P>>& G, int s) {
  int V = G.size();
  vector<vector<P>> rG(V);
  for (int i = 0; i < V; i++) {
    for (auto p : G[i]) {
      rG[p.first].push_back(P(i, p.second));
    }
  }
  int res = 0;
  UnionFind uf(V);
  for (int v = 0; v < V; v++) {
    while (!uf.Find(v, s)) {
      P p(-1, INF);
      for (int u = 0; u < V; u++) {
        if (uf.Find(v, u)) {
          for (auto e : rG[u]) {
            if (!uf.Find(v, e.first) && e.second < p.second) {
              p = e;
            }
          }
        }
      }
      uf.Union(v, p.first);
      res += p.second;
    }
  }
  return res;
}
int main() {
  int V, E, r;
  cin >> V >> E >> r;
  vector<vector<P>> G(V);
  for (int i = 0, s, t, w; i < E; i++) {
    cin >> s >> t >> w;
    G[s].push_back(P(t, w));
  }
  cout << mini_arb(G, r) << endl;
  return 0;
}
