#include <bits/stdc++.h>
using namespace std;
template <class T>
void pv(T a, T b) {
  for (T i = a; i != b; ++i) cerr << *i << ' ';
  cerr << endl;
}
int main() {
  for (long long N, M; cin >> N >> M;) {
    for (int i = 0; i < (int)(N); ++i) {
      long long S, F, T;
      cin >> S >> F >> T;
      if (S == F) {
        cout << T << endl;
        continue;
      }
      long long loop = (M - 1) * 2;
      long long ans = 0;
      if (S < F) {
        ans = T / loop * loop;
        T = T % loop;
        if (S - 1 < T) ans += (M - 1) * 2;
        ans += F - 1;
      } else {
        ans = T / loop * loop;
        T = T % loop;
        if (M - S + M - 1 < T) ans += (M - 1) * 2;
        ans += M - 1;
        ans += (M - F);
      }
      cout << ans << endl;
    }
  }
  return 0;
}
