#include <bits/stdc++.h>
int getint(void) {
  int v = 0, c;
  while (isdigit(c = getchar())) v = 10 * v + c - '0';
  return v;
}
int tf(int m, int t, int x) {
  int te = t % (2 * (m - 1)), e = m - abs(te - m + 1);
  if (te < m - 1 && e <= x || te >= m - 1 && e >= x)
    return abs(e - x);
  else if (te < m - 1)
    return m - e + m - x;
  else
    return e + x - 2;
}
int main(void) {
  int n = getint(), m = getint();
  for (int i = 0; i < n; i++) {
    int s = getint(), f = getint(), t = getint();
    if (s == f)
      printf("%d\n", t);
    else {
      int t1 = t + tf(m, t, s);
      printf("%d\n", t1 + tf(m, t1, f));
    }
  }
}
