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 <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;

struct StronglyConnectedComponents {
    int id = 0;
    vector<vector<int>> gg, rg;
    vector<int> comp, order, seen, depth;
    StronglyConnectedComponents(int n) : \
        gg(n), rg(n), comp(n, -1), seen(n, 0), depth(n, 1) { }
    void add_edge(int x, int y) {
        gg[x].emplace_back(y);
        rg[y].emplace_back(x);
    }
    void dfs(int u) {               // order, seen
        seen[u] = true;
        for (int v: gg[u]) if (!seen[v]) dfs(v);
        order.emplace_back(u);
    }
    void rdfs(int u, int _id) {     // comp, depth
        comp[u] = _id;
        for (int v: rg[u]) {
            if (comp[v] == -1) rdfs(v, _id);
            depth[u] = max(depth[u], depth[v] + 1);
        }
    }
    int build() {
        for (int u = 0; u < (int)gg.size(); ++u) if (!seen[u]) dfs(u);
        reverse(begin(order), end(order));
        for (int u: order) if (comp[u] == -1) rdfs(u, id++);
        return id;                  // id < n -> has_cycle
    }
    vector<vector<int>> make_graph() {
        vector<vector<int>> g_comp(id);
        set<pair<int, int>> connect;
        for (int u = 0; u < (int)gg.size(); ++u) for (int v: gg[u]) {
            int x = comp[u], y = comp[v];
            if (x == y) continue;
            if (connect.count({x, y})) continue;
            g_comp[x].emplace_back(y);
            connect.emplace(x, y);
        }
        return g_comp;
    }
    int operator[](int k) { return comp[k]; }
};

template<typename T>
struct Arborescence {
    const T INF = numeric_limits<T>::max();
    using Graph = vector<vector<pair<int, T>>>;
    Graph g;
    Arborescence(int n) : g(n) { };
    void add_edge(int u, int v, T w) { g[u].emplace_back(v, w); }
    T build(Graph &g, int r, T sum) {
        int n = g.size();
        vector<int> par(n, -1);
        vector<T> w_in(n, INF);
        for (int u = 0; u < n; u++) for (auto &e: g[u]) {
            if (w_in[e.first] > e.second) {
                w_in[e.first] = e.second, par[e.first] = u;
            }
        }
        StronglyConnectedComponents scc(n);
        for (int u = 0; u < n; u++) if (r != u) {
            if (par[u] == -1) return INF;
            scc.add_edge(par[u], u), sum += w_in[u];
        }
        int sz = scc.build();
        if (sz == n) return sum;    // no cycle
        Graph g_comp(sz);
        for (int u = 0; u < n; u++) for (auto &e: g[u]) {
            if (scc[u] == scc[e.first]) continue;
            g_comp[scc[u]].emplace_back(scc[e.first], e.second - w_in[e.first]);
        }
        return build(g_comp, scc[r], sum);
    }
    T build(int r) {
        T res = build(g, r, 0);
        return res == INF ? -1 : res;
    }
};

int main() {
    int n, m; cin >> n >> m;
    int r; cin >> r;
    Arborescence<long long> mst(n);
    while (m--) {
        int u, v, w; cin >> u >> v >> w;
        mst.add_edge(u, v, w);
    }
    cout << mst.build(r) << endl;
    return 0;
}

