#include <bits/stdc++.h>
using namespace std;
vector<int> g[1000000], rg[1000000];
vector<pair<int, long long>> gg[1000000];
int vi[1000000], comp[1000000], curr;
stack<int> stk;
long long wght[1000000], dp[1000000];
vector<pair<int, pair<int, long long>>> edg;
inline void dfs1(int u) {
  vi[u] = 1;
  for (auto x : g[u])
    if (!vi[x]) dfs1(x);
  stk.push(u);
}
inline void dfs2(int u, int curr) {
  vi[u] = 1;
  comp[u] = curr;
  for (auto x : rg[u])
    if (!vi[x]) dfs2(x, curr);
}
inline long long calc(long long x) {
  int l = 0, r = 1000000;
  while (l <= r) {
    int mid = (l + r) >> 1;
    long long chk = 1LL * mid * (mid + 1) / 2LL;
    if (chk > x)
      r = mid - 1;
    else
      l = mid + 1;
  }
  int y = l - 1;
  long long hatao = 1LL * y * (y + 1) * (2 * y + 1) / 6 + y * (y + 1) / 2;
  hatao /= 2LL;
  long long ret = (y + 1) * x * 1LL - hatao;
  return ret;
}
inline long long rec(int u) {
  long long &ret = dp[u];
  if (ret != -1) return ret;
  ret = 0;
  for (auto x : gg[u]) {
    ret = max(ret, x.second + rec(x.first));
  }
  ret += wght[u];
  return ret;
}
int main() {
  int n, m;
  scanf("%d", &n);
  ;
  scanf("%d", &m);
  ;
  for (int x = 0; x < m; x++) {
    int u, v;
    long long w;
    scanf("%d", &u);
    ;
    scanf("%d", &v);
    ;
    scanf("%lld", &w);
    ;
    u--;
    v--;
    g[u].emplace_back(v);
    rg[v].emplace_back(u);
    edg.emplace_back(make_pair(u, make_pair(v, w)));
  }
  memset(vi, 0, sizeof(vi));
  ;
  for (int x = 0; x < n; x++)
    if (!vi[x]) dfs1(x);
  memset(vi, 0, sizeof(vi));
  ;
  curr = 0;
  while (!stk.empty()) {
    int nxt = stk.top();
    stk.pop();
    if (!vi[nxt]) dfs2(nxt, curr), curr++;
  }
  for (int x = 0; x < m; x++) {
    int from = edg[x].first, to = edg[x].second.first;
    long long W = edg[x].second.second;
    if (comp[from] == comp[to])
      wght[comp[from]] += calc(W);
    else
      gg[comp[from]].emplace_back(comp[to], W);
  }
  int start;
  scanf("%d", &start);
  ;
  start--;
  memset(dp, -1, sizeof(dp));
  ;
  printf("%lld\n", rec(comp[start]));
}
