using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        int N = int.Parse(str[0]);
        int E = int.Parse(str[1]);
        int S = int.Parse(str[2]);
        int[] F = new int[N];
        long[] C = new long[N];
        List<Edge>[] G = new List<Edge>[N];
        for(int i=0;i<N;i++){
            G[i] = new List<Edge>();
            F[i] = -1;
        }
        for(int i=0;i<E;i++){
            str = Console.ReadLine().Split(' ');
            G[int.Parse(str[0])].Add(new Edge(int.Parse(str[0]),int.Parse(str[1]),int.Parse(str[2])));
        }
        Heap H = new Heap(2*E+1);
        H.push(new Edge(S,S,0));
        long cost = 0;
        while(H.size != 0){
            Edge e = H.pop();
            if(F[e.t] == -1){
                F[e.t] = e.f;
                C[e.t] = e.cost;
                cost += e.cost;
                int v = e.t;
                for(int i=0;i<G[v].Count;i++){
                    H.push(G[v][i]);
                }
            }
            else{
                if(C[e.t] > e.cost){
                    int v = e.f;
                    bool b = true;
                    while(v != S){
                        v = F[v];
                        if(v == e.t){
                            b = false;
                            break;
                        } 
                    }
                    if(b){
                        F[e.t] = e.f;
                        cost -= C[e.t] - e.cost;
                        C[e.t] = e.cost;
                    }
                }
            }
        }
        sb.Append(cost+"\n");
    }
}
struct Edge{
    public int f;
    public int t;
    public long cost;
    public Edge(int f0,int t0,long cost0){
        f = f0;
        t = t0;
        cost = cost0;
    }
}
class Heap{
    public int size;
    Edge[] obj;

    public Heap(int maxsize){
        size = 0;
        obj = new Edge[maxsize];
    }
    public bool Compare(Edge a,Edge b){
        return a.cost <= b.cost;
    }
    public void push(Edge a){
        int i = size;
        size++;
        while(i > 0){
            int p = (i - 1)/2;
            if(Compare(obj[p],a)){
                obj[i] = a;
                break;
            }
            obj[i] = obj[p];
            i = p;
        }
        if(i == 0){
            obj[0] = a;
        }
    }
    public Edge pop(){
        Edge t = obj[0];
        int i = 0;
        size--;
        while(2*i+1 < size){
            int p = 2*i+1;
            if(p+1 < size && Compare(obj[p+1],obj[p])){
                p++;
            }
            if(Compare(obj[p],obj[size])){
                obj[i] = obj[p];
                i = p;
            }
            else{
                obj[i] = obj[size];
                break;
            }
        }
        if(2*i+1 >= size){
            obj[i] = obj[size];
        }
        return t;
    }
}