#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long X = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') w = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar();
  return X * w;
}
const long long N = 1000000 + 10;
long long n, m;
struct sedge {
  long long u, v, w;
} se[N];
struct edge {
  long long v, w, nxt;
} e[N];
long long head[N], ecnt = 0;
inline void addEdge(long long u, long long v, long long w) {
  e[++ecnt] = (edge){v, w, head[u]}, head[u] = ecnt;
}
long long dfn[N], low[N], tim = 0, sta[N], in[N], top = 0, bl[N], tot = 0;
inline void tarjan(long long u) {
  dfn[u] = low[u] = ++tim, sta[++top] = u, in[u] = 1;
  for (register long long i = head[u]; i; i = e[i].nxt) {
    long long v = e[i].v;
    if (!dfn[v])
      tarjan(v), low[u] = min(low[u], low[v]);
    else if (in[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (low[u] == dfn[u]) {
    ++tot;
    while (233) {
      long long t = sta[top--];
      in[t] = 0, bl[t] = tot;
      if (u == t) break;
    }
  }
}
long long w[N], dp[N];
inline long long dfs(long long u) {
  if (dp[u]) return dp[u];
  for (register long long i = head[u]; i; i = e[i].nxt)
    dp[u] = max(dp[u], e[i].w + dfs(e[i].v));
  return dp[u] += w[u];
}
inline long long calc(long long w) {
  long long t = sqrt(2 * w + 0.25) - 0.5;
  return w * (t + 1) - (t + 1) * (t + 2) * t / 6;
}
signed main() {
  n = read(), m = read();
  for (register long long i = 1; i <= m; ++i) {
    se[i] = (sedge){read(), read(), read()};
    addEdge(se[i].u, se[i].v, se[i].w);
  }
  for (register long long i = 1; i <= n; ++i)
    if (!dfn[i]) tarjan(i);
  memset(head, 0, sizeof(head)), ecnt = 0;
  for (register long long i = 1; i <= m; ++i) {
    long long u = se[i].u, v = se[i].v;
    if (bl[u] == bl[v])
      w[bl[u]] += calc(e[i].w);
    else
      addEdge(bl[u], bl[v], e[i].w);
  }
  printf("%lld\n", dfs(bl[read()]));
  return 0;
}
