//It is made by HolseLee on 17th Aug 2018
//华山论剑
#include<bits/stdc++.h>
#define Max(a,b) (a)>(b)?(a):(b)
using namespace std;
const int N=1001;
const int M=1e5+7;
int n,m,tag,h1[N],h2[N],cnt1,cnt2,dis1[N],dis2[N],ans;
bool vis[N];
struct Node{
int to,val,nxt;
}e1[M],e2[M];
queue<int>t;
inline int read()
{
char ch=getchar();int num=0;bool flag=false;
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)flag=true;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){num=num*10+ch-‘0‘;ch=getchar();}
return flag?-num:num;
}
inline void add1(int x,int y,int z)
{
e1[++cnt1].to=y;
e1[cnt1].val=z;
e1[cnt1].nxt=h1[x];
h1[x]=cnt1;
}
inline void add2(int x,int y,int z)
{
e2[++cnt2].to=y;
e2[cnt2].val=z;
e2[cnt2].nxt=h2[x];
h2[x]=cnt2;
}
void spfa1()
{
memset(vis,0,sizeof(vis));
memset(dis1,0x7f,sizeof(dis1));
while(!t.empty())t.pop();
dis1[tag]=0;vis[tag]=1;
t.push(tag);
int x,y;
while(!t.empty()){
x=t.front();t.pop();
vis[x]=0;
for(int i=h1[x];i!=-1;i=e1[i].nxt){
y=e1[i].to;
if(dis1[y]>dis1[x]+e1[i].val){
dis1[y]=dis1[x]+e1[i].val;
if(!vis[y])t.push(y),vis[y]=1;
}
}
}
}
void spfa2()
{
memset(vis,0,sizeof(vis));
memset(dis2,0x7f,sizeof(dis2));
while(!t.empty())t.pop();
dis2[tag]=0;vis[tag]=1;
t.push(tag);
int x,y;
while(!t.empty()){
x=t.front();t.pop();
vis[x]=0;
for(int i=h2[x];i!=-1;i=e2[i].nxt){
y=e2[i].to;
if(dis2[y]>dis2[x]+e2[i].val){
dis2[y]=dis2[x]+e2[i].val;
if(!vis[y])t.push(y),vis[y]=1;
}
}
}
}
int main()
{
n=read();m=read();tag=read();
memset(h1,-1,sizeof(h1));
memset(h2,-1,sizeof(h2));
int x,y,z;
for(int i=1;i<=m;++i){
x=read();y=read();z=read();
add1(x,y,z);add2(y,x,z);
}
spfa1();spfa2();
for(int i=1;i<=n;++i){
ans=Max(ans,dis1[i]+dis2[i]);
}
printf("%d\n",ans);
return 0;
}