#include <bits/stdc++.h>
using namespace std;
void _dbg(string) { cout << endl; }
template <class H, class... T>
void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.first << "," << p.second << ")";
  return o;
}
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}
using P = pair<int, int>;
class SCC {
 private:
  vector<vector<P>> &G;
  vector<vector<P>> &rG;
  vector<bool> used;
  vector<int> vs;
  int n;
  void dfs(int v) {
    used[v] = true;
    for (auto to : G[v])
      if (!used[to.first]) dfs(to.first);
    vs.push_back((v));
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    group.back().push_back((v));
    for (auto to : rG[v])
      if (!used[to.first]) {
        rdfs(to.first, k);
      }
  }

 public:
  vector<int> cmp;
  vector<vector<int>> group;
  int gc = 0;
  SCC(vector<vector<P>> &g, vector<vector<P>> &r) : G(g), rG(r) {
    n = G.size();
    used = vector<bool>(n, false);
    cmp.resize(n);
    for (int i = (int)(0); i < (int)(n); i++)
      if (!used[i]) dfs(i);
    fill((used).begin(), (used).end(), false);
    for (int i = vs.size() - 1; i >= 0; i--)
      if (!used[vs[i]]) {
        group.push_back((vector<int>()));
        rdfs(vs[i], gc++);
      }
  }
  inline bool same(int i, int j) { return cmp[i] == cmp[j]; }
  inline int operator[](int i) { return cmp[i]; }
};
unordered_map<int, long long> weight_cache;
inline long long calc_weight(int v) {
  if (weight_cache.count(v)) return weight_cache[v];
  long long l = 0, r = v;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (mid * (mid + 1) / 2 <= v)
      l = mid;
    else
      r = mid;
  }
  long long ret = l * (l + 1) * (2 * l + 1) / 6 + (v - l * (l + 1) / 2) * r;
  return weight_cache[v] = ret;
}
int main() {
  for (int i = (int)(0); i < (int)(30); i++) calc_weight(i);
  int n, m;
  scanf("%d %d", &n, &m);
  vector<vector<P>> g(n), rg(n);
  for (int i = (int)(0); i < (int)(m); i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    a--;
    b--;
    g[a].push_back((make_pair((b), (c))));
    rg[b].push_back((make_pair((a), (c))));
  }
  int s;
  scanf("%d", &s);
  s--;
  SCC scc(g, rg);
  int gn = scc.gc;
  vector<long long> val(gn, 0);
  for (int i = (int)(0); i < (int)(n); i++) {
    int grp = scc.cmp[i];
    for (auto &p : g[i])
      if (scc.cmp[p.first] == grp) val[grp] += calc_weight(p.second);
  }
  vector<long long> dp(gn, 0);
  for (int i = (int)(scc.cmp[s]); i < (int)(gn); i++)
    if (i == scc.cmp[s] || dp[i] > 0) {
      dp[i] += val[i];
      for (auto v : scc.group[i]) {
        for (auto &p : g[v]) {
          int to = scc.cmp[p.first];
          if (to != i) {
            dp[to] = max(dp[to], dp[i] + p.second);
          }
        }
      }
    }
  cout << *max_element((dp).begin(), (dp).end()) << endl;
  return 0;
}
