<gh_stars>1-10
import sys
import heapq


def sol():
    sys.stdin = open("./1238/input.txt")
    input = sys.stdin.readline
    N, M, X = map(int, input().split())
    graph_each = {i: set() for i in range(1, N + 1)}
    graph_X = {i: set() for i in range(1, N + 1)}
    for _ in range(M):
        u, v, w = map(int, input().split())
        graph_each[u].add((v, w))
        graph_X[v].add((u, w))
    print(max([a + b for a, b in zip(dijkstra(graph_each, X), dijkstra(graph_X, X))]))


def dijkstra(graph, start):
    N = len(graph.keys())
    max_int = sys.maxsize
    dist = [max_int] * (N + 1)
    dist[start] = 0
    heap = []
    for i in range(1, N + 1):
        heapq.heappush(heap, (dist[i], i))
    while heap:
        d, u = heapq.heappop(heap)
        if d > dist[u]:
            continue
        for v, w in graph[u]:
            if dist[v] > dist[u] + w:
                dist[v] = dist[u] + w
                heapq.heappush(heap, (dist[v], v))
    return dist[1:]


if __name__ == "__main__":
    sol()

import sys
import heapq


def sol():
    sys.stdin = open("./1238/input.txt")
    input = sys.stdin.readline
    N, M, X = map(int, input().split())
    graph_each = {i: set() for i in range(1, N + 1)}
    graph_X = {i: set() for i in range(1, N + 1)}
    for _ in range(M):
        u, v, w = map(int, input().split())
        graph_each[u].add((v, w))
        graph_X[v].add((u, w))
    print(max([a + b for a, b in zip(dijkstra(graph_each, X), dijkstra(graph_X, X))]))


def dijkstra(graph, start):
    N = len(graph.keys())
    max_int = sys.maxsize
    dist = [max_int] * (N + 1)
    dist[start] = 0
    heap = []
    for i in range(1, N + 1):
        heapq.heappush(heap, (dist[i], i))
    while heap:
        d, u = heapq.heappop(heap)
        if d > dist[u]:
            continue
        for v, w in graph[u]:
            if dist[v] > dist[u] + w:
                dist[v] = dist[u] + w
                heapq.heappush(heap, (dist[v], v))
    return dist[1:]


if __name__ == "__main__":
    sol()