#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000000;
int my_scc[MAX_N], sccno, N;
bool visited[MAX_N];
long long cost[MAX_N];
stack<int> topo;
vector<int> g[MAX_N], revg[MAX_N];
void dfs(int u) {
  visited[u] = true;
  for (const auto& v : g[u])
    if (!visited[v]) dfs(v);
  topo.push(u);
}
void dfs2(int u) {
  visited[u] = true;
  my_scc[u] = sccno - 1;
  for (const auto& v : revg[u])
    if (!visited[v]) dfs2(v);
}
void kosaraju() {
  memset(visited, false, sizeof visited);
  for (int u = (int)0; u < (int)N; ++u) {
    if (!visited[u]) dfs(u);
    for (const auto& v : g[u]) revg[v].push_back(u);
  }
  memset(visited, false, sizeof visited);
  sccno = 0;
  while (topo.size()) {
    int u = topo.top();
    topo.pop();
    if (!visited[u]) {
      ++sccno;
      dfs2(u);
    }
  }
}
const int MAX_SQ = 14150;
vector<pair<int, long long>> newg[MAX_N];
long long prefixsum[MAX_SQ], memo[MAX_N];
tuple<int, int, int> edges[MAX_N];
inline long long gauss(long long k) { return (k * (1LL + k)) >> 1; }
long long getidx(long long val) {
  int low = 0, high = MAX_SQ, mid, best;
  while (low <= high) {
    mid = (low + high) >> 1;
    if (gauss(mid) > val) {
      high = mid - 1;
      best = mid;
    } else
      low = 1 + mid;
  }
  return best;
}
long long dp(int u) {
  if (~memo[u]) return memo[u];
  long long ans = 0LL;
  for (const pair<int, long long> p : newg[u])
    ans = max(ans, p.second + dp(p.first));
  return memo[u] = ans + cost[u];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  memset(memo, -1, sizeof memo);
  prefixsum[0] = 0LL;
  for (int i = (int)1; i < (int)MAX_SQ; ++i)
    prefixsum[i] = prefixsum[i - 1] + gauss(i);
  int m, s, u, v, c;
  cin >> N >> m;
  for (int i = (int)0; i < (int)m; ++i) {
    cin >> u >> v >> c;
    --u;
    --v;
    get<0>(edges[i]) = u;
    get<1>(edges[i]) = v;
    get<2>(edges[i]) = c;
    g[u].push_back(v);
  }
  kosaraju();
  long long idx;
  for (int i = (int)0; i < (int)m; ++i) {
    u = get<0>(edges[i]);
    v = get<1>(edges[i]);
    c = get<2>(edges[i]);
    if (my_scc[u] == my_scc[v]) {
      idx = getidx(c);
      cost[my_scc[u]] += ((idx * (long long)c) - prefixsum[idx - 1]);
    } else
      newg[my_scc[u]].push_back({my_scc[v], c});
  }
  cin >> s;
  cout << dp(my_scc[s - 1]);
  return 0;
}
