#include <bits/stdc++.h>

using namespace std;

struct UnionFind
{
  vector< int > data;

  UnionFind(int sz)
  {
    data.assign(sz, -1);
  }

  bool unite(int x, int y)
  {
    x = find(x), y = find(y);
    if(x == y) return (false);
    if(data[x] > data[y]) swap(x, y);
    data[x] += data[y];
    data[y] = x;
    return (true);
  }

  int find(int k)
  {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k)
  {
    return (-data[find(k)]);
  }
};

template< typename T, typename E = T >
struct SkewHeap
{
  using G = function< T(T, E) >;
  using H = function< E(E, E) >;

  struct Node
  {
    T key;
    E lazy;
    Node *l, *r;
  } *root = nullptr;

  const bool rev;
  const G g;
  const H h;

  SkewHeap(bool rev = false) : g([](const T &a, const E &b) { return a + b; }),
                               h([](const E &a, const E &b) { return a + b; }), rev(rev) {}

  SkewHeap(const G &g, const H &h, bool rev = false) : g(g), h(h), rev(rev) {}

  Node *propagate(Node *t)
  {
    if(t->lazy != 0) {
      if(t->l) t->l->lazy = h(t->l->lazy, t->lazy);
      if(t->r) t->r->lazy = h(t->r->lazy, t->lazy);
      t->key = g(t->key, t->lazy);
      t->lazy = 0;
    }
    return t;
  }

  Node *merge(Node *x, Node *y)
  {
    if(!x || !y) return x ? x : y;
    propagate(x), propagate(y);
    if((x->key > y->key) ^ rev) swap(x, y);
    x->r = merge(y, x->r);
    swap(x->l, x->r);
    return x;
  }

  Node *push(const T &key)
  {
    auto t = new Node({key, 0, nullptr, nullptr});
    root = merge(root, t);
  }

  T top()
  {
    return propagate(root)->key;
  }

  void pop()
  {
    propagate(root);
    auto *temp = root;
    root = merge(root->l, root->r);
    delete temp;
  }

  bool empty() const
  {
    return !root;
  }

  void add(const E &lazy)
  {
    if(root) {
      root->lazy = h(root->lazy, lazy);
      propagate(root);
    }
  }

  void merge(SkewHeap< T, E > &p)
  {
    root = merge(root, p.root);
  }
};


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 > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename T >
struct MinimumSpanningTreeArborescence
{
  using Pi = pair< T, int >;
  using Heap = SkewHeap< Pi, int >;
  const Edges< T > &es;
  const int V;
  T INF;

  MinimumSpanningTreeArborescence(const Edges< T > &es, int V) :
      INF(numeric_limits< T >::max()), es(es), V(V) {}

  T build(int start)
  {
    auto g = [](const Pi &a, const T &b) { return Pi(a.first + b, a.second); };
    auto h = [](const T &a, const T &b) { return a + b; };
    vector< Heap > heaps(V, Heap(g, h));
    for(auto &e : es) heaps[e.to].push({e.cost, e.src});
    UnionFind uf(V);
    vector< int > used(V, -1);
    used[start] = start;

    T ret = 0;
    for(int s = 0; s < V; s++) {
      stack< int > path;
      for(int u = s; used[u] < 0;) {
        path.push(u);
        used[u] = s;
        if(heaps[u].empty()) return -1;
        auto p = heaps[u].top();
        ret += p.first;
        heaps[u].add(-p.first);
        heaps[u].pop();
        int v = uf.find(p.second);
        if(used[v] == s) {
          int w;
          Heap nextheap(g, h);
          do {
            w = path.top();
            path.pop();
            nextheap.merge(heaps[w]);
          } while(uf.unite(v, w));
          heaps[uf.find(v)].root = nextheap.root;
          used[uf.find(v)] = -1;
        }
        u = uf.find(v);
      }
    }
    return ret;
  }
};

const int INF = 1 << 29;

int main()
{
  int n, m, r;
  cin >> n >> m >> r;
  Edges< int > e;
  for(int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    e.emplace_back(x, y, z);
  }
  MinimumSpanningTreeArborescence< int > G(e, n);
  cout << G.build(r) << endl;
}


Measured memory_bytes: 3200