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