text: //
  // Created by OFShare on 2019-10-01
  //
  
  #include <cstdio>
  #include <vector>
  #include <algorithm>
  #include <queue>
  #include <cstring>
  
  const int maxn = 2 * 2e5 + 5;
  const int INF = 1e9;
  // dis[i]: 表示从源点到结点i的最短距离
  int n, m, x, s, dis[maxn], cnt;
  // vis[i]: 表示结点i是否已被标记(访问)过
  bool vis[maxn];
  int head[maxn];
  struct edge {
      int v, w, next;
  }A[maxn];
  
  void addEdge(int u, int v, int w) {
      A[++cnt].v = v;
      A[cnt].w = w;
      A[cnt].next = head[u];
      head[u] = cnt;
  }
  
  void Dijkstra(int start_) {
      dis[start_] = 0;
      std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int> >, std::greater<std::pair<int, int> > > que;
      que.push({dis[start_], start_});
      while (!que.empty()) {
          int d = que.top().first, u = que.top().second;
          que.pop();
          if (vis[u]) continue;
          vis[u] = true;
          for (int i = head[u]; i != -1; i = A[i].next) {
              int v = A[i].v, w = A[i].w;
              // 尝试更新没有标记的结点
              if (!vis[v] && dis[u] + w < dis[v]) {
                  dis[v] = dis[u] + w;
                  que.push({dis[v], v});
              }
          }
      }
  }
  
  // 存边
  struct node {
      int u, v, w;
  };
  std::vector<node> vec;
  
  int main() {
      std::memset(dis, 0x3f3f3f, sizeof dis);
      std::memset(head, -1, sizeof head);
      scanf("%d %d %d", &n, &m, &x);
      for (int i = 1; i <= m; ++i) {
          int u, v, w;
          scanf("%d %d %d", &u, &v, &w);
          addEdge(u, v, w);
          vec.push_back({u, v, w});
      }
      Dijkstra(x);
      int sum1[maxn];
      std::memcpy(sum1, dis, sizeof dis);
      //std::copy_n(dis + 1, n, sum1 + 1);
  
      // 反向边建图
      std::memset(dis, 0x3f3f3f, sizeof dis);
      std::memset(vis, 0, sizeof vis);
      std::memset(head, -1, sizeof head);
      cnt = 0;
      for (int i = 0; i < vec.size(); ++i) {
          int u = vec[i].u, v = vec[i].v, w = vec[i].w;
          addEdge(v, u, w);
      }
      Dijkstra(x);
      int sum2[maxn];
      std::memcpy(sum2, dis, sizeof dis);
      //std::copy_n(dis + 1, n, sum2 + 1);
  
      int ans = 0;
      for (int i = 1; i <= n; ++i) {
          ans = std::max(ans, sum1[i] + sum2[i]);
      }
      printf("%d\n", ans);
      return 0;
  }
  