#include <bits/stdc++.h>
template <typename T>
void MACRO_VAR_Scan(T& t) {
  std::cin >> t;
}
template <typename First, typename... Rest>
void MACRO_VAR_Scan(First& first, Rest&... rest) {
  std::cin >> first;
  MACRO_VAR_Scan(rest...);
}
template <typename T>
void MACRO_VEC_ROW_Init(int n, T& t) {
  t.resize(n);
}
template <typename First, typename... Rest>
void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) {
  first.resize(n);
  MACRO_VEC_ROW_Init(n, rest...);
}
template <typename T>
void MACRO_VEC_ROW_Scan(int p, T& t) {
  std::cin >> t[p];
}
template <typename First, typename... Rest>
void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) {
  std::cin >> first[p];
  MACRO_VEC_ROW_Scan(p, rest...);
}
template <typename T>
inline T CHMAX(T& a, const T b) {
  return a = (a < b) ? b : a;
}
template <typename T>
inline T CHMIN(T& a, const T b) {
  return a = (a > b) ? b : a;
}
void CHECKTIME(std::function<void()> f) {
  auto start = std::chrono::system_clock::now();
  f();
  auto end = std::chrono::system_clock::now();
  auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start))
                 .count();
  std::cerr << "[Time:" << res << "ns  (" << res / (1.0e9) << "s)]\n";
}
template <class T>
std::vector<std::vector<T>> VV(int n) {
  return std::vector<std::vector<T>>(n);
}
template <class T>
std::vector<std::vector<T>> VV(int n, int m, T init = T()) {
  return std::vector<std::vector<T>>(n, std::vector<T>(m, init));
}
template <typename S, typename T>
std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) {
  os << "(" << p.first << ", " << p.second << ")";
  return os;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using PAIR = std::pair<int, int>;
using PAIRLL = std::pair<ll, ll>;
constexpr int INFINT = 1 << 30;
constexpr int INFINT_LIM = (1LL << 31) - 1;
constexpr ll INFLL = 1LL << 60;
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62);
constexpr double EPS = 1e-9;
constexpr int MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;
struct StronglyConnectedComponent {
  int n;
  std::vector<std::vector<int>> g, rg;
  std::vector<bool> used;
  std::vector<int> vs;
  std::vector<int> cmp;
  StronglyConnectedComponent(int n) : n(n), g(n), rg(n), used(n), cmp(n) {}
  int operator[](int id) { return cmp[id]; }
  void addEdge(int from, int to) {
    g[from].emplace_back(to);
    rg[to].emplace_back(from);
  }
  void dfs(int v) {
    used[v] = true;
    for (auto& to : g[v]) {
      if (!used[to]) dfs(to);
    }
    vs.emplace_back(v);
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    for (size_t i = 0; i < rg[v].size(); ++i) {
      if (!used[rg[v][i]]) rdfs(rg[v][i], k);
    }
  }
  int scc() {
    used.assign(n, false);
    vs.clear();
    for (size_t v = 0; v < n; ++v) {
      if (!used[v]) dfs(v);
    }
    used.assign(n, false);
    int k = 0;
    for (signed i = vs.size() - 1; i >= 0; --i) {
      if (!used[vs[i]]) rdfs(vs[i], k++);
    }
    return k;
  }
  bool same(int u, int v) { return cmp[u] == cmp[v]; }
  std::vector<std::vector<int>> getDAG() {
    int N = scc();
    std::vector<std::vector<int>> res(N);
    std::vector<std::set<int>> set(N);
    for (int i = 0; i < int(n); ++i) {
      set[cmp[i]].insert(i);
    }
    for (int i = 0; i < int(N); ++i) {
      std::set<int> out;
      for (const auto& v : set[i]) {
        for (const auto& to : g[v]) {
          out.insert(cmp[to]);
        }
      }
      for (const auto& to : out) {
        if (i != to) res[i].emplace_back(to);
      }
    }
    return res;
  }
} scc(0);
std::vector<int> sp;
std::map<int, ll> mapmax;
inline ll calcMax(ll x) {
  auto it = std::upper_bound((sp).begin(), (sp).end(), x);
  it = std::prev(it);
  ll dis = std::distance(sp.begin(), it);
  return x * (dis + 1) - dis * (dis + 1) * (dis + 2) / 6;
}
signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  ;
  {
    int p = 0;
    int t = 0;
    while ((t = p * (p + 1) / 2) < 400000001) {
      sp.emplace_back(t);
      ++p;
    }
  }
  int n, m, s;
  scanf("%d %d", &n, &m);
  std::vector<int> x(m), y(m), w(m);
  for (int i = 0; i < int(m); ++i) scanf("%d %d %d", &x[i], &y[i], &w[i]);
  scanf("%d", &s);
  for (int i = 0; i < int(m); ++i) --x[i], --y[i];
  --s;
  scc = StronglyConnectedComponent(n);
  for (int i = 0; i < int(m); ++i) {
    scc.addEdge(x[i], y[i]);
  }
  auto g(scc.getDAG());
  int N = g.size();
  std::vector<std::map<int, int>> e(N);
  std::vector<ll> VS(N, 0);
  for (int i = 0; i < int(m); ++i) {
    int cx = scc[x[i]], cy = scc[y[i]];
    if (cx == cy)
      VS[cx] += calcMax(w[i]);
    else
      CHMAX(e[cx][cy], w[i]);
  }
  std::vector<int> incnt(N, 0);
  {
    std::vector<bool> visited(N, false);
    std::queue<int> que;
    que.push(scc[s]);
    while (!que.empty()) {
      int now = que.front();
      que.pop();
      for (auto& to : g[now]) {
        ++incnt[to];
        if (visited[to]) continue;
        visited[to] = true;
        que.push(to);
      }
    }
  }
  std::vector<ll> max(N, 0);
  {
    std::queue<int> que;
    int S = scc[s];
    que.push(S);
    while (!que.empty()) {
      int now = que.front();
      que.pop();
      max[now] += VS[now];
      for (auto& to : g[now]) {
        CHMAX(max[to], max[now] + e[now][to]);
        --incnt[to];
        if (incnt[to] == 0) que.push(to);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < int(N); ++i) CHMAX(ans, max[i]);
  std::cout << ans;
  std::cout << "\n";
  ;
  return 0;
}
