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