#include <bits/stdc++.h>
using namespace std;
long N, M;
int F(int s, int f, int t) {
  if (f == s) return t;
  int a = t / (M - 1), b = t % (M - 1);
  int ft;
  if (a % 2 == 0) {
    ft = 1 + b;
    if (ft <= s) {
      t += s - ft;
      if (f >= s)
        return t + f - s;
      else
        return t + 2 * M - f - s;
    } else {
      t += (M - ft) + (M - s);
      if (f <= s)
        return t + s - f;
      else
        return t + s + f - 2;
    }
  } else {
    ft = M - b;
    if (ft >= s) {
      t += ft - s;
      if (f <= s)
        return t + s - f;
      else
        return t + s + f - 2;
    } else {
      t += ft + s - 2;
      if (f >= s)
        return t + f - s;
      else
        return t + 2 * M - f - s;
    }
  }
}
void ReadData() {
  cin >> N >> M;
  int s, f, t;
  for (int i = 1; i <= N; i++) {
    scanf("%d%d%d", &s, &f, &t);
    printf("%d\n", F(s, f, t));
  }
}
int main() { ReadData(); }
