#include <bits/stdc++.h>
using namespace std;
long long v, e, r;
vector<vector<long long> > cost;
const long long INF = 1000000000;
long long x, y, c, w;
vector<long long> ans;
vector<bool> vis;
int main() {
  cin >> v >> e >> r;
  cost = vector<vector<long long> >(v, vector<long long>(v, INF));
  for (long long i = 0; i < e; i++) {
    cin >> x >> y >> c;
    cost[x][y] = c;
  }
  ans = vector<long long>(v, INF);
  vis = vector<bool>(v);
  ans[r] = 0;
  while (true) {
    long long k = -1;
    for (long long i = 0; i < v; i++) {
      if (!vis[i] && (k == -1 || ans[i] < ans[k])) k = i;
    }
    if (k == -1) break;
    vis[k] = true;
    for (long long i = 0; i < v; i++) {
      ans[i] = min(ans[i], cost[k][i]);
    }
  }
  cout << accumulate(ans.begin(), ans.end(), 0) << endl;
  return 0;
}
