#include <bits/stdc++.h>
using namespace std;
class Edge {
 public:
  Edge(){};
  Edge(int s, int t, int w) : s{s}, t{t}, w{w} {};
  int s;
  int t;
  int w;
};
bool isConnected(vector<vector<int>> g, int st) {
  int n = g.size();
  auto used = vector<bool>(n, false);
  int cnt = 0;
  auto xs = stack<int>();
  xs.push(st);
  while (xs.size() > 0 && cnt < n) {
    auto x = xs.top();
    xs.pop();
    if (used[x]) {
      continue;
    }
    used[x] = true;
    ++cnt;
    for (int i = 0; i < n; ++i) {
      if (g[x][i] != 2000000000 && !used[i]) {
        xs.push(i);
      }
    }
  }
  return cnt == n;
}
bool hasCycle(int st, vector<vector<int>> g) {
  int n = g.size();
  auto used = vector<bool>(n, false);
  for (int i = 0; i < n; ++i) {
    if (used[i]) {
      continue;
    }
    auto xs = stack<int>();
    xs.push(i);
    while (xs.size()) {
      auto v = xs.top();
      used[v] = true;
      xs.pop();
      for (int j = 0; j < n; ++j) {
        if (j != v && g[v][j] != 2000000000) {
          if (used[j]) {
            return true;
          } else {
            xs.push(j);
          }
        }
      }
    }
  }
  return false;
}
int kruskal(int v, vector<Edge> es, int st) {
  sort(es.begin(), es.end(), [](Edge a, Edge b) { return a.w > b.w; });
  auto g = vector<vector<int>>();
  for (int i = 0; i < v; ++i) {
    g.push_back(vector<int>(v, 2000000000));
    g[i][i] = 0;
  }
  int sum = 0;
  for (auto it = es.begin(); it != es.end(); ++it) {
    g[it->s][it->t] = it->w;
    sum += it->w;
  }
  int n = es.size();
  for (auto it = es.begin(); it != es.end(); ++it) {
    Edge e = *it;
    g[e.s][e.t] = 2000000000;
    if (isConnected(g, st)) {
      sum -= e.w;
      --n;
      if (n == v - 1) {
        break;
      }
    } else {
      g[e.s][e.t] = e.w;
    }
  }
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  int v, e, r;
  cin >> v >> e >> r;
  auto es = vector<Edge>();
  int s, t, w;
  for (int i = 0; i < e; ++i) {
    cin >> s >> t >> w;
    es.push_back(Edge(s, t, w));
  }
  cout << kruskal(v, es, r) << endl;
}
