#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
//_____
#define pb emplace_back
#define fi first
#define se second
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i,f,n) for(int i=(f); i<=(n); ++i)
#define err(...) fprintf(stderr, __VA_ARGS__)
#define show(x) cerr << #x << ':' << (x) << endl
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef pair<int,int> pii;
#define let const auto
template<class A, class B>inline bool chmax(A &a, B b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, B b){return b<a ? a=b,1 : 0;}
#define INF  0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3fLL
#define NIL -1
//_______

int N;
vector <pii> G[101];
bool vis[101];

int slv(let s)
{
   priority_queue< pii, vector<pii>, greater<pii> > pq;
   pq.emplace( pii(0,s) );
   int sum = 0;

   while (!pq.empty()) {
      let tmp = pq.top(); pq.pop();
      if ( vis[tmp.se]++ )continue;
      sum += tmp.fi;
      for(let x : G[tmp.se])
         pq.emplace(x);
   }
   return (sum);
}

signed main()
{
   FAST();
   int m,r;
   cin >> N >> m >> r;

   while (m--) {
      int s, t, w;
      cin >> s >> t >> w;
      G[s].pb(pii(w,t));
   }

   outl( slv(r) );
}


