from sys import stdin
readline = stdin.readline

from heapq import merge, heappop
from collections import defaultdict

v, e, r = map(int, readline().split())
g = defaultdict(list)
for _ in range(e):
    s, t, d = map(int, readline().split())
    if t == r:
        continue
    g[s].append((d, t))
for vi in range(v):
    g[vi].sort()
    
heap = iter(g[r])
rest_tree = set(range(v)) - {r}
cost = 0
while rest_tree:
    d, t = next(heap)
    if t in rest_tree:
        cost += d
        rest_tree -= {t}
        heap = merge(heap, g[t])

print(cost)