import heapq


class UnionFind:

    def __init__(self, size):
        # ????????????????????? (???????????£??¨) ??§??????????????°
        # ??£??????????¬????????´??????¨???
        self.table = [-1 for _ in range(size)]

    def find(self, x):
        # ???????????£??¨????±???????
        while self.table[x] >= 0:
            x = self.table[x]
        return x

    def union(self, x, y):
        # ??????
        s1 = self.find(x)
        s2 = self.find(y)
        if s1 != s2:
            if self.table[s1] >= self.table[s2]:
                self.table[s1] += self.table[s2]
                self.table[s2] = s1
            else:
                self.table[s2] += self.table[s1]
                self.table[s1] = s2
        return self.table[s1]


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)
uf = UnionFind(V)
h = []#[(0, -1, root)]
for u in range(V):
    for i in range(len(nodes[u])):
        nv = nodes[u][i]
        # if u < nv:
        heapq.heappush(h, (cost[u][i], u, nv))
total = 0
forest = []
while len(forest) < V - 1 or h:
    c, s, t = heapq.heappop(h)
    if uf.find(s) != uf.find(t):
        uf.union(s,t)
        forest.append((c,s,t))
        total += c
print(total)