import heapq
V,E,root=map(int, input().split())
nodes = [[] for i in range(V)]
cost = [[] for i in range(V)]
for i in range(E):
    s,t,w = map(int, input().split())
    nodes[s].append(t)
    cost[s].append(w)

visited = [False] * V
total = 0
h = [(0,-1,root)]
while h:
    c,s,t = heapq.heappop(h)
    if visited[t]:
        continue
    total += c
    visited[t] = True
    for i in range(len(nodes[t])):
        nv = nodes[t][i]
        if not visited[nv]:
            heapq.heappush(h,(cost[t][i],t,nv))
print(total)