#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
void Freopen () {freopen("build.in","r",stdin); freopen("build.out","w",stdout);}
void Fclose() {fclose(stdin); fclose(stdout);}
#define MAXM 1000010
#define MAXN 1010
int read()
{
int x=0,f=1; char ch=getchar();
while (ch<‘0‘ || ch>‘9‘) {if (ch==‘-‘) f=-1; ch=getchar();}
while (ch>=‘0‘ && ch<=‘9‘) {x=x*10+ch-‘0‘; ch=getchar();}
return x*f;
}
int N,M,id;
struct RoadNode{int u,v,w,id;}road[MAXM];
struct EdgeNode{int next,to,cap;}edge[MAXM<<1];
int head[MAXN],cnt=1;
void AddEdge(int u,int v,int w) {cnt++; edge[cnt].next=head[u]; head[u]=cnt; edge[cnt].to=v; edge[cnt].cap=w;}
void InsertEdge(int u,int v,int w) {AddEdge(u,v,w); AddEdge(v,u,0);}
int h[MAXN],cur[MAXN],S,T;
bool BFS()
{
queue<int>q;
for (int i=S; i<=T; i++) h[i]=-1;
q.push(S); h[S]=0;
while (!q.empty())
{
int now=q.front(); q.pop();
for (int i=head[now]; i; i=edge[i].next)
if (edge[i].cap && h[edge[i].to]==-1)
h[edge[i].to]=h[now]+1,q.push(edge[i].to);
}
return h[T]!=-1;
}
int DFS(int loc,int low)
{
if (loc==T) return low;
int used=0,w;
for (int i=cur[loc]; i; i=edge[i].next)
if (edge[i].cap && h[edge[i].to]==h[loc]+1)
{
w=DFS(edge[i].to,min(edge[i].cap,low-used));
edge[i].cap-=w,edge[i^1].cap+=w; used+=w;
if (edge[i].cap) cur[loc]=i;
if (used==low) return low;
}
if (!used) h[loc]=-1;
return used;
}
#define INF 0x7fffffff
int Dinic()
{
int re=0;
while (BFS())
{
for (int i=S; i<=T; i++) cur[i]=head[i];
re+=DFS(S,INF);
}
return re;
}
int D;
void BuildGraph()
{
S=0,T=N+1;
for (int i=1; i<=M; i++)
if (road[i].w<=D)
{
if (road[i].id!=id)
InsertEdge(road[i].u,road[i].v,D+1-road[i].w),
InsertEdge(road[i].v,road[i].u,D+1-road[i].w);
else InsertEdge(S,road[i].u,INF),InsertEdge(road[i].v,T,INF);
}
else break;
}
bool cmp(RoadNode A,RoadNode B) {return A.w<B.w;}
int main()
{
N=read(),M=read(),id=read();
for (int i=1; i<=M; i++)
road[i].u=read(),road[i].v=read(),road[i].w=read(),road[i].id=i;
D=road[id].w;
sort(road+1,road+M+1,cmp);
BuildGraph();
printf("%d\n",Dinic());
return 0;
}