#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
struct Edge {
  int dst;
  int weight;
  Edge(int dst, int weight) : dst(dst), weight(weight) {}
  bool operator<(const Edge &right) const {
    return weight != right.weight ? weight < right.weight : dst < right.dst;
  }
};
void printGraph(vector<vector<Edge> > &G) {
  cout << "graph" << endl;
  for (int i = 0; i < G.size(); i++) {
    for (int j = 0; j < G[i].size(); j++) {
      cout << i << " " << G[i][j].dst << " " << G[i][j].weight << endl;
    }
  }
}
void printVector(vector<int> &v) {
  cout << "vector" << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
vector<vector<Edge> > reverse(vector<vector<Edge> > &G) {
  vector<vector<Edge> > RG(G.size());
  for (int i = 0; i < G.size(); i++) {
    for (int j = 0; j < G[i].size(); j++) {
      Edge e(i, G[i][j].weight);
      RG[G[i][j].dst].push_back(e);
    }
  }
  return RG;
}
int reverseCycleDetect(vector<vector<Edge> > &G, int s, vector<int> &used,
                       vector<int> &cycle) {
  if (used[s] == 1) {
    return 0;
  }
  used[s] = 2;
  for (int i = 0; i < G[s].size(); i++) {
    if (used[G[s][i].dst] == 2) {
      cycle.push_back(G[s][i].dst);
      return 1;
    }
    if (used[G[s][i].dst] == 0) {
      int judge = reverseCycleDetect(G, G[s][i].dst, used, cycle);
      if (judge == 1) {
        if (cycle[0] != G[s][i].dst) {
          cycle.push_back(G[s][i].dst);
          return 1;
        } else {
          return 2;
        }
      } else if (judge == 2) {
        return 2;
      }
    }
  }
  used[s] = 1;
  return 0;
}
vector<int> cycleDetect(vector<vector<Edge> > &G) {
  vector<int> used(G.size(), 0);
  vector<int> cycle;
  for (int i = 0; i < G.size(); i++) {
    reverseCycleDetect(G, i, used, cycle);
    if (!cycle.empty()) break;
  }
  reverse(cycle.begin(), cycle.end());
  return cycle;
}
int findWeight(vector<vector<Edge> > &G, int from, int dest) {
  int i = 0;
  while (G[from][i].dst != dest) {
    i++;
  }
  return G[from][i].weight;
}
int findVector(vector<int> &v, int x) {
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == x) return i;
  }
  return -1;
}
int ChuLiu(vector<vector<Edge> > &G, int r) {
  vector<vector<Edge> > H(G.size());
  vector<vector<Edge> > RG = reverse(G);
  for (int i = 0; i < G.size(); i++) {
    if (i != r && !RG[i].empty()) {
      Edge e = *min_element(RG[i].begin(), RG[i].end());
      Edge f(i, e.weight);
      H[e.dst].push_back(f);
    }
  }
  vector<int> cycle = cycleDetect(H);
  if (cycle.empty()) {
    int S = 0;
    for (int i = 0; i < H.size(); i++) {
      for (int j = 0; j < H[i].size(); j++) {
        S += H[i][j].weight;
      }
    }
    return S;
  } else {
    int S = 0;
    for (int i = 0; i < cycle.size(); i++) {
      S += findWeight(H, cycle[i], cycle[(i + 1) % cycle.size()]);
    }
    vector<vector<Edge> > K(G.size() + 1);
    for (int i = 0; i < G.size(); i++) {
      for (int j = 0; j < G[i].size(); j++) {
        int b1 = findVector(cycle, i);
        int b2 = findVector(cycle, G[i][j].dst);
        if (b1 == -1 && b2 == -1) {
          K[i].push_back(G[i][j]);
        } else if (b1 == -1 && b2 != -1) {
          int w = G[i][j].weight;
          int dest = cycle[b2];
          int from = cycle[(b2 - 1 + cycle.size()) % cycle.size()];
          w -= findWeight(G, from, dest);
          Edge e(G.size(), w);
          K[i].push_back(e);
        } else if (b1 != -1 && b2 == -1) {
          K[G.size()].push_back(G[i][j]);
        }
      }
    }
    return ChuLiu(K, r) + S;
  }
}
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;
    Edge e(t, w);
    G[s].push_back(e);
  }
  cout << ChuLiu(G, r) << endl;
  return 0;
}
