#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
inline long long input() {
  long long n;
  cin >> n;
  return n;
}
long long pw(long long a, long long b) {
  return (!b ? 1 : (b & 1 ? a * pw(a * a, b / 2) : pw(a * a, b / 2)));
}
const int MAXN = 2e5;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
const long long INF = 8e18;
pair<int, pair<int, int> > v[150001];
long long dp[2][150001];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m, d;
  cin >> n >> m >> d;
  for (int i = 0; i < m; i++) {
    long long a, b, t;
    cin >> a >> b >> t;
    a--;
    v[i] = {t, {a, b}};
  }
  bool z = 0;
  sort(v, v + m);
  fill(dp[1], dp[1] + n, -100000000000000000);
  long long lt = 0;
  deque<int> dq, dq2;
  for (int i = 0; i < m; i++) {
    long long k = (v[i].first - lt) * d;
    dq.clear();
    for (int j = 0; j < n; j++) {
      while (dq.size() && dp[z][dq.back()] <= dp[z][j]) {
        dq.pop_back();
      }
      while (dq.size() && (j - dq.front()) > k) {
        dq.pop_front();
      }
      dq.push_back(j);
      dp[!z][j] = dp[z][dq.front()] +
                  1ll * (v[i].second.second - abs(j - v[i].second.first));
    }
    dq2.clear();
    for (int j = n - 1; j >= 0; j--) {
      while (dq2.size() && dp[z][dq2.back()] <= dp[z][j]) {
        dq2.pop_back();
      }
      while (dq2.size() && (dq2.front() - j) > k) {
        dq2.pop_front();
      }
      dq2.push_back(j);
      dp[!z][j] = max(dp[!z][j],
                      dp[z][dq2.front()] + 1ll * (v[i].second.second -
                                                  abs(j - v[i].second.first)));
    }
    lt = v[i].first;
    z = !z;
  }
  cout << *max_element(dp[z], dp[z] + n);
  return 0;
}
