/*每个点到指定点的距离+指定点回到各个点的距离*/
/*每指定点到每个点之间的逆序+指定点到每个点之间的正序*/
#include<iostream>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1000+5;
const int inf = 0x3f3f3f3f;
int n, m, x;
struct Node
{
int v;
int w;
}edge;
vector<Node> RV[N], V[N];
ostream & operator<< (ostream & os, const Node& A){
os << A.v << "=" << A.w;
}
int dist1[N];
int dist2[N];
bool vis[N];
void Dijkstra(int (&dist1)[N], vector<Node> (&RV)[N])//必须加()
{
memset(vis, false, sizeof(vis));
memset(dist1, inf, sizeof(dist1));
dist1[x] = 0;//从到达位置开始
while(1){
int u = 0, v, min=inf;
for(int i=1;i<=n;i++){
if(!vis[i] && min>dist1[i]){
min = dist1[i];
u = i;
}
}
if(!u)
break;
vis[u] = true;
for(int j=0; j<RV[u].size(); j++){
int v = RV[u][j].v;
int w = RV[u][j].w;
if(!vis[v] && dist1[v] > dist1[u] + w){
dist1[v] = dist1[u] + w;
}
}
}
}
int main()
{
while(cin >> n >> m >> x){
int a, b, c;
for(int i=1;i<=m;i++){
cin >> a >> b >> c;
edge.v = b;
edge.w = c;
RV[a].push_back(edge);//返回
edge.v = a;
V[b].push_back(edge);//到达
}
/* for(int i=1; i<=n; i++){
cout << i << "-";
for(int j=0; j<RV[i].size(); j++)
cout << RV[i][j] << "->";cout << endl;
}
for(int i=1; i<=n; i++){
cout << i << "--";
for(int j=0; j<V[i].size(); j++)
cout << V[i][j] << "->";cout << endl;
}
*/
Dijkstra(dist1, RV);
Dijkstra(dist2, V);
int max = - inf;
for(int i=1;i<=n;i++){
// cout << dist1[i] << ":" << dist2[i] << endl;
if(dist1[i]!=inf && dist2[i]!=inf){
if(max<dist1[i]+dist2[i]){
max = dist1[i] + dist2[i];
}
}
}
cout << max << endl;
for(int i=1;i<=n;i++){
RV[i].clear();
V[i].clear();
}
}
return 0;
}