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;

const int inf = 1e9+10;

struct Edge {
    int from,to,weight;

    Edge(int f,int t,int w)
    {
        from=f;to=t;weight=w;
    }

    bool operator < (const Edge &x) const
    {
        return weight < x.weight;
    }
};

int edmonds(vector<Edge> &G, int V, int R)
{
    vector<Edge> minInEdge(V,Edge(-1, -1, inf));
    for (int i=0;i<G.size();i++)
    {
        const Edge &e = G[i];
        minInEdge[e.to] = min(minInEdge[e.to], e);
    }
    minInEdge[R] = Edge(-1, R, 0);

    vector<int> group(V, 0);
    vector<bool> visited(V, false), isCycleGroup(V, false);
    int cnt = 0;
    for (int i = 0; i < V; 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].from;
        }

        bool isCycle = false;
        for (int i=0;i<path.size();i++)
        {
            int v = path[i];
            group[v] = cnt;
            if (v == node)
                isCycleGroup[cnt] = isCycle = true;
            if (!isCycle)
                cnt++;
        }

        if (isCycle)
            cnt++;
    }

    if (cnt == V)
    {
        int answer = 0;
        for (Edge e : minInEdge)
            answer += e.weight;
        return answer;
    }

    int answer = 0;
    for (int i=0;i<minInEdge.size();i++)
    {
        Edge &e = minInEdge[i];
        if (isCycleGroup[group[e.to]])
            answer += e.weight;
    }


    vector<Edge> n_G;
    for (int i=0;i<G.size();i++)
    {
        const Edge &e = G[i];
        int u = group[e.from], v = group[e.to], w = e.weight;
        if (u == v)
            continue;
        else
            n_G.push_back(Edge(u, v, w - (isCycleGroup[v] ? minInEdge[e.to].weight : 0)));
    }

    return answer + edmonds(n_G, cnt, group[R]);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int v,e,r;
    cin>>v>>e>>r;

    vector<Edge> G;
    for(int i=0;i<e;i++)
    {
        int s,t,w;
        cin>>s>>t>>w;
        G.push_back(Edge(s,t,w));
    }

    cout<<edmonds(G,v,r)<<endl;

    return 0;
}

