#include <bits/stdc++.h>
using namespace std;
int n, m;
int vremyadoetazha(int s, int t) {
  int M = m - 1;
  int step = t / M;
  int napr = (step % 2) ? 1 : 0;
  int etazhNaT = 0;
  if (napr) {
    etazhNaT = M - t % M;
  } else {
    etazhNaT = t % M;
  }
  int wremya = t;
  if (!napr) {
    if (etazhNaT <= s) {
      wremya += s - etazhNaT;
    } else {
      wremya += (M - etazhNaT) + (M - s);
    }
  } else {
    if (etazhNaT >= s) {
      wremya += etazhNaT - s;
    } else {
      wremya += (etazhNaT) + (s);
    }
  }
  return wremya;
}
int main(int argc, char** argv) {
  scanf("%d%d", &n, &m);
  int delivered = 0;
  int time[100005] = {0};
  for (int i = 0; i < n; i++) {
    int s, f, t;
    scanf("%d%d%d", &s, &f, &t);
    if (s == f) {
      time[i] = t;
      continue;
    }
    time[i] = vremyadoetazha(f - 1, vremyadoetazha(s - 1, t));
  }
  for (int i = 0; i < n; i++) {
    printf("%d\n", time[i]);
  }
  return 0;
}
