import heapq

def prim(n, r, path):
  selected = {}
  paths = []
  hq = []
  selected[r] = True

  if r not in path:
    return []

  for s,d,w in path[r]:
    heapq.heappush(hq, (w, [r,d,w]))

  while len(hq) != 0:
    k,root = heapq.heappop(hq)
    if root[1] in selected: continue
    selected[root[1]] = True
    paths.append(root)
    if root[1] in path:
      for s,d,w in path[root[1]]:
        heapq.heappush(hq, (w, [s,d,w]))
  return paths

V,E,r = map(int, raw_input().split())
path = {}
for _ in xrange(E):
  s,t,w = map(int, raw_input().split())
  if s not in path:
    path[s] = []
  path[s].append([s,t,w])
  # if t not in path:
  #   path[t] = []
  # path[t].append([t,s,w])
result = prim(V, r, path)
sumcost = 0
for s,d,v in result:
  sumcost += v
print sumcost