#include <algorithm>
#include <iostream>
#include <vector>
#include <set>
using namespace std;

struct StronglyConnectedComponents {
    int id = 0;
    vector<vector<int>> gg, rg;
    vector<int> comp, order, seen, depth;
    StronglyConnectedComponents(int n) : \
        gg(n), rg(n), comp(n, -1), seen(n, 0), depth(n, 1) { }
    void add_edge(int x, int y) {
        gg[x].emplace_back(y);
        rg[y].emplace_back(x);
    }
    void dfs(int u) {               // order, seen
        seen[u] = true;
        for (int v: gg[u]) if (!seen[v]) dfs(v);
        order.emplace_back(u);
    }
    void rdfs(int u, int _id) {     // comp, depth
        comp[u] = _id;
        for (int v: rg[u]) {
            if (comp[v] == -1) rdfs(v, _id);
            depth[u] = max(depth[u], depth[v] + 1);
        }
    }
    int build() {
        for (int u = 0; u < (int)gg.size(); ++u) if (!seen[u]) dfs(u);
        reverse(begin(order), end(order));
        for (int u: order) if (comp[u] == -1) rdfs(u, id++);
        return id;                  // id < n -> has_cycle
    }
    vector<vector<int>> make_graph() {
        vector<vector<int>> g_comp(id);
        set<pair<int, int>> connect;
        for (int u = 0; u < (int)gg.size(); ++u) for (int v: gg[u]) {
            int x = comp[u], y = comp[v];
            if (x == y) continue;
            if (connect.count({x, y})) continue;
            g_comp[x].emplace_back(y);
            connect.emplace(x, y);
        }
        return g_comp;
    }
    int operator[](int k) { return comp[k]; }
};

template<typename T>
struct Arborescence {
    const T INF = numeric_limits<T>::max();
    using Graph = vector<vector<pair<int, T>>>;
    Graph g;
    Arborescence(int n) : g(n) { };
    void add_edge(int u, int v, T w) { g[u].emplace_back(v, w); }
    T build(Graph &g, int r, T sum) {
        int n = g.size();
        vector<int> par(n, -1);
        vector<T> w_in(n, INF);
        for (int u = 0; u < n; u++) for (auto &e: g[u]) {
            if (w_in[e.first] > e.second) {
                w_in[e.first] = e.second, par[e.first] = u;
            }
        }
        StronglyConnectedComponents scc(n);
        for (int u = 0; u < n; u++) if (r != u) {
            if (par[u] == -1) return INF;
            scc.add_edge(par[u], u), sum += w_in[u];
        }
        int sz = scc.build();
        if (sz == n) return sum;    // no cycle
        Graph g_comp(sz);
        for (int u = 0; u < n; u++) for (auto &e: g[u]) {
            if (scc[u] == scc[e.first]) continue;
            g_comp[scc[u]].emplace_back(scc[e.first], e.second - w_in[e.first]);
        }
        return build(g_comp, scc[r], sum);
    }
    T build(int r) {
        T res = build(g, r, 0);
        return res == INF ? -1 : res;
    }
};

int main() {
    int n, m; cin >> n >> m;
    int r; cin >> r;
    Arborescence<long long> mst(n);
    while (m--) {
        int u, v, w; cin >> u >> v >> w;
        mst.add_edge(u, v, w);
    }
    cout << mst.build(r) << endl;
    return 0;
}

