#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
input:

6 10 0
0 2 7
0 1 1
0 3 5
1 4 9
2 1 6
1 3 2
3 4 3
4 2 2
2 5 8
3 5 3

output:
11
"""

import sys
import heapq as hp

WHITE, GRAY, BLACK = 0, 1, 2


def generate_adj_table(v_table):
    for each in v_table:
        v_from, v_to, edge_weight = map(int, each)
        init_adj_table[v_from][v_to] = edge_weight
        # init_adj_table[v_to][v_from] = edge_weight

    return init_adj_table


def prim_span_tree():
    distance[init_v] = root
    distance_heap = []
    hp.heappush(distance_heap, (0, init_v))

    while distance_heap:
        current_vertex = hp.heappop(distance_heap)[1]

        color[current_vertex] = BLACK

        for adj, cost in adj_table[current_vertex].items():
            if color[adj] is not BLACK:
                if cost < distance[adj]:
                    distance[adj] = cost
                    parent[adj] = current_vertex
                    color[adj] = GRAY
                    hp.heappush(distance_heap, (cost, adj))

    return distance


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    vertices, edges, root = map(int, _input[0].split())
    v_info = map(lambda x: x.split(), _input[1:])

    parent = [-1] * vertices
    distance = [float('inf')] * vertices
    color = [WHITE] * vertices
    init_v = 0

    init_adj_table = tuple(dict() for _ in range(vertices))
    adj_table = generate_adj_table(v_info)
    print(sum(prim_span_tree()))