func_code_string: #include <bits/stdc++.h>   using namespace std;   void test_case() {     int n;     long long k;     cin >> n >> k;     int l, r[2] = {-1, -1};     long long curr[2] = {k, k}, ans[2] = {0, 0}, monsters;     for (int i = 0; i < n; i++) {       int old_r[2] = {r[0], r[1]}, tmp_r;       long long old_curr[2] = {curr[0], curr[1]}, old_ans[2] = {ans[0], ans[1]};       cin >> l >> tmp_r >> monsters;       ans[0] = -1;       for (int j = 0; j < 2; j++) {         if (old_ans[j] == -1) continue;         if (old_curr[j] + (tmp_r - l) * k < monsters) continue;         if (ans[0] != -1 && ans[0] < old_ans[j] + monsters) continue;         ans[0] = old_ans[j] + monsters;         if (old_curr[j] >= monsters) {           r[0] = l;           curr[0] = old_curr[j] - monsters;         } else {           int turns = (monsters - old_curr[j] + k - 1) / k;           r[0] = l + turns;           curr[0] = k * turns + old_curr[j] - monsters;         }       }       ans[1] = -1;       for (int j = 0; j < 2; j++) {         if (old_ans[j] == -1) continue;         if ((tmp_r - l + 1) * k < monsters) continue;         if (old_r[j] == l) continue;         if (ans[1] != -1 && ans[1] < old_ans[j] + old_curr[j] + monsters)           continue;         ans[1] = old_ans[j] + old_curr[j] + monsters;         int turns = (monsters + k - 1) / k;         r[1] = l + turns - 1;         curr[1] = k * turns - monsters;       }     }     if (ans[0] == -1)       cout << ans[1] <<   n ;     else if (ans[1] == -1)       cout << ans[0] <<   n ;     else       cout << min(ans[0], ans[1]) <<   n ;   }   int main(void) {     ios::sync_with_stdio(0);     cin.tie(0);     int Case = 1;     for (int i = 0; i < Case; i++) {       test_case();     }     return 0;   }   