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