#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back

typedef long long LL;
typedef long double LD;

const LL INF = (1LL << 60);

struct Edge {
  int u, v; LL w;

  Edge(int u, int v, LL w) : u(u), v(v), w(w) {}

  // order edges by weight
  bool operator <(const Edge& x) const {
      return w < x.w;
  }
};

LL edmonds(vector<Edge>& edgeList, int n, int source);

int main() {

  ios_base::sync_with_stdio(0); cin.tie(0);

  vector<Edge> edgeList;

  int n, m, source;
  cin >> n >> m >> source;

  for (int i = 0; i < m; i++) {
    int u, v; LL w;
    cin >> u >> v >> w;

    edgeList.push_back(Edge(u, v, w));
  }

  LL result = edmonds(edgeList, n, source);

  cout << result << endl;

  return 0;
}

LL edmonds(vector<Edge>& edgeList, int n, int source) {
     
  // determine min cost of edge entering each vertex
  vector<Edge> minInEdge(n, Edge(-1, -1, INF));
  for (Edge e : edgeList) {
    minInEdge[e.v] = min(minInEdge[e.v], e);
  } minInEdge[source] = Edge(-1, source, 0);

  // assign vertices to their cyclic group
  vector<int> group(n, 0);
  vector<bool> visited(n, false), isCycleGroup(n, false); int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (visited[i])
      continue;

    int node = i; vector<int> path;
    while (node != -1 && !visited[node]) {
      visited[node] = true;
      path.push_back(node);
      node = minInEdge[node].u;
    }

    bool isCycle = false;
    for (int v : path) {
      group[v] = cnt;
      if (v == node)
        isCycleGroup[cnt] = isCycle = true;
      if (!isCycle)
        cnt++;
    }

    if (isCycle)
      cnt++;
  }

  // when there are no cycles
  if (cnt == n) {
    int result = 0;
    for (Edge e : minInEdge)
      result += e.w;
    return result;
  }

  LL result = 0;
  for (Edge e : minInEdge)
    if (isCycleGroup[group[e.v]])
      result += e.w;

  // form new graph with groups
  vector<Edge> n_edgeList;
  for (Edge e : edgeList) {
    int u = group[e.u], v = group[e.v], w = e.w;
    if (u == v)
      continue;
    else
      n_edgeList.push_back(Edge(u, v, w - (isCycleGroup[v] ? minInEdge[e.v].w : 0)));
  }

  return result + edmonds(n_edgeList, cnt, group[source]);
}
