#include <bits/stdc++.h>
using namespace std;
struct edge {
  int from, to;
  int cost;
  edge(int t, int c) : to(t), cost(c) {}
  edge(int f, int t, int c) : from(f), to(t), cost(c) {}
  bool operator<(const edge &e) const { return cost < e.cost; }
};
vector<edge> G[105];
void visit(vector<vector<edge> > h, int v, int s, int r, vector<int> &no,
           vector<vector<int> > &comp, vector<int> &prev,
           vector<vector<int> > &next, vector<int> &mcost, vector<int> &mark,
           int &cost, bool &found) {
  int n = h.size();
  if (mark[v]) {
    vector<int> temp = no;
    found = true;
    do {
      cost += mcost[v];
      v = prev[v];
      if (v != s) {
        while (comp[v].size() > 0) {
          no[comp[v].back()] = s;
          comp[s].push_back(comp[v].back());
          comp[v].pop_back();
        }
      }
    } while (v != s);
    for (int i = 0; i < comp[s].size(); i++) {
      int j = comp[s][i];
      if (j != r) {
        for (int k = 0; k < h[j].size(); k++) {
          edge e = h[j][k];
          if (no[e.from] != s) {
            e.cost -= mcost[temp[j]];
          }
        }
      }
    }
    mark[v] = true;
    for (int i = 0; i < next[v].size(); i++) {
      int j = next[v][i];
      if (no[j] != no[v] && prev[no[j]] == v) {
        if (!mark[no[j]] || j == s) {
          visit(h, j, s, r, no, comp, prev, next, mcost, mark, cost, found);
        }
      }
    }
  }
}
int minimumSpanningAborescence(int n, int s) {
  vector<vector<edge> > H(n);
  for (int u = 0; u < n; u++) {
    for (int i = 0; i < G[u].size(); i++) {
      edge e = G[u][i];
      H[e.to].push_back(e);
    }
  }
  vector<int> no(n);
  vector<vector<int> > comp(n);
  for (int i = 0; i < n; i++) comp[i].push_back(no[i] = i);
  for (int cost = 0;;) {
    vector<int> prev(n, -1);
    vector<int> mcost(n, 1 << 30);
    for (int i = 0; i < n; i++) {
      if (i != s) {
        for (int j = 0; j < H[i].size(); j++) {
          edge e = H[i][j];
          int id = no[i];
          if (no[e.from] != id) {
            if (e.cost < mcost[id]) {
              mcost[id] = e.cost;
              prev[id] = no[e.from];
            }
          }
        }
      }
    }
    vector<vector<int> > next(n);
    for (int i = 0; i < n; i++) {
      if (prev[i] >= 0) {
        next[prev[i]].push_back(i);
      }
    }
    bool stop = true;
    vector<int> mark(n);
    for (int i = 0; i < n; i++) {
      if (i != s && !mark[i] && !comp[i].empty()) {
        bool found = false;
        visit(H, i, i, s, no, comp, prev, next, mcost, mark, cost, found);
        if (found) {
          stop = false;
        }
      }
    }
    if (stop) {
      for (int i = 0; i < n; i++) {
        if (prev[i] >= 0) cost += mcost[i];
      }
      return cost;
    }
  }
}
int main() {
  int n, m, r;
  cin >> n >> m >> r;
  for (int i = 0; i < m; i++) {
    int s, t, w;
    cin >> s >> t >> w;
    G[s].push_back(edge(s, t, w));
  }
  cout << minimumSpanningAborescence(n, r) << endl;
  return 0;
}
