Find the sum of the weights of edges of the Minimum-Cost Arborescence with the root r for a given weighted directed graph G = (V, E).

Constraints

* 1 ≤ |V| ≤ 100
* 0 ≤ |E| ≤ 1,000
* 0 ≤ wi ≤ 10,000
* G has arborescence(s) with the root r

Input


|V| |E| r
s0 t0 w0
s1 t1 w1
:
s|E|-1 t|E|-1 w|E|-1


, where |V| is the number of vertices and |E| is the number of edges in the graph. The graph vertices are named with the numbers 0, 1,..., |V|-1 respectively. r is the root of the Minimum-Cost Arborescence.

si and ti represent source and target verticess of i-th directed edge. wi represents the weight of the i-th directed edge.

Output

Print the sum of the weights the Minimum-Cost Arborescence.

Examples

Input

4 6 0
0 1 3
0 2 2
2 0 1
2 3 1
3 0 1
3 1 5


Output

6


Input

6 10 0
0 2 7
0 1 1
0 3 5
1 4 9
2 1 6
1 3 2
3 4 3
4 2 2
2 5 8
3 5 3


Output

11
#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]);
}
