#include <bits/stdc++.h>

using namespace std;


template< typename T >
struct edge
{
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
template< typename T >
using UnWeightedGraph = vector< vector< T > >;
template< typename T >
using Matrix = UnWeightedGraph< T >;

struct StronglyConnectedComponents
{
  vector< vector< int > > gg, rg;
  vector< pair< int, int > > edges;
  vector< int > comp, order, used;

  StronglyConnectedComponents(size_t v) : gg(v), rg(v), comp(v, -1), used(v, 0) {}

  void add_edge(int x, int y)
  {
    gg[x].push_back(y);
    rg[y].push_back(x);
    edges.emplace_back(x, y);
  }

  int operator[](int k)
  {
    return (comp[k]);
  }

  void dfs(int idx)
  {
    if(used[idx]) return;
    used[idx] = true;
    for(int to : gg[idx]) dfs(to);
    order.push_back(idx);
  }

  void rdfs(int idx, int cnt)
  {
    if(comp[idx] != -1) return;
    comp[idx] = cnt;
    for(int to : rg[idx]) rdfs(to, cnt);
  }

  void build(vector< vector< int > > &t)
  {
    for(int i = 0; i < gg.size(); i++) dfs(i);
    reverse(begin(order), end(order));
    int ptr = 0;
    for(int i : order) if(comp[i] == -1) rdfs(i, ptr), ptr++;

    t.resize(ptr);
    set< pair< int, int > > connect;
    for(auto &e : edges) {
      int x = comp[e.first], y = comp[e.second];
      if(x == y) continue;
      if(connect.count({x, y})) continue;
      t[x].push_back(y);
      connect.emplace(x, y);
    }
  }
};

template< typename T >
struct MinimumSpanningTreeArborescence
{
  WeightedGraph< T > g;
  T INF;

  MinimumSpanningTreeArborescence(const WeightedGraph< T > g) : INF(numeric_limits< T >::max()), g(g) {}

  T build(WeightedGraph< T > &g, int start, T sum)
  {
    int N = (int) g.size();

    vector< int > rev(N, -1);
    vector< T > weight(N, INF);
    for(int idx = 0; idx < N; idx++) {
      for(auto &e : g[idx]) {
        if(e.cost < weight[e.to]) {
          weight[e.to] = e.cost;
          rev[e.to] = idx;
        }
      }
    }

    StronglyConnectedComponents scc(N);
    for(int idx = 0; idx < N; idx++) {
      if(start == idx) continue;
      if(rev[idx] == -1) return (INF);
      scc.add_edge(rev[idx], idx);
      sum += weight[idx];
    }
    vector< vector< int > > renew;
    scc.build(renew);
    if(renew.size() == N) return (sum);
    WeightedGraph< T > fixgraph(renew.size());
    for(int i = 0; i < N; i++) {
      for(auto &e : g[i]) {
        if(scc[i] == scc[e.to]) continue;
        fixgraph[scc[i]].emplace_back(scc[e.to], e.cost - weight[e.to]);
      }
    }
    return (build(fixgraph, scc[start], sum));
  }

  T build(int start)
  {
    auto get = build(g, start, 0);
    if(get >= INF) return (-1);
    return (get);
  }
};

int main()
{
  int V, E, R;
  cin >> V >> E >> R;
  WeightedGraph< int > g(V);
  while(E--) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].emplace_back(b, c);
  }
  MinimumSpanningTreeArborescence< int > msta(g);
  cout << msta.build(R) << endl;;
}


Measured memory_bytes: 3944