#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
const int INF = 0x3f3f3f3f;
using namespace std;
int n;
bool vis[1005];
int dis[1005][1005];
int d1[1005], d2[1005];
void init(){
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
dis[i][j] = INF;
}
dis[i][i] = 0;
}
}
void dijkstra1(int s){
for(int i = 1; i <= n; i++){
d1[i] = dis[i][s];
vis[i] = 0;
}
for(int i = 1; i <= n; i++){
int Min = INF;
int k = -1;
for(int j = 1; j <= n; j++){
if(!vis[j] && d1[j] < Min){
k = j;
Min = d1[j];
}
}
if(k == -1)
break;
vis[k] = 1;
for(int j = 1; j <= n; j++){
if(!vis[j] && d1[j] > d1[k] + dis[j][k]){
d1[j] = dis[j][k] +d1[k];
}
}
}
}
void dijkstra2(int s){
for(int i = 1; i <= n; i++){
d2[i] = dis[s][i];
vis[i] = 0;
}
for(int i = 1; i <= n; i++){
int Min = INF;
int k = -1;
for(int j = 1; j <= n; j++){
if(!vis[j] && d2[j] < Min){
k = j;
Min = d2[j];
}
}
if(k == -1)
break;
vis[k] = 1;
for(int j = 1; j <= n; j++){
if(!vis[j] && d2[j] > d2[k] + dis[k][j]){
d2[j] = d2[k] + dis[k][j];
}
}
}
}
int main(){
int m, s, v, u, w;
scanf("%d%d%d", &n, &m, &s);
init();
for(int i = 0; i < m; i++){
scanf("%d%d%d", &v, &u, &w);
if(dis[v][u] > w)dis[v][u] = w;
}
dijkstra1(s);
dijkstra2(s);
int ans = 0;
for(int i = 1; i <= n; i++){
if(ans < d1[i] + d2[i])
ans = d1[i] + d2[i];
}
printf("%d
",ans);
return 0;
}