#include <bits/stdc++.h>
using namespace std;
class GraphCLE {
 private:
  struct node {
    long long int d;
    int edgeid;
    set<int> inc;
    int mode;
  };
  vector<node> nodes;
  struct edgeinfo {
    int from, to, cost;
  };
  struct edge {
    vector<edgeinfo> info;
    int mode;
  };
  vector<edge> edges;
  int n;
  vector<set<int>> cycle;
  bool choose_min_edge(int start) {
    for (int i = 0; i < (int)edges.size(); i++) {
      edgeinfo buf = edges[i].info.back();
      if (buf.to != start && buf.cost < nodes[buf.to].d) {
        nodes[buf.to].d = buf.cost;
        nodes[buf.to].edgeid = i;
      }
    }
    for (int i = 0; i < (int)n; i++) {
      if (i == start) continue;
      if (nodes[i].d == (long long)1e14) return false;
      edges[nodes[i].edgeid].mode = 1;
    }
    return true;
  }
  bool find_cycle() {
    for (int i = 0; i < (int)nodes.size(); i++) {
      if (nodes[i].mode > 0) continue;
      stack<int> stk;
      int b = i;
      while (nodes[b].mode == 0) {
        stk.push(b);
        nodes[b].mode = -1;
        if (nodes[b].edgeid == -1) break;
        b = edges[nodes[b].edgeid].info.back().from;
      }
      if (nodes[b].edgeid != -1 && nodes[b].mode == -1) {
        cycle.push_back({b});
        while (stk.top() != b) {
          cycle.back().insert(stk.top());
          nodes[stk.top()].mode = 1;
          stk.pop();
        }
      }
      while (stk.size()) {
        nodes[stk.top()].mode = 1;
        stk.pop();
      }
    }
    if (cycle.size())
      return true;
    else
      return false;
  }
  bool contract() {
    while (cycle.size()) {
      set<int> pack = cycle.back();
      cycle.pop_back();
      int newnodeid = nodes.size();
      nodes.push_back({(long long)1e14, -1, pack, 0});
      for (int i = 0; i < (int)edges.size(); i++) {
        edgeinfo buf = edges[i].info.back();
        if (pack.find(buf.from) != pack.end()) {
          if (pack.find(buf.to) != pack.end()) {
            if (edges[i].mode == 1)
              edges[i].mode = 2;
            else
              edges[i].mode = -1;
          } else
            edges[i].info.push_back({newnodeid, buf.to, buf.cost});
        } else if (pack.find(buf.to) != pack.end()) {
          int x = buf.cost - edges[nodes[buf.to].edgeid].info.back().cost;
          edges[i].info.push_back({buf.from, newnodeid, x});
          if (x < nodes.back().d) {
            nodes.back().d = x;
            nodes.back().edgeid = i;
          }
        }
      }
      if (nodes.back().edgeid == -1) return false;
      edges[nodes.back().edgeid].mode = 1;
      auto itr = nodes.back().inc.begin();
      while (itr != nodes.back().inc.end()) {
        nodes[*itr].mode = 2;
        itr++;
      }
    }
    return true;
  }
  void unfold() {
    while (nodes.size() > n) {
      while (edges[nodes.back().edgeid].info.back().to >= nodes.size() - 1) {
        edges[nodes.back().edgeid].info.pop_back();
      }
      int flag = edges[nodes.back().edgeid].info.back().to;
      auto itr = nodes.back().inc.begin();
      while (itr != nodes.back().inc.end()) {
        if (edges[nodes[*itr].edgeid].info.back().to == flag) {
          edges[nodes[*itr].edgeid].mode = -1;
          nodes[*itr].d = edges[nodes.back().edgeid].info.back().cost;
          nodes[*itr].edgeid = nodes.back().edgeid;
        } else
          edges[nodes[*itr].edgeid].mode = 1;
        itr++;
      }
      nodes.pop_back();
    }
    for (int i = 0; i < (int)edges.size(); i++) {
      if (edges[i].mode == 1) {
        nodes[edges[i].info[0].to].d = edges[i].info[0].cost;
        nodes[edges[i].info[0].to].edgeid = i;
      }
    }
  }

 public:
  vector<long long int> result;
  GraphCLE(vector<vector<pair<int, int>>> data) {
    n = data.size();
    result.resize(n);
    nodes.resize(n);
    for (int i = 0; i < (int)n; i++) {
      for (int j = 0; j < (int)data[i].size(); j++) {
        edgeinfo buf = {i, data[i][j].first, data[i][j].second};
        edges.push_back({{buf}, 0});
      }
    }
  }
  bool mainfunc(int start) {
    for (int i = 0; i < (int)n; i++) nodes[i] = {(long long)1e14, -1, {}, 0};
    nodes[start].d = 0;
    if (!choose_min_edge(start)) return false;
    while (find_cycle()) {
      if (!contract()) return false;
    }
    unfold();
    for (int i = 0; i < (int)n; i++) {
      result[i] = nodes[i].d;
    }
    return true;
  }
};
int main() {
  int n, m, r;
  cin >> n >> m >> r;
  vector<vector<pair<int, int>>> data(n);
  for (int i = 0; i < (int)m; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    data[s].push_back({t, d});
  }
  GraphCLE cle(data);
  if (cle.mainfunc(r)) {
    long long int ans = 0;
    for (int i = 0; i < (int)n; i++) {
      ans += cle.result[i];
    }
    cout << ans << endl;
  }
  return 0;
}
