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