#include <bits/stdc++.h>
int main() {
  int n, m;
  while (scanf("%d %d", &n, &m) != EOF) {
    while (n--) {
      int s, f, t;
      scanf("%d %d %d", &s, &f, &t);
      if (s == f) {
        printf("%d\n", t);
        continue;
      }
      int row = t / (m - 1) + (t % (m - 1) != 0 ? 1 : 0);
      if (t == 0) {
        if (s < f) {
          printf("%d\n", f - 1);
        } else {
          printf("%d\n", m - f + m - 1);
        }
        continue;
      }
      if (row & 1) {
        int pos = (t % (m - 1) == 0) ? m : (1 + t % (m - 1));
        if (s < f) {
          if (pos <= s) {
            printf("%d\n", f - pos + t);
          } else {
            printf("%d\n", t + m - pos + m - 1 + f - 1);
          }
        } else {
          printf("%d\n", t + m - pos + m - f);
        }
      } else {
        int pos = (t % (m - 1) == 0) ? 1 : (m - t % (m - 1));
        if (s < f) {
          printf("%d\n", t + pos - 1 + f - 1);
        } else {
          if (s <= pos) {
            printf("%d\n", t + pos - f);
          } else
            printf("%d\n", t + pos - 1 + m - 1 + m - f);
        }
      }
    }
  }
  return 0;
}
