#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6 + 7;
vector<long long> g[MAXN];
vector<pair<long long, long long>> gg[MAXN];
set<pair<long long, long long>> ans;
vector<long long> gt[MAXN];
vector<long long> g2[MAXN];
vector<pair<long long, long long>> gt2[MAXN];
vector<long long> topsort;
bool visited[MAXN];
long long color[MAXN];
long long cost[MAXN];
long long vis[MAXN];
long long dp[MAXN];
void topDfs(long long v) {
  visited[v] = true;
  for (long long to : g[v])
    if (!visited[to]) topDfs(to);
  topsort.push_back(v);
}
void dfs(long long v, long long clr) {
  color[v] = clr;
  for (long long to : gt[v])
    if (color[to] == -1) dfs(to, clr);
}
void getTopsort2(long long v) {
  vis[v] = 1;
  for (long long u : g2[v])
    if (!vis[u]) getTopsort2(u);
  topsort.push_back(v);
}
long long t(long long n) { return n * (n + 1) / 2; }
long long sumt(long long n) { return n * (n + 1) * (n + 2) / 6; }
long long f(long long w) {
  long long left = 0, right = 1e5;
  while (left != right - 1) {
    long long m = (left + right) >> 1;
    if (t(m) > w) {
      right = m;
    } else {
      left = m;
    }
  }
  return w * left - sumt(left) + w;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long a, b, w;
    cin >> a >> b >> w;
    --a;
    --b;
    g[a].push_back(b);
    gt[b].push_back(a);
    gg[a].push_back({b, w});
  }
  for (long long i = 0; i < n; i++) {
    color[i] = -1;
    visited[i] = false;
  }
  for (long long i = 0; i < n; i++)
    if (!visited[i]) topDfs(i);
  long long clr = 0;
  for (long long i = topsort.size() - 1; i >= 0; --i)
    if (color[topsort[i]] == -1) {
      dfs(topsort[i], clr);
      ++clr;
    }
  for (long long v = 0; v < n; ++v)
    for (pair<long long, long long> pr : gg[v])
      if (color[v] != color[pr.first]) {
        g2[color[v]].push_back({color[pr.first]});
        gt2[color[pr.first]].push_back({color[v], pr.second});
      } else
        cost[color[v]] += f(pr.second);
  topsort.clear();
  long long s;
  cin >> s;
  --s;
  s = color[s];
  getTopsort2(s);
  reverse(topsort.begin(), topsort.end());
  dp[s] = cost[s];
  for (long long i = 0; i < n; ++i) dp[i] = (long long)(1e18);
  for (long long v : topsort) {
    dp[v] = cost[v];
    for (pair<long long, long long> pr : gt2[v])
      if (dp[pr.first] != (long long)(1e18))
        dp[v] = max(dp[v], dp[pr.first] + pr.second + cost[v]);
  }
  long long ans = -1;
  for (long long i = 0; i < n; ++i)
    if (dp[i] != (long long)(1e18)) ans = max(ans, dp[i]);
  cout << ans;
  return;
}
signed main() {
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
