#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long n, m, d;
  cin >> n >> m >> d;
  long long a[m], b[m], t[m];
  map<long long, vector<long long> > time;
  map<long long, vector<long long> > timeb;
  for (long long i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> t[i];
    time[t[i]].push_back(a[i]);
    timeb[t[i]].push_back(b[i]);
  }
  map<long long, vector<long long> >::iterator it, it2;
  long long dp[n + 1];
  memset(dp, 0, sizeof(dp));
  long long ctr = 0;
  long long maxi[n + 1];
  memset(maxi, 0, sizeof(maxi));
  it2 = ++time.begin();
  long long ar[n + 1];
  for (it = time.begin(); it != time.end(); it++, ctr++, it2++) {
    vector<long long> v1 = (*it).second;
    sort(v1.begin(), v1.end());
    vector<long long> v2 = timeb[(*it).first];
    long long temp = 0;
    map<long long, long long> cnt;
    for (long long i = 0; i < v2.size(); i++) {
      temp += -v1[i] + v2[i];
      cnt[v1[i]]++;
    }
    long long right = v1.size();
    long long left = 0;
    ar[0] = temp;
    map<long long, long long>::iterator iter;
    iter = cnt.begin();
    for (long long i = 1; i <= n; i++) {
      ar[i] = ar[i - 1] + right - left;
      if ((i == (*iter).first) && (iter != cnt.end())) {
        iter++;
        right -= cnt[i];
        left += cnt[i];
      }
    }
    for (long long i = 1; i <= n; i++) {
      dp[i] = maxi[i] + ar[i];
    }
    if (it == --time.end()) {
      break;
    }
    long long dist = ((*it2).first - (*it).first) * d;
    dist = min(dist, n);
    deque<long long> q;
    for (long long i = 1; i <= n + dist; i++) {
      if (i - 2 * dist - 1 > 0) {
        long long temp = q.front();
        if (temp == dp[i - 2 * dist - 1]) {
          q.pop_front();
        }
      }
      if (i <= n) {
        while (q.size()) {
          long long temp = q.back();
          if (temp >= dp[i]) {
            break;
          } else {
            q.pop_back();
          }
        }
        q.push_back(dp[i]);
      }
      if (i - dist > 0) {
        maxi[i - dist] = q.front();
      }
    }
  }
  long long res = LONG_LONG_MIN;
  for (long long i = 1; i <= n; i++) {
    res = max(res, dp[i]);
  }
  cout << res << "\n";
}
