#include <bits/stdc++.h>
int main() {
  long n, m;
  scanf("%ld %ld", &n, &m);
  while (n--) {
    long s, f, t, v, mov = 0;
    scanf("%ld %ld %ld", &s, &f, &t);
    if (s == f) {
      mov = t;
    } else {
      v = 2 * (m - 1);
      --s;
      --f;
      mov = (t / v) * v;
      if (s < f && (mov + s) < t) {
        mov += v;
      } else if (s > f && (mov + v - s) < t) {
        mov += v;
      }
      if (s < f)
        mov += f;
      else
        mov += (v - f);
    }
    printf("%ld\n", mov);
  }
  return 0;
}
