#include <bits/stdc++.h>
using namespace std;
int const lim = 150003;
long long dp[2][lim + 3];
vector<pair<int, int> > inp[lim + 3];
long long dif[lim + 3];
long long inf = 1e15;
int main() {
  int n, m, d;
  cin >> n >> m >> d;
  int last = -10;
  for (int i = 1; i <= m; i++) {
    int a, b, t;
    scanf("%d%d%d", &a, &b, &t);
    if (t == last)
      inp[i - 1].push_back({a, b}), i--, m--;
    else
      inp[i].push_back({a, b}), dif[i - 1] = t - last;
    last = t;
  }
  int b = false;
  long long ans = -inf;
  for (int i = m; i >= 1; i--) {
    b = !b;
    deque<pair<long long, int> > cur;
    int last = 1;
    for (int j = 1; j <= n; j++) {
      while (!cur.empty() && cur.front().second < j - dif[i] * d)
        cur.pop_front();
      while (last <= n && j + dif[i] * d >= last) {
        while (!cur.empty() && cur.back().first <= dp[!b][last]) cur.pop_back();
        cur.push_back(make_pair(dp[!b][last], last));
        last++;
      }
      long long tem = 0;
      for (int k = 0; k < inp[i].size(); k++)
        tem += inp[i][k].second - abs(j - inp[i][k].first);
      dp[b][j] = cur.front().first + tem;
      if (i == 1) ans = max(ans, dp[b][j]);
    }
  }
  cout << ans << endl;
}
