func_code_string: #include <bits/stdc++.h>   const int M = 100010;   int n, k;   int l[M], r[M], a[M];   long long dp[M], ans = 1e18;   template <typename T>   inline void read(T &x) {     x = 0;     char ch = getchar();     int f = 1;     for (; ch <  0  || ch >  9 ; ch = getchar())       if (ch ==  - ) f = -1;     for (; ch >=  0  && ch <=  9 ; ch = getchar()) x = x * 10 + ch -  0 ;     x *= f;   }   int main() {     read(n);     read(k);     for (int i = 1; i <= n; i++) read(l[i]), read(r[i]), read(a[i]);     memset(dp, 0x3f, sizeof(dp));     dp[1] = 0;     for (int i = 1; i <= n; i++) {       long long sum = dp[i], res = k;       for (int j = i; j <= n; j++) {         long long reload;         if (a[j] <= res)           reload = 0;         else           reload = (a[j] - res + k - 1) / k;         if (reload > r[j] - l[j]) break;         long long rest = res + reload * k - a[j];         sum += a[j];         if (j == n)           ans = std::min(ans, sum);         else if (l[j] + reload < l[j + 1])           dp[j + 1] = std::min(dp[j + 1], sum + rest);         res = rest;       }     }     if (ans >= 1e16)       printf( -1 n );     else       printf( %lld n , ans);     return 0;   }   