#include <bits/stdc++.h>
using namespace std;
struct Node {
  int b, d;
  Node(int b = 0, int d = 0) : b(b), d(d) {}
};
bool operator<(const Node& n1, const Node& n2) { return n1.d > n2.d; }
vector<Node> edge[105];
bool visit[105] = {false};
int main() {
  cin.tie(0);
  cin.sync_with_stdio(0);
  int V, E, r;
  cin >> V >> E >> r;
  int s, t, w;
  for (int i = 0; i < E; ++i) {
    cin >> s >> t >> w;
    edge[s].push_back(Node(t, w));
  }
  priority_queue<Node> PQ;
  int ans = 0;
  PQ.push(Node(r, 0));
  while (!PQ.empty()) {
    auto now = PQ.top();
    PQ.pop();
    if (visit[now.b]) continue;
    ans += now.d;
    visit[now.b] = true;
    for (auto e : edge[now.b]) {
      if (!visit[e.b]) PQ.push(Node(e.b, e.d));
    }
  }
  cout << ans << endl;
}
