#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void priv(vector<T> a) {
  for (int i = 0; i < a.size(); i++) {
    cerr << a[i] << ((i == a.size() - 1) ? "\n" : " ");
  }
}
long long gcd(long long a, long long b) {
  int c = max(a, b);
  int d = min(a, b);
  return c == 0 || d == 0 ? c : gcd(c % d, d);
}
long long lcm(long long a, long long b) {
  return a == 0 || b == 0 ? 0 : a * b / gcd(a, b);
}
long long fact(long long a) {
  long long b = 1;
  for (int i = 1; i <= a; i++) b *= i;
  return b;
}
long long power(long long x, long long n) {
  long long a = 1;
  for (int i = 0; i < n; i++) a *= x;
  return a;
}
template <typename T>
struct Edge {
  int src, dst;
  T w;
  Edge(){};
  Edge(int src, int dst) : src(src), dst(dst) { w = 1; }
  Edge(int src, int dst, T w) : src(src), dst(dst), w(w) {}
  bool operator<(const Edge &e) const {
    return w != e.w ? w > e.w : dst < e.dst;
  }
  bool operator==(const Edge &e) { return dst == e.dst; }
};
template <typename T>
class Graph : public vector<vector<Edge<T>>> {
 private:
  pair<T, int> farthest(int p, int v) {
    pair<T, int> r(0, v);
    for (auto e : (*this)[v])
      if (e.dst != p) {
        auto t = farthest(v, e.dst);
        t.first += e.w;
        if (t.first > r.first) r = t;
      }
    return r;
  }
  void visit(Graph &h, int v, int s, int r, vector<int> &no,
             vector<vector<int>> &cmp, vector<int> &prev,
             vector<vector<int>> &next, vector<T> &mcost, vector<int> &mark,
             T &cost, bool &found) {
    const int n = h.size();
    if (mark[v]) {
      vector<int> tmp = no;
      found = true;
      do {
        cost += mcost[v];
        v = prev[v];
        if (v != s) {
          while (cmp[v].size() > 0) {
            no[cmp[v].back()] = s;
            cmp[s].push_back(cmp[v].back());
            cmp[v].pop_back();
          }
        }
      } while (v != s);
      for (auto j : cmp[s])
        if (j != r)
          for (auto e : h[j])
            if (no[e.src] != s) e.w -= mcost[tmp[j]];
    }
    mark[v] = true;
    for (auto i : next[v])
      if (no[i] != no[v] && prev[no[i]] == v)
        if (!mark[no[i]] || i == s)
          visit(h, i, s, r, no, cmp, prev, next, mcost, mark, cost, found);
  }

 public:
  T inf;
  Graph(int n) {
    (*this).resize(n);
    inf = numeric_limits<T>::max();
  }
  void direct(int s, int t) { (*this)[s].push_back(Edge<T>(s, t)); }
  void direct(int s, int t, T w) { (*this)[s].push_back(Edge<T>(s, t, w)); }
  void undirect(int s, int t) {
    direct(s, t);
    direct(t, s);
  }
  void undirect(int s, int t, T w) {
    direct(s, t, w);
    direct(t, s, w);
  }
  T diameter(void) {
    auto r = farthest(-1, 0);
    auto t = farthest(-1, r.second);
    return t.first;
  }
  vector<T> dijkstra(int s = 0) {
    int n = (*this).size();
    priority_queue<Edge<T>> PQ;
    vector<T> dist(n, inf);
    dist[s] = 0;
    Edge<T> e(-1, s, 0);
    PQ.push(e);
    while (!PQ.empty()) {
      auto f = PQ.top();
      PQ.pop();
      int u = f.dst;
      if (dist[u] < f.w * (-1)) continue;
      for (int j = 0; j < (*this)[u].size(); j++) {
        int v = (*this)[u][j].dst;
        if (dist[v] > dist[u] + (*this)[u][j].w) {
          dist[v] = dist[u] + (*this)[u][j].w;
          Edge<T> e(-1, v, dist[v] * (-1));
          PQ.push(e);
        }
      }
    }
    return dist;
  }
  pair<bool, vector<T>> bellmanFord(int s = 0) {
    int n = (*this).size();
    vector<T> dist(n, inf);
    dist[s] = 0;
    for (int i = 0; i < n; i++) {
      bool update = false;
      for (int v = 0; v < n; v++)
        for (auto e : (*this)[v]) {
          if (dist[v] != inf && dist[e.dst] > dist[v] + e.w) {
            dist[e.dst] = dist[v] + e.w;
            update = true;
            if (i == n - 1) return make_pair(true, dist);
          }
        }
      if (!update) break;
    }
    return make_pair(false, dist);
  }
  pair<T, vector<Edge<T>>> MST(int r = 0) {
    int n = (*this).size();
    vector<Edge<T>> U;
    T total = 0;
    vector<bool> visited(n, false);
    priority_queue<Edge<T>> Q;
    Q.push(Edge<T>(-1, r, 0));
    while (!Q.empty()) {
      auto e = Q.top();
      Q.pop();
      if (visited[e.dst]) continue;
      U.push_back(e);
      total += e.w;
      visited[e.dst] = true;
      for (auto f : (*this)[e.dst])
        if (!visited[f.dst]) Q.push(f);
    }
    return make_pair(total, U);
  }
  T MSA(int r = 0) {
    const int n = (*this).size();
    Graph h(n);
    for (int u = 0; u < n; u++)
      for (auto e : (*this)[u]) h[e.dst].push_back(e);
    vector<int> no(n);
    vector<vector<int>> cmp(n);
    for (int u = 0; u < n; u++) cmp[u].push_back(no[u] = u);
    for (T cost = 0;;) {
      vector<int> prev(n, -1);
      vector<T> mcost(n, inf);
      for (int j = 0; j < n; j++)
        if (j != r)
          for (auto e : h[j])
            if (no[e.src] != no[j])
              if (e.w < mcost[no[j]])
                mcost[no[j]] = e.w, prev[no[j]] = no[e.src];
      vector<vector<int>> next(n);
      for (int u = 0; u < n; u++)
        if (prev[u] >= 0) next[prev[u]].push_back(u);
      bool stop = true;
      vector<int> mark(n);
      for (int u = 0; u < n; u++)
        if (u != r && !mark[u] && !cmp[u].empty()) {
          bool found = false;
          visit(h, u, u, r, no, cmp, prev, next, mcost, mark, cost, found);
          if (found) stop = false;
        }
      if (stop) {
        for (int u = 0; u < n; u++)
          if (prev[u] >= 0) cost += mcost[u];
        return cost;
      }
    }
  }
};
int main() {
  int v, e, r;
  cin >> v >> e >> r;
  Graph<long long> g(v);
  for (int i = 0; i < e; i++) {
    long long s, t, w;
    cin >> s >> t >> w;
    g.direct(s, t, w);
  }
  cout << g.MSA(r) << endl;
  return 0;
}
