#include <cstdio>
#include <cassert>
#include <vector>
#include <map>
#include <set>
#include <algorithm>

using namespace std;

struct Edge {
    int from, to, cost;
    Edge(){}
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost){}
    bool operator < (const Edge &rhs) const {
        return (cost < rhs.cost);
    }
    bool operator > (const Edge &rhs) const {
        return (rhs < *this);
    }
};

int directed_MST(vector<Edge> &G, int N, int r)
{
    vector<Edge> mini(N, Edge(-1, -1, 1000000));

    for (int i = 0; i < G.size(); i++){
        mini[G[i].to] = min(mini[G[i].to], G[i]);
    }

    mini[r] = Edge(-1, -1, -1);

    vector<int> gr(N, 0);
    vector<bool> isCycle(N, false);
    vector<bool> visited(N, false);
    int ct = 0;

    for (int i = 0; i < N; i++){
        if (visited[i]) continue;

        vector<int> chain;
        int v = i;
        while (v != -1 && !visited[v]){
            visited[v] = true;
            chain.push_back(v);
            v = mini[v].from;
        }

        if (v != -1){
            bool inCycle = false;
            for (int j = 0; j < chain.size(); j++){
                gr[chain[j]] = ct;
                if (chain[j] == v){
                    isCycle[ct] = true;
                    inCycle = true;
                }
                if (!inCycle) ct++;
            }
            ct += inCycle;
        }
        else {
            for (int j = 0; j < chain.size(); j++){
                gr[chain[j]] = ct++;
            }
        }
    }

    if (ct == N){
        int ans = 0;
        for (int i = 0; i < N; i++){
            if (i != r) ans += mini[i].cost;
        }
        return (ans);
    }

    int ans = 0;

    for (int i = 0; i < N; i++){
        if (i != r && isCycle[gr[i]]) ans += mini[i].cost;
    }

    vector<Edge> nG;
    for (int i = 0; i < G.size(); i++){
        int f2 = gr[G[i].from], t2 = gr[G[i].to];
        if (f2 == t2) continue;
        else if (isCycle[t2]){
            nG.push_back(Edge(f2, t2, G[i].cost - mini[G[i].to].cost));
        }
        else {
            nG.push_back(Edge(f2, t2, G[i].cost));
        }
    }

    return (ans + directed_MST(nG, ct, gr[r]));
}

int main()
{
    int n, m, s;
    set< pair<int, int> > dup;
    vector<Edge> G;

    scanf("%d %d %d", &n, &m, &s);

    for (int i = 0; i < m; i++){
        int x, y, z;
        scanf("%d %d %d", &x, &y, &z);
        G.push_back(Edge(x, y, z));
    }

    printf("%d\n", directed_MST(G, n, s));

    return (0);
}

