#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(int i=0;i<minInEdge.size();i++)
        {
            Edge &e = minInEdge[i];
            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;
}

