#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

struct edge{
  int v,w;
};

vector<int> minimum_spanning_arborescence(const vector<vector<edge> > &G,int root){
  const int INF=1<<29;
  const int N=G.size();
  vector<int> prev(N,-1),weight(N,INF);
  for(int i=0;i<N;i++){
    for(auto e:G[i]){
      if(e.v!=root&&e.w<weight[e.v]){
	weight[e.v]=e.w;
	prev[e.v]=i;
      }
    }
  }
  vector<int> check(N,N);
  int cycle_len=0;
  vector<char> in_cycle(N);
  for(int i=0;i<N;i++){
    for(int j=i;j>=0;){
      if(check[j]<i)break;
      if(check[j]==i){
	for(int k=j;;){
	  cycle_len++;
	  in_cycle[k]=true;
	  k=prev[k];
	  if(k==j)goto found;
	}
      }
      check[j]=i;
      j=prev[j];
    }
  }
  return prev;
 found:
  int nN=N-cycle_len+1;
  vector<int> newv(N),oldv(N);
  for(int i=0,idx=0;i<N;i++){
    if(!in_cycle[i]){
      oldv[idx]=i;
      newv[i]=idx++;
    }else{
      newv[i]=nN-1;
    }
  }
  vector<vector<edge> > nG(nN);
  vector<int> in(N,-1),out(N,-1);
  vector<int> in_val(N,INF),out_val(N,INF);
  for(int i=0;i<N;i++){
    for(auto e:G[i]){
      int nc=e.w-weight[e.v];
      if(!in_cycle[i]){
	if(!in_cycle[e.v]){
	  nG[newv[i]].push_back({newv[e.v],nc});
	}else{
	  if(nc<in_val[i]){
	    in_val[i]=nc;
	    in[i]=e.v;
	  }
	}
      }else{
	if(!in_cycle[e.v]){
	  if(nc<out_val[e.v]){
	    out_val[e.v]=nc;
	    out[e.v]=i;
	  }
	}
      }
    }
  }
  for(int i=0;i<N;i++){
    if(in_val[i]<INF){
      nG[newv[i]].push_back({nN-1,in_val[i]});
    }
    if(out_val[i]<INF){
      nG[nN-1].push_back({newv[i],out_val[i]});
    }
  }
  auto r=minimum_spanning_arborescence(nG,newv[root]);
  vector<int> rv(N);
  for(int i=0;i<nN-1;i++){
    if(r[i]<0){
      rv[oldv[i]]=r[i];
    }else if(r[i]<nN-1){
      rv[oldv[i]]=oldv[r[i]];
    }else{
      rv[oldv[i]]=out[oldv[i]];
    }
  }
  int v_into_cycle=oldv[r[nN-1]];
  int v_in_cycle=in[v_into_cycle];
  rv[v_in_cycle]=v_into_cycle;
  int x=prev[v_in_cycle];
  for(int i=0;i<cycle_len-1;i++){
    rv[x]=prev[x];
    x=prev[x];
  }
  return rv;
}

int main(){
  int V,E,r;
  cin>>V>>E>>r;
  vector<vector<edge> > G(V);
  for(int i=0;i<E;i++){
    int s,t,w;
    cin>>s>>t>>w;
    G[s].push_back({t,w});
  }
  auto msa=minimum_spanning_arborescence(G,r);
  for(auto e:msa){
    //    cout<<e<<endl;
  }
  int ans=0;
  for(int i=0;i<G.size();i++){
    for(auto e:G[i]){
      if(msa[e.v]==i){
	ans+=e.w;
      }
    }
  }
  cout<<ans<<endl;
}