import heapq as pq
n,E,r = map(int, input().split())
M = [[] for i in range(n)]

for i in range(E):
 s,t,w = map(int, input().split())
 M[s].append([t,w])

def prim(r):
 d = [float("inf")] * n
 d[r] = 0
 H = []
 pq.heappush(H, [0, r])
 while H:
  u2,u1 = pq.heappop(H)
  if d[u1] < u2: continue
  for v1, v2 in M[u1]:
   if d[v1] > v2:
    d[v1] = v2
    pq.heappush(H, [d[v1], v1])
 return sum(d)
 
print(prim(r))