#include <bits/stdc++.h>
int main() {
  int i, n, m, a, b, flag, start, t;
  while (scanf("%d%d", &n, &m) != EOF) {
    for (i = 1; i <= n; i++) {
      scanf("%d%d%d", &a, &b, &t);
      if (a == b) {
        printf("%d\n", t);
        continue;
      }
      if (a == 1) {
        start = 0;
        while (start < t) start += 2 * (m - 1);
        printf("%d\n", start + b - a);
      } else if (a == m) {
        start = m - 1;
        while (start < t) start += 2 * (m - 1);
        printf("%d\n", start + a - b);
      } else {
        flag = 1;
        start = a - 1;
        while (start < t) {
          if (flag == 1) {
            flag = -1;
            start += 2 * (m - a);
          } else {
            flag = 1;
            start += 2 * (a - 1);
          }
        }
        if (flag == 1) {
          if (b > a)
            printf("%d\n", start + b - a);
          else
            printf("%d\n", start + 2 * (m - a) + a - b);
        } else {
          if (b < a)
            printf("%d\n", start + a - b);
          else
            printf("%d\n", start + 2 * (a - 1) + b - a);
        }
      }
    }
  }
  return 0;
}
