#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long f = 1, w = 0;
  char x = 0;
  while (x < '0' || x > '9') {
    if (x == '-') f = -1;
    x = getchar();
  }
  while (x != EOF && x >= '0' && x <= '9') {
    w = (w << 3) + (w << 1) + (x ^ 48);
    x = getchar();
  }
  return w * f;
}
const long long N = 1e6 + 10;
long long n, num_edge, S;
long long m, num_Sdge, Time, Cnt, Top;
long long Low[N], Vis[N], Val[N], U[N];
long long head[N], Sead[N], Dfn[N], f[N];
long long V[N], D[N], Col[N], Dag[N], Stk[N];
struct Edge {
  long long next, to, dis;
} edge[N], Sdge[N];
inline long long SM(long long W) {
  long long X = (sqrt(1 + (W << 3)) - 1) / 2;
  return W * (X + 1) - X * (X + 1) * (X + 2) / 6;
}
inline void Add(long long from, long long to, long long dis) {
  edge[++num_edge].next = head[from];
  edge[num_edge].dis = dis;
  edge[num_edge].to = to;
  head[from] = num_edge;
}
inline void Sdd(long long from, long long to, long long dis) {
  Sdge[++num_Sdge].next = Sead[from];
  Sdge[num_Sdge].dis = dis;
  Sdge[num_Sdge].to = to;
  Sead[from] = num_Sdge;
}
inline void Tarjan(long long pos) {
  Dfn[pos] = Low[pos] = ++Time, Stk[++Top] = pos, Vis[pos] = 1;
  for (long long i = head[pos]; i; i = edge[i].next)
    if (!Dfn[edge[i].to])
      Tarjan(edge[i].to), Low[pos] = min(Low[edge[i].to], Low[pos]);
    else if (Vis[edge[i].to])
      Low[pos] = min(Dfn[edge[i].to], Low[pos]);
  if (Low[pos] == Dfn[pos]) {
    long long Now;
    ++Cnt;
    do Now = Stk[Top--], Vis[Now] = 0, Col[Now] = Cnt;
    while (Now != pos);
  }
}
signed main() {
  n = read(), m = read();
  for (long long i = 1; i <= m; i++)
    U[i] = read(), V[i] = read(), D[i] = read(), Add(U[i], V[i], D[i]);
  S = read();
  for (long long i = 1; i <= n; i++)
    if (!Dfn[i]) Tarjan(i);
  for (long long i = 1; i <= m; i++)
    if (Col[U[i]] == Col[V[i]]) Val[Col[U[i]]] += SM(D[i]);
  for (long long i = 1; i <= n; i++)
    for (long long j = head[i]; j; j = edge[j].next)
      if (Col[i] != Col[edge[j].to])
        Sdd(Col[i], Col[edge[j].to], edge[j].dis + Val[Col[edge[j].to]]),
            Dag[Col[edge[j].to]]++;
  queue<long long> Q;
  for (long long i = 1; i <= Cnt; i++) f[i] = -(1LL << 60);
  f[Col[S]] = Val[Col[S]];
  for (long long i = 1; i <= Cnt; i++)
    if (!Dag[i]) Q.push(i);
  while (Q.size()) {
    long long x = Q.front();
    Q.pop();
    for (long long i = Sead[x]; i; i = Sdge[i].next) {
      f[Sdge[i].to] = max(f[Sdge[i].to], f[x] + Sdge[i].dis);
      if (!(--Dag[Sdge[i].to])) Q.push(Sdge[i].to);
    }
  }
  long long Max = 0;
  for (long long i = 1; i <= Cnt; i++) Max = max(Max, f[i]);
  printf("%lld", Max);
  return 0;
}
