#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10, inf = 1e10;
int main() {
  int n, m, s, f, t, salita;
  scanf("%d%d", &n, &m);
  salita = 2 * (m - 1);
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &s, &f, &t);
    if (s == f) printf("%d\n", t);
    if (s < f) {
      int tt = t - (s - 1);
      if (tt <= 0)
        tt = s - 1;
      else if (tt > 0)
        tt = s - 1 + ((tt - 1) / salita + 1) * salita;
      printf("%d\n", tt + f - s);
    }
    if (s > f) {
      int sca = s - 1 + 2 * (m - s);
      int tt = t - sca;
      if (tt <= 0)
        tt = sca;
      else if (tt > 0)
        tt = sca + ((tt - 1) / salita + 1) * salita;
      printf("%d\n", tt + s - f);
    }
  }
  return 0;
}
