#include <bits/stdc++.h>
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
class SCC {
 public:
  int n;
  vector<vector<int> > G;
  vector<vector<int> > rG;
  vector<bool> used;
  vector<int> vs;
  vector<int> cmp;
  SCC(int size) {
    n = size;
    G = rG = vector<vector<int> >(n);
    used = vector<bool>(n);
    cmp = vector<int>(n);
    vs = vector<int>(0);
  }
  void add_edge(int s, int t) {
    G[s].push_back(t);
    rG[t].push_back(s);
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    for (int i = 0; i < rG[v].size(); i++)
      if (!used[rG[v][i]]) rdfs(rG[v][i], k);
  }
  void dfs(int v) {
    used[v] = true;
    for (int i = 0; i < G[v].size(); i++)
      if (!used[G[v][i]]) dfs(G[v][i]);
    vs.push_back(v);
  }
  int scc() {
    for (int i = 0; i < n; i++) used[i] = false;
    for (int v = 0; v < n; v++)
      if (!used[v]) dfs(v);
    for (int i = 0; i < n; i++) used[i] = false;
    int kk = 0;
    for (int i = vs.size() - 1; i >= 0; i--)
      if (!used[vs[i]]) rdfs(vs[i], kk++);
    return kk;
  }
};
struct edge {
  int to, cost;
};
class MCA {
 public:
  int n, r;
  vector<vector<edge> > G, rG, tG, trG, nG;
  vector<bool> used;
  MCA(int size, int R) {
    r = R;
    n = size;
    rG = G = vector<vector<edge> >(n);
  }
  void add_edge(int s, int t, int cost) {
    edge e = {t, cost}, ee = {s, cost};
    G[s].push_back(e);
    rG[t].push_back(ee);
  }
  void dfs(int s) {
    used[s] = true;
    for (int i = 0; i < tG[s].size(); i++)
      if (!used[tG[s][i].to]) dfs(tG[s][i].to);
  }
  int mca() {
    tG = trG = vector<vector<edge> >(n);
    for (int i = 0; i < n; i++)
      if (i != r) {
        int mi = inf;
        for (int j = 0; j < rG[i].size(); j++) mi = min(mi, rG[i][j].cost);
        for (int j = 0; j < rG[i].size(); j++)
          if (mi == rG[i][j].cost) {
            edge e = {i, rG[i][j].cost};
            tG[rG[i][j].to].push_back(e);
            trG[i].push_back(rG[i][j]);
          }
      }
    used = vector<bool>(n);
    dfs(r);
    bool h = true;
    for (int i = 0; i < n; i++)
      if (!used[i]) h = false;
    if (h) {
      int sum = 0;
      for (int i = 0; i < n; i++)
        for (int j = 0; j < tG[i].size(); j++) sum += tG[i][j].cost;
      return sum;
    } else {
      SCC scc(n);
      for (int i = 0; i < n; i++)
        for (int j = 0; j < tG[i].size(); j++) scc.add_edge(i, tG[i][j].to);
      int N = scc.scc();
      MCA mca(N, scc.cmp[r]);
      nG = vector<vector<edge> >(N);
      int sum = 0;
      for (int i = 0; i < n; i++)
        for (int j = 0; j < G[i].size(); j++)
          if (scc.cmp[i] == scc.cmp[G[i][j].to]) {
            sum += G[i][j].cost;
          } else {
            bool h = false;
            int To = G[i][j].to;
            for (int k = 0; k < n; k++)
              if (To != k && scc.cmp[To] == scc.cmp[k]) h = true;
            if (h) {
              mca.add_edge(scc.cmp[i], scc.cmp[To],
                           G[i][j].cost - trG[To][0].cost);
            } else {
              mca.add_edge(scc.cmp[i], scc.cmp[G[i][j].to], G[i][j].cost);
            }
          }
      return sum + mca.mca();
    }
    return false;
  }
};
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  MCA mca(n, k);
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    mca.add_edge(a, b, c);
  }
  cout << mca.mca() << endl;
}
