#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int ans;
  int s, f, t;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &s, &f, &t);
    int tmp = t % (2 * m - 2);
    if (t % (2 * m - 2) < m - 1) {
      if (s - 1 >= tmp) {
        if (f >= s)
          ans = t + f - 1 - tmp;
        else
          ans = t + 2 * m - 2 - tmp - f + 1;
      } else {
        if (f <= s)
          ans = t + 2 * m - 2 - tmp - f + 1;
        else
          ans = t + m - 1 + m - 1 - tmp + f - 1;
      }
    } else {
      tmp = 2 * m - 2 - tmp;
      if (s - 1 <= tmp) {
        if (f <= s)
          ans = t + tmp - f + 1;
        else
          ans = t + tmp + f - 1;
      } else {
        if (f >= s)
          ans = t + tmp + f - 1;
        else
          ans = t + tmp + 2 * m - 2 - f + 1;
      }
    }
    if (s == f) ans = t;
    printf("%d\n", ans);
  }
  return 0;
}
