#include <bits/stdc++.h>
using namespace std;
template <typename T>
bool smax(T &a, const T &b) {
  if (a > b) return false;
  a = b;
  return true;
}
template <typename T>
bool smin(T &a, const T &b) {
  if (a < b) return false;
  a = b;
  return true;
}
template <typename T>
T cd(T a, T b) {
  return (a + b - 1) / b;
}
template <typename T>
bool se(T &a, T &b) {
  if (a < b) return false;
  swap(a, b);
  return true;
}
const int INF = 0x3f3f3f3f;
const int MAXN = 110;
const int MAXM = 1010;
int F[MAXN];
struct Edge {
  int u, v, w;
} edge[MAXM];
int tol;
void addedge(int u, int v, int w) {
  edge[tol].u = u;
  edge[tol].v = v;
  edge[tol++].w = w;
}
bool cmp(Edge a, Edge b) { return a.w < b.w; }
int find(int x) {
  if (F[x] == -1)
    return x;
  else
    return F[x] = find(F[x]);
}
int Kruskal(int n, int r) {
  memset(F, -1, sizeof(F));
  sort(edge, edge + tol, cmp);
  int cnt = 0;
  int ans = 0;
  for (int i = 0; i < tol; i++) {
    int u = edge[i].u;
    int v = edge[i].v;
    int w = edge[i].w;
    if (u == r) {
      int t1 = find(u);
      int t2 = find(v);
      if (t1 != t2) {
        ans += w;
        F[t1] = t2;
        cnt++;
      }
      if (cnt == n - 1) break;
      break;
    }
  }
  for (int i = 0; i < tol; i++) {
    int u = edge[i].u;
    int v = edge[i].v;
    int w = edge[i].w;
    int t1 = find(u);
    int t2 = find(v);
    if (t1 != t2) {
      ans += w;
      F[t1] = t2;
      cnt++;
    }
    if (cnt == n - 1) break;
  }
  if (cnt < n - 1)
    return -1;
  else
    return ans;
}
inline void build() {}
inline void solve() {
  int n, m, r;
  int u, v, w;
  cin >> n >> m >> r;
  for (int i = 0; i < m; i++) {
    cin >> u >> v >> w;
    addedge(u, v, w);
  }
  cout << Kruskal(n, r) << "\n";
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  build();
  solve();
  return 0;
}
