#include<bits/stdc++.h>
//????°?????????¨?????¨(Chu-Liu/Edmonds?????¢?????´?????????)
//??????
//http://www.creativ.xyz/chu-liu-edmonds-522
using namespace std;
#define rep(i,n)  for(int i=0;i<n;i++)
using pii = pair<int, int>;
using piii = pair<pii, int>;
using Edge  = pair<int, pii>;
using ll = long long;
const int INF=1e9;
#define mp make_pair
//define int ll
ll Chu_Liu_Edmonds(vector<Edge>edge, int V,int start){
    vector<pii>mins(V,mp(INF,-1));
    rep(i,edge.size()){
        Edge now=edge[i];
        mins[now.second.second]=min(mins[now.second.second],mp(now.first,now.second.first));
    }
    mins[start]=mp(-1,-1);
    vector<int>group(V,0);
    vector<bool>IsCycle(V,false);
    int count=0;
    vector<bool>IsUsed(V,false);
    rep(i,V){
        //??????i??????????????¨????????£?????????
        if(IsUsed[i])continue;
        vector<int>chain;
        int cursor=i;
        while(cursor!=-1&&!IsUsed[cursor]){
            //?????????????????£?????????
            IsUsed[cursor]=true;
            chain.emplace_back(cursor);
            //cursor
            cursor=mins[cursor].second;
        }
        if(cursor!=-1){
            //????????????????????????????????£??????????????????????????§??????
            bool InCycle=false;
            rep(j,chain.size()){
                //???????????????????????\?????????????????\????????¨?????????
                group[chain[j]]=count;
                //chan[j]???cursor????????\???????????????????????£??????????????§???????????\
                if(chain[j]==cursor){
                    //count???????????????vertex?????????????????????
                    IsCycle[count]=true;
                    InCycle=true;
                }
                if(!InCycle)count++;
            }
            if(InCycle)count++;
        }else{
            //?????§??????????????????????????????????????????
            rep(j,chain.size()){
                group[chain[j]]=count;
                count++;
            }
        }
    }
    // ??°????????????????????????
    if(count==V){
        ll ans=1;
        rep(i,V)ans+=mins[i].first;
        return ans;
    }
    //????´????????????????????????????????¨?????¨????
    ll res=0;
    rep(i,V){
        //?????????????????????-1?????\??£???????????????
        if(i!=start&&IsCycle[group[i]]){
            res+=mins[i].first;
        }
    }
    //??°????????°??????????§???????????????°???????????????????±???????
    //??°????????????????????°?????????????????????
    vector<Edge>newEdge;
    rep(i,edge.size()){
        Edge _edge=edge[i];
        int to=_edge.second.second;
        int gfrom=group[_edge.second.first];
        int gto=group[_edge.second.second];
        if(gfrom==gto)continue; //??????????????????????????????????????????????????????
        else if(IsCycle[gto])
            newEdge.push_back(mp(_edge.first-mins[to].first,mp(gfrom,gto)));
        else newEdge.push_back((mp(_edge.first,mp(gfrom,gto))));
        
    }
    return res+Chu_Liu_Edmonds(newEdge, count, group[start]);
}
int main(){
    int s;
    int v,e; cin>>v>>e>>s;
    vector<Edge>edge;
    rep(i,e){
        int to,from,cost; cin>>to>>from>>cost;
        edge.push_back(mp(cost,mp(to,from)));
    }
    cout<<Chu_Liu_Edmonds(edge, v,s )<<endl;
}

Measured memory_bytes: 3740