#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const long long int INF = 2147483647;
const long long int ZERO = 0;
struct Edge {
  int src, dst;
  long long int weight;
  int id;
  Edge(int src_, int dst_, long long int weight_, const int id_)
      : src(src_), dst(dst_), weight(weight_), id(id_) {}
  Edge(int src, int dst, long long int weight)
      : src(src), dst(dst), weight(weight) {}
  Edge() : src(0), dst(0), weight(0) {}
};
namespace getcycle {
vector<Edge> get_cycle(const vector<vector<Edge> >& g, const int start) {
  vector<Edge> prev_es(g.size());
  for (auto es : g) {
    for (auto e : es) {
      prev_es[e.dst] = e;
    }
  }
  for (int i = 0; i < int(g.size()); ++i) {
    if (i == start) continue;
    int now = i;
    vector<Edge> cycles;
    vector<int> comes(g.size(), -1);
    comes[now] = 0;
    int num = 1;
    while (1) {
      cycles.emplace_back(prev_es[now]);
      now = prev_es[now].src;
      if (comes[now] != -1) {
        cycles = vector<Edge>(cycles.begin() + comes[now], cycles.end());
        reverse(cycles.begin(), cycles.end());
        return cycles;
      } else if (now == start) {
        break;
      }
      comes[now] = num++;
    }
  }
  return vector<Edge>(0);
}
}  // namespace getcycle
pair<long long int, vector<vector<Edge> > > chu_liu(
    const vector<vector<Edge> >& g, const int start, const int e_size) {
  vector<Edge> cycle_edges;
  long long int total_len = 0;
  {
    vector<vector<Edge> > tree(g.size());
    {
      vector<vector<Edge> > revg(g.size());
      for (auto es : g) {
        for (auto e : es) {
          revg[e.dst].emplace_back(e.dst, e.src, e.weight);
        }
      }
      for (int node = 0; node < int(g.size()); ++node) {
        if (node == start)
          continue;
        else {
          long long int amin = INF;
          Edge min_edge;
          for (auto reve : revg[node]) {
            if (amin > reve.weight) {
              amin = reve.weight;
              min_edge = Edge(reve.dst, reve.src, reve.weight);
            }
          }
          if (amin < INF) {
            total_len += min_edge.weight;
            tree[min_edge.src].emplace_back(min_edge);
          }
        }
      }
    }
    cycle_edges = getcycle::get_cycle(tree, start);
    if (cycle_edges.empty()) {
      return make_pair(total_len, tree);
    }
  }
  vector<bool> node_is_cycle(g.size());
  vector<int> edge_is_cycle(e_size);
  vector<long long int> prev_lens(g.size(), ZERO);
  for (auto ce : cycle_edges) {
    node_is_cycle[ce.src] = true;
    prev_lens[ce.dst] = ce.weight;
  }
  vector<vector<Edge> > newg(g.size());
  const int compress = cycle_edges[0].src;
  vector<Edge> memo(e_size);
  for (int s = 0; s < int(g.size()); ++s) {
    bool src_is_cycle = node_is_cycle[s];
    for (auto e : g[s]) {
      memo[e.id] = e;
      bool dst_is_cycle = node_is_cycle[e.dst];
      if (src_is_cycle && dst_is_cycle) {
        continue;
      } else if (src_is_cycle) {
        e.src = compress;
      } else if (dst_is_cycle) {
        e.weight -= prev_lens[e.dst];
        e.dst = compress;
      }
      newg[e.src].push_back(e);
    }
  }
  auto p = chu_liu(newg, start, e_size);
  vector<vector<Edge> > compress_tree(p.second);
  vector<vector<Edge> > ans_tree(g.size());
  for (auto es : compress_tree) {
    for (auto e : es) {
      const Edge prev_edge(memo[e.id]);
      ans_tree[prev_edge.src].emplace_back(prev_edge);
    }
  }
  long long int cycle_len = 0;
  for (auto e : cycle_edges) {
    cycle_len += e.weight;
  }
  return make_pair(p.first + cycle_len, ans_tree);
}
int main() {
  int V, E, R;
  cin >> V >> E >> R;
  vector<vector<Edge> > g(V);
  for (int i = 0; i < E; ++i) {
    int s, t, w;
    cin >> s >> t >> w;
    g[s].push_back(Edge(s, t, w));
  }
  auto p = chu_liu(g, R, E);
  cout << p.first << endl;
  return 0;
}
