#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
stack<int> s;
vector<pair<long long, long long> > G[N];
int dfsnum[N], low[N], scc[N];
bool mk[N], erased[N];
long long cost[N];
int cnt, SCC;
const int oo = 1e5 + 100;
void DFS(int u) {
  s.push(u);
  low[u] = dfsnum[u] = cnt++;
  mk[u] = true;
  for (auto p : G[u]) {
    int v = p.first;
    if (!erased[v]) {
      if (!mk[v]) {
        DFS(v);
        low[u] = min(low[u], low[v]);
      } else
        low[u] = min(low[u], dfsnum[v]);
    }
  }
  if (dfsnum[u] == low[u]) {
    ++SCC;
    while (s.top() != u) {
      erased[s.top()] = true;
      scc[s.top()] = SCC;
      s.pop();
    }
    scc[u] = SCC;
    erased[u] = true;
    s.pop();
  }
}
long long val(long long a) { return a * (a + 1) / 2; }
long long sum(long long a) { return a * (a + 1) * (a + 2) / 6; }
long long get_val(long long x) {
  long long lo = 0, hi = oo;
  while (lo < hi) {
    long long m = (lo + hi + 1) / 2;
    if (x - val(m) >= 0) {
      lo = m;
    } else
      hi = m - 1;
  }
  return x * (lo + 1) - sum(lo);
}
int n, m;
vector<pair<long long, long long> > Gr[N];
long long dp[N];
long long solve(int u) {
  if (dp[u] != -1) return dp[u];
  dp[u] = 0;
  for (auto pp : Gr[u]) {
    int v = pp.first;
    long long w = pp.second;
    long long x = solve(v);
    dp[u] = max(dp[u], w + x);
  }
  dp[u] += cost[u];
  return dp[u];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    G[u].push_back({v, w});
  }
  int src;
  scanf("%d", &src);
  for (int i = 1; i <= n; i++) {
    if (!mk[i]) {
      DFS(i);
    }
  }
  for (int u = 1; u <= n; u++) {
    for (auto p : G[u]) {
      int v = p.first;
      long long w = p.second;
      if (scc[u] == scc[v]) {
        cost[scc[u]] += get_val(w);
      } else {
        Gr[scc[u]].push_back({scc[v], w});
      }
    }
  }
  memset(dp, -1, sizeof dp);
  printf("%lld\n", solve(scc[src]));
  return 0;
}
