#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int n, m;     cin >> n >> m;     for (long long int i = 0; i < n; i += 1) {       long long int s, f, t;       cin >> s >> f >> t;       long long int q = t / (m - 1);       long long int r = t % (m - 1);       if (s == f) {         cout << t <<   n ;         continue;       }       if (q % 2 == 0) {         long long int floor = r + 1;         if (s >= floor) {           t += (s - floor);           if (f >= s) {             t += (f - s);           } else {             t += (m - s) + (m - f);           }         } else {           t += (m - floor) + (m - s);           if (f > s) {             t += (s - 1) + (f - 1);           } else {             t += (s - f);           }         }       } else {         long long int floor = m - r;         if (s <= floor) {           t += (floor - s);           if (f <= s) {             t += (s - f);           } else {             t += (s - 1) + (f - 1);           }         } else {           t += (floor - 1) + (s - 1);           if (f >= s) {             t += (f - s);           } else {             t += (m - s) + (m - f);           }         }       }       cout << t <<   n ;     }     return 0;   }   