#include<bits/stdc++.h>

using namespace std;

constexpr int INF = 100000000;

using Weight=int;
struct Edge{
    int src,dst;
    Weight weight;
    Edge(int src,int dst,Weight weight):
    src(src),dst(dst),weight(weight){}
};

auto operator < (const Edge &e,const Edge &f){
    if(e.weight!=f.weight)return e.weight>f.weight;
    else if(e.src!=f.src)return e.src<f.src;
    else return e.dst<f.dst;
}

using Edges=vector<Edge>;
using Graph=vector<Edges>;

long long ChuLiu_Edmonds(const Graph &g,int root){
    const int V=g.size();
    Edges mins(V,Edge(-1,-1,INF));
    //????????????????????\??£?????????????°???????????????¶
    for(int i=0;i<V;++i){
        for(auto e:g[i]){
            if(e.weight < mins[e.dst].weight){
                mins[e.dst]=e;
            }
        }
        if(i==root){
            mins[i]=Edge(-1,-1,-1);
            continue;
        }
    }
    vector<int>group(V,0);
    vector<bool>isCycle(V,false);
    int count=0;
    vector<bool>used(V,false);
    //??°???????????????
    for(int i=0;i<V;++i){
        if(used[i])continue;
        vector<int>chain;
        int cursor=i;
        //??????????????£?????????
        while(cursor != -1 && !used[cursor]){
            used[cursor]=true;
            chain.push_back(cursor);
            cursor=mins[cursor].src;
        }
        //?????????????????§??????
        if(cursor != -1){
            bool inCycle=false;
            for(int j=0;j<chain.size();++j){
                group[chain[j]]=count;
                if(chain[j]==cursor){
                    isCycle[count]=true;
                    inCycle=true;
                }
                if(!inCycle)++count;
            }
            if(inCycle)++count;
        }else{//????????§??????
            for(int j=0;j<chain.size();++j){
                group[chain[j]]=count++;
            }
        }
    }
    //??°?????????????????????
    //?????????????????¨???
    if(count==V){
        long long ans=1;
        for(int i=0;i<V;++i){
            ans+=mins[i].weight;
        }
        return ans;
    }
    //????´?????????????????????????????¨?
    long long res=0;
    for(int i=0;i<V;++i){
        if(i!=root && isCycle[group[i]]){
            res+=mins[i].weight;
        }
    }
    //???????????????????????????????????°????????????????????????
    Graph ng(count);
    for(auto e:mins){
        int to=e.dst;
        int gfrom=group[e.src];
        int gto=group[e.dst];
        if(gfrom==gto){
            continue;
        }else if(isCycle[gto]){
            ng[gfrom].emplace_back(gfrom,gto,e.weight-mins[to].weight);
        }else{
            ng[gfrom].emplace_back(gfrom,gto,e.weight);
        }
    }
    return res+ ChuLiu_Edmonds(ng,group[root]);
}

int main(){
    int v,e,r;cin>>v>>e>>r;
    Graph g(v);
    for(int i=0;i<e;++i){
        int s,t,w;cin>>s>>t>>w;
        g[s].emplace_back(s,t,w);
    }
    cout<<ChuLiu_Edmonds(g,r)<<endl;
    return 0;
}