#include <bits/stdc++.h>
using namespace std;
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
using Weight = int;
using Flow = int;
struct Edge {
  int s, d;
  Weight w;
  Flow c;
  Edge(){};
  Edge(int s, int d, Weight w = 1) : s(s), d(d), w(w), c(w){};
};
bool operator<(const Edge &e1, const Edge &e2) { return e1.w < e2.w; }
bool operator>(const Edge &e1, const Edge &e2) { return e2 < e1; }
inline ostream &operator<<(ostream &os, const Edge &e) {
  return (os << '(' << e.s << ", " << e.d << ", " << e.w << ')');
}
using Edges = vector<Edge>;
using Graph = vector<Edges>;
using Array = vector<Weight>;
using Matrix = vector<Array>;
void add_arc(Graph &g, int s, int d, Weight w = 1) {
  g[s].emplace_back(s, d, w);
}
void add_edge(Graph &g, int a, int b, Weight w = 1) {
  add_arc(g, a, b, w);
  add_arc(g, b, a, w);
}
Graph reverse(const Graph &g) {
  Graph rg(g.size());
  for (auto &es : g)
    for (auto &e : es) rg[e.d].emplace_back(e.d, e.s, e.w);
  return rg;
}
vector<int> kosaraju(const Graph &g) {
  int n = g.size();
  Graph rg = reverse(g);
  vector<int> cc(n, -1);
  vector<int> post;
  function<void(int, int)> dfs_cc = [&](int u, int cc_id) {
    if (cc[u] != -1) return;
    cc[u] = cc_id;
    for (auto &e : g[u]) dfs_cc(e.d, cc_id);
    post.emplace_back(u);
  };
  for (int u = 0; u < n; u++)
    if (cc[u] == -1) dfs_cc(u, u);
  vector<int> scc(n, -1);
  function<void(int, int, int)> dfs_scc = [&](int u, int scc_id, int cc_id) {
    if (scc[u] != -1) return;
    if (cc[u] != cc_id) return;
    scc[u] = scc_id;
    for (auto &e : rg[u]) dfs_scc(e.d, scc_id, cc_id);
  };
  reverse(post.begin(), post.end());
  for (auto &u : post)
    if (scc[u] == -1) dfs_scc(u, u, cc[u]);
  return scc;
}
bool detect_cycle(const vector<int> &idx) {
  int n = idx.size();
  vector<bool> f(n);
  for (auto &u : idx) {
    if (f[u]) return true;
    f[u] = true;
  }
  return false;
}
struct UnionFind {
  vector<int> parent;
  int size;
  UnionFind(int n) : parent(n, -1), size(n) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) return false;
    if (size_of(x) < size_of(y)) swap(x, y);
    parent[x] += parent[y];
    parent[y] = x;
    size--;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); }
  int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }
  int size_of(int x) { return -parent[root(x)]; }
};
Weight edmonds(const Graph &g, int root) {
  int n = g.size();
  Weight total = 0;
  Graph rg = reverse(g);
  Graph rmsa;
  while (true) {
    rmsa.assign(n, Edges());
    Edges minout(n);
    for (int u = (0); u < (n); u++) {
      if (u == root) continue;
      if (rg[u].size()) {
        minout[u] = *min_element(begin(rg[u]), end(rg[u]));
        rmsa[u].emplace_back(minout[u]);
      }
    }
    vector<int> idx = kosaraju(rmsa);
    ;
    if (!detect_cycle(idx)) break;
    Array cycle_cost(n);
    vector<int> cycle_count(n);
    for (int u = (0); u < (n); u++) {
      if (u == root) continue;
      if (rmsa[u].size()) {
        Edge &e = rmsa[u].front();
        cycle_cost[idx[e.d]] += e.w;
        cycle_count[idx[u]]++;
      }
    }
    for (int i = (0); i < (n); i++) {
      if (cycle_count[i] >= 2) {
        total += cycle_cost[i];
      }
    }
    for (int u = (0); u < (n); u++) {
      if (cycle_count[idx[u]] >= 2) {
        for (auto &e : rg[u]) {
          e.w -= minout[u].w;
        }
      }
    }
    rmsa.assign(n, Edges());
    for (auto &es : rg)
      for (auto &e : es)
        if (e.w != 0) rmsa[idx[e.s]].emplace_back(idx[e.s], idx[e.d], e.w);
    rg = rmsa;
    ;
    ;
  };
  for (auto &es : rmsa)
    for (auto &e : es) total += e.w;
  return total;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int V, E, r;
  cin >> V >> E >> r;
  Graph g(V);
  for (int i = (0); i < (E); i++) {
    int s, t, w;
    cin >> s >> t >> w;
    add_arc(g, s, t, w);
  }
  cout << edmonds(g, r) << endl;
  return 0;
}
